Fatskills
Practice. Master. Repeat.
Study Guide: Docker Docker Dockerfile and Building Images Multistage Builds Separate Build and Runtime Dependencies
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-dockerfile-and-building-images-multistage-builds-separate-build-and-runtime-dependencies

Docker Docker Dockerfile and Building Images Multistage Builds Separate Build and Runtime Dependencies

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~3 min read

1. Core Command / Concept

Multi-stage builds separate build and runtime dependencies by using multiple Dockerfiles to create a final image. Use the --build-arg flag to pass arguments to the build stage.

2. Step-by-Step

  1. Create a new directory for your project and navigate into it: mkdir multi-stage-build && cd multi-stage-build
  2. Create a file named Dockerfile and add the following content: FROM python:3.9-slim AS build-stage
  3. Add the following command to install dependencies in the build stage: RUN pip install -r requirements.txt
  4. Add the following command to copy the source code into the build stage: COPY . /app
  5. Add the following command to build the application in the build stage: RUN python -m build
  6. Create a new file named Dockerfile.runtime and add the following content: FROM python:3.9-slim
  7. Add the following command to copy the built application into the runtime stage: COPY --from=build-stage /app/dist /app
  8. Add the following command to run the application in the runtime stage: CMD ["python", "app.py"]
  9. Build the image using the multi-stage build: docker build -t myapp -f Dockerfile.runtime .
  10. Run the image: docker run -p 8000:8000 myapp
  11. ⚠️ This deletes all stopped containers: docker system prune -f

3. Real Commands with Examples


Example 1: Separate build and runtime dependencies

  • Purpose: Build a Python application with separate build and runtime dependencies.
  • Command: docker build -t myapp -f Dockerfile.runtime .
  • Brief explanation of flags: -t sets the image name, -f specifies the Dockerfile to use.

Example 2: Use --build-arg to pass arguments to the build stage

  • Purpose: Pass a build argument to the build stage.
  • Command: docker build -t myapp -f Dockerfile.runtime --build-arg VCS_REF=$(git rev-parse --short HEAD) .
  • Brief explanation of flags: --build-arg sets a build argument, VCS_REF is the build argument name.

Example 3: Use COPY --from to copy files from the build stage

  • Purpose: Copy files from the build stage to the runtime stage.
  • Command: docker build -t myapp -f Dockerfile.runtime .
  • Brief explanation of flags: COPY --from copies files from the specified stage.

4. Common Errors


Error 1: COPY --from not found

  • Error message or symptom: COPY --from not found
  • Why it happens: The COPY --from flag is only available in Docker 17.05 and later.
  • One-line fix: docker --version ? Verify with 'docker version'

Error 2: --build-arg not found

  • Error message or symptom: --build-arg not found
  • Why it happens: The --build-arg flag is only available in Docker 17.05 and later.
  • One-line fix: docker --version ? Verify with 'docker version'

Error 3: FROM not found

  • Error message or symptom: FROM not found
  • Why it happens: The FROM instruction is missing in the Dockerfile.
  • One-line fix: docker build -t myapp -f Dockerfile.runtime .

5. Quick Checks


Scenario 1: Check if the build stage is working

  • Situation: You want to check if the build stage is working correctly.
  • Command: docker run -it --rm myapp /bin/bash
  • What a correct response looks like: The build stage should be able to run the application.

Scenario 2: Check if the runtime stage is working

  • Situation: You want to check if the runtime stage is working correctly.
  • Command: docker run -p 8000:8000 myapp
  • What a correct response looks like: The runtime stage should be able to run the application.

Scenario 3: Check if the COPY --from command is working

  • Situation: You want to check if the COPY --from command is working correctly.
  • Command: docker run -it --rm myapp /bin/bash
  • What a correct response looks like: The COPY --from command should be able to copy files from the build stage.

6. Last-Minute Reference

  • --build-arg sets a build argument.
  • COPY --from copies files from the specified stage.
  • FROM specifies the base image.
  • RUN executes a command in the build stage.
  • CMD sets the default command to run in the runtime stage.
  • docker build builds an image from a Dockerfile.
  • docker run runs a container from an image.
  • docker system prune removes all stopped containers.
  • docker --version displays the Docker version.


ADVERTISEMENT