skip to Main Content

I have this docker file configurations for my angular application and It works fine to build a multiple images for each environment and create any numbers of containers

# Stage 1: Use the official Node.js image as the base image
FROM node:14.10-alpine AS node

# Set the working directory inside the container
WORKDIR /app

# copy these over first and run 'npm install' so the node_modules will be cached
# until the package.json / lock changes
COPY package*.json ./

# Install the dependencies
RUN npm install

# Copy the entire project directory into the container
COPY . .

# Build the Angular app for the desired environment
ARG environment
RUN npm run build:$environment

# Delete the not needed source after the build is done
RUN rm -rf src

# Stage 2: Create the final image with Nginx
# Use the nginx:alpine base image
FROM nginx:alpine

# Copy the compiled Angular application from the 'node' image to the nginx HTML directory
COPY --from=node  /app/dist /usr/share/nginx/html

# Copy the custom nginx configuration file to the appropriate location to overwrite the default configuration
COPY ./nginx-custom.conf /etc/nginx/conf.d/default.conf

using docker build --pull --rm --build-arg environment=local -f "Dockerfile" -t imag_name:latest "." for build and docker run --rm -d -p 80:80/tcp image_name:latest for creating a container and run it in specific port

but I need to update it to build only one image once for all environments and multi containers from it for each environment could you please tell me how the configurations should look like

2

Answers


  1. Chosen as BEST ANSWER

    the is the solution for my problem to build only one image and multi containers for each environment from the same image https://gabrielemilan.dev/publish-same-docker-image-with-angular-application-to-any-environments-edbfc454c6bb


  2. You can modify your Dockerfile as follows:

    # Stage 1: Use the official Node.js image as the base image
    FROM node:14.10-alpine AS node
    
    # Set the working directory inside the container
    WORKDIR /app
    
    # copy these over first and run 'npm install' so the node_modules will be cached
    # until the package.json / lock changes
    COPY package*.json ./
    
    # Install the dependencies
    RUN npm install
    
    # Copy the entire project directory into the container
    COPY . .
    
    # Build the Angular app for the desired environment (default to 'local' if not provided)
    ARG environment=local
    RUN npm run build:$environment
    
    # Delete the not needed source after the build is done
    RUN rm -rf src
    
    # Stage 2: Create the final image with Nginx
    # Use the nginx:alpine base image
    FROM nginx:alpine
    
    # Copy the compiled Angular application from the 'node' image to the nginx HTML directory
    COPY --from=node  /app/dist /usr/share/nginx/html
    
    # Copy the custom nginx configuration file to the appropriate location to overwrite the default configuration
    COPY ./nginx-custom.conf /etc/nginx/conf.d/default.conf
    

    Now, you can build the Docker image and create containers for different environments using the following commands:

    # Build the Docker image (default environment is 'local')
    docker build --pull --rm -t image_name:latest .
    
    # Create a container for the 'local' environment
    docker run --rm -d -p 80:80/tcp -e environment=local image_name:latest
    
    # Create a container for a different environment (e.g., 'production')
    docker run --rm -d -p 8080:80/tcp -e environment=production image_name:latest
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search