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
Dockerfileusing 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:
Create a new directory called
myappand navigate into it.mkdir myapp cd myappDownload the NodeJS source code from
https://github.com/labex-labs/nodejs-example.git.git clone https://github.com/labex-labs/nodejs-example.gitMove the downloaded code into the
myappdirectory.mv nodejs-example/* . rm -rf nodejs-exampleCreate a new file named
Dockerfilein themyappdirectory 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
myappin the/home/labex/projectdirectory. - Clone the NodeJS source code into the
myappdirectory. - Create a
Dockerfilein themyappdirectory 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:
Use the
docker buildcommand to build a new image calledmyapp.docker build -t myapp .Verify that the image was built successfully using
docker images.
Requirements
To complete this challenge, you will need:
- Execute the
docker buildcommand in the/home/labex/project/myappdirectory. - Name the image
myapp.
Run a Docker Container
In this step, you will use the Docker image to run a container.
Tasks
- Use the
myappimage to run a container. The container should expose port3000on the container to port3030on the host, and then access the application in your browser.
Example
Here's an example of what you should be able to accomplish:
Start a new Docker container called
my-app.docker run --name my-app -p 3030:3000 myappOpen a web browser and navigate to
http://localhost:3030to access the running application.
Requirements
To complete this challenge, you will need:
- Run the container with the name
my-app. - Map port
3000inside the container to port3030on 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.



