WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Docker · Intermediate · question 26 of 100

How do you use multi-stage builds in Docker, and why are they important?

📕 Buy this interview preparation book: 100 Docker questions & answers — PDF + EPUB for $5

Multi-stage builds are a powerful feature in Docker that allow you to build a Docker image in multiple stages, and only include the necessary files and dependencies in the final image. This can help to reduce the size of the image, improve security, and speed up the build process. Here’s how to use multi-stage builds in Docker:

Define multiple stages in the Dockerfile: A multi-stage build consists of multiple stages, each of which is defined in the Dockerfile using the FROM keyword. Each stage can be based on a different base image, and can be used to perform different tasks, such as building the application and copying files.

    FROM node:14-alpine AS build-stage
    WORKDIR /app
    COPY package*.json ./
    RUN npm install
    COPY . .
    RUN npm run build
    
    FROM nginx:1.21-alpine
    COPY --from=build-stage /app/dist /usr/share/nginx/html

Name the stages: Each stage can be named using the AS keyword, which allows you to reference the stage later in the Dockerfile.

Copy files between stages: To copy files between stages, use the COPY –from command, which allows you to copy files from one stage to another. This is useful for copying files that were built in an earlier stage to the final image.

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

Use the final stage as the base image: The final stage in the Dockerfile should be used as the base image for the final image. This is the image that will be pushed to the Docker registry and used to run containers.

    FROM nginx:1.21-alpine
    COPY --from=build-stage /app/dist /usr/share/nginx/html

Multi-stage builds are important for several reasons:

Reduced image size: Multi-stage builds allow you to only include the necessary files and dependencies in the final image, which can help to reduce the size of the image. This is especially important for applications that require a large number of dependencies or files.

Improved security: By using multiple stages, you can separate the build environment from the final image, which can help to improve security. This can help to prevent attacks that target vulnerabilities in the build environment.

Faster builds: Multi-stage builds can help to speed up the build process, since only the necessary files and dependencies are included in the final image. This can help to reduce the time required to build and deploy Docker images.

Overall, multi-stage builds are a powerful feature in Docker that can help to reduce the size of images, improve security, and speed up the build process. By using multi-stage builds, you can create more efficient and secure Docker images, and deploy them more quickly and easily.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Docker interview — then scores it.
📞 Practice Docker — free 15 min
📕 Buy this interview preparation book: 100 Docker questions & answers — PDF + EPUB for $5

All 100 Docker questions · All topics