Docker Multi-stage Build Challenge

DockerBeginner
Practice Now

Introduction

Docker Multi-stage Build is a feature that allows you to use multiple stages in a Dockerfile to create a final Docker image. In this challenge, you will learn how to use Docker Multi-stage Build to create a smaller and more efficient Docker image.

Create a Dockerfile with Multi-stage Build

In this section, you will create a Dockerfile that uses a multi-stage build.

Tasks

  • Create a new Dockerfile using two stages. The first stage builds the application by installing the required dependencies and running the build script. The second stage creates the final image by copying the compiled code from the first stage, installing only the production dependencies, and exposing the necessary port.

Example

Here's an example of the steps you'll take:

  1. Create a new directory called myapp and navigate into it.

    mkdir myapp
    cd myapp
    
  2. Download the NodeJS source code from https://github.com/labex-labs/nodejs-example.git.

    git clone https://github.com/labex-labs/nodejs-example.git
    

    Move the downloaded code into the myapp directory.

    mv nodejs-example/* .
    rm -rf nodejs-example
    
  3. Create a new file named Dockerfile in the myapp directory with the following content:

    ## Stage 1: Build the application
    FROM node:14-alpine AS base
    WORKDIR /app
    COPY . ./
    RUN npm install
    
    ## Stage 2: Create the final image
    FROM node:14-alpine
    WORKDIR /app
    COPY --from=base /app/ .
    EXPOSE 3000
    CMD [ "npm", "start" ]
    

Requirements

To complete this challenge, you will need:

  • Create a directory named myapp in the /home/labex/project directory.
  • Clone the NodeJS source code into the myapp directory.
  • Create a Dockerfile in the myapp directory with the specified content.

Build a Docker Image

In this step, you will create an image using the Dockerfile.

Tasks

  • Build a new image called myapp.

Example

Here's an example of what you should be able to accomplish:

  1. Use the docker build command to build a new image called myapp.

    docker build -t myapp .
    
  2. Verify that the image was built successfully using docker images.

Requirements

To complete this challenge, you will need:

  • Execute the docker build command in the /home/labex/project/myapp directory.
  • Name the image myapp.

Run a Docker Container

In this step, you will use the Docker image to run a container.

Tasks

  • Use the myapp image to run a container. The container should expose port 3000 on the container to port 3030 on the host, and then access the application in your browser.

Example

Here's an example of what you should be able to accomplish:

  1. Start a new Docker container called my-app.

    docker run --name my-app -p 3030:3000 myapp
    
  2. Open a web browser and navigate to http://localhost:3030 to access the running application.

Requirements

To complete this challenge, you will need:

  • Run the container with the name my-app.
  • Map port 3000 inside the container to port 3030 on the host.

Summary

In this challenge, you learned how to use Docker Multi-stage Build to create a smaller and more efficient Docker image. By using multiple stages in a Dockerfile, you can separate the build and runtime environments, and reduce the size of the final Docker image. With this knowledge, you can now start building your own Docker images using Multi-stage Build.

✨ Check Solution and Practice✨ Check Solution and Practice✨ Check Solution and Practice