Multi-stage builds

Intermediate Docker

Mike Metzger

Data Engineering Consultant

Single-stage builds

  • Typical Docker images are created using a single FROM command
  • Each addition to the source image adds space and makes its management
  • Consider an application that must be compiled prior to use
    • You can add all the necessary components to the image, compile it, and then configure the final image for use
    • This often leaves superfluous content in the image even if it is not used
FROM ubuntu
RUN apt update
RUN apt install gcc -y
...
RUN make
CMD ["data_app"]
Intermediate Docker

Multi-stage builds

  • Multi-stage builds use multiple containers
  • Typically has one or more build stages
  • Final components are copied into a final container image
  • The build stages are then removed automatically
    • Saving space and minimizing the size of the container image
  • Uses some additional syntax in the Dockerfile
    • AS <alias>
    • COPY --from=<alias>
Intermediate Docker

Multi-stage build example

# Create initial build stage
FROM ubuntu AS stage1

# Install compiler and compile code RUN apt install gcc -y ... RUN make
# Start new stage to create final image FROM alpine-base
# Copy from first stage to final COPY --from=stage1 /data_app /data_app
# Run application on container start CMD ["data_app"]
Intermediate Docker

Let's practice!

Intermediate Docker

Preparing Video For Download...