angulardockerdeploymentgitlabopenshift

Deploying an Angular Application to OpenShift via GitLab


My company utilizes OpenShift for application deployment. As per guidance from my senior, the recommended approach involves utilizing GitLab as an intermediate step. The process entails creating and pushing the container to a GitLab repository, and then using that repository (containing the container) to deploy the application on OpenShift.

I'm seeking assistance from anyone experienced in this process to provide detailed steps. Specifically, I aim to deploy an Angular application following this workflow.

Thank you in advance for your guidance.


Solution

  • Step 1: Set Up GitLab Repository Create a GitLab Account: If you don't have one already, sign up for a GitLab account. Create a New Project: In GitLab, create a new project where you'll store your Angular application code. Clone the Project Locally: Use Git to clone the newly created GitLab project to your local machine. Step 2: Angular Application Setup Create Angular Application: If you haven't already, create your Angular application using Angular CLI:

    ng new my-angular-app

    Develop Your Application: Write your Angular application code within the src directory.
    Step 3: Dockerize Your Angular Application
    Create Dockerfile: In the root directory of your Angular project, create a Dockerfile with the following content:
    # Use official Node.js image as the base image
    FROM node:14 AS build
    # Set the working directory
    

    WORKDIR /app

    # Copy package.json and package-lock.json
    

    COPY package*.json ./

    # Install dependencies
    

    RUN npm install

    # Copy the application files
    

    COPY . .

    # Build the Angular app
    

    RUN npm run build -- --prod

    # Use nginx as the base image for serving content
    FROM nginx:alpine
    # Copy built Angular app to nginx
    

    COPY --from=build /app/dist/my-angular-app /usr/share/nginx/html

    # Expose port 80
    EXPOSE 80
    # Command to run nginx
    

    CMD ["nginx", "-g", "daemon off;"]

    Build Docker Image: Build your Docker image using the Dockerfile:
    docker build -t my-angular-app .
    Step 4: Push Docker Image to GitLab Registry
    Tag Your Docker Image: Tag your Docker image with the GitLab registry URL:
    docker tag my-angular-app:latest registry.gitlab.com/your-username/my-angular-app:latest
    Login to GitLab Registry: Login to your GitLab registry:
    docker login registry.gitlab.com
    Push Docker Image: Push your Docker image to the GitLab registry:
    

    docker push registry.gitlab.com/your-username/my-angular-app:latest

    Step 5: Deploy to OpenShift
    Login to OpenShift: Login to your OpenShift cluster using the command-line interface (CLI).
    Create Deployment Configuration: Create a deployment configuration for your Angular application in OpenShift, specifying the GitLab registry image as the image source.
    

    oc create deployment my-angular-app --image=registry.gitlab.com/your-username/my-angular-app:latest

    Expose Service: Expose your application as a service to make it accessible outside the OpenShift cluster:
    

    oc expose deployment my-angular-app --port=80 --type=LoadBalancer

    Access Your Application: Once deployed, you can access your Angular application using the provided route.