2. Separating Application Build and Execution with Multi-Stage Builds Flashcards
What extra step do we need to specify in the Dockerfile when using compiled languages?
Compile the source code into a binary artifact before it can be defined as an executable for the derived container.
When it comes to Golang, it looks like this:
RUN go build -o mini
# before
ENTRYPOINT [”./mini”]
~~~
~~~
What are the two main problems with using compiled languages to develop our app in a Docker container?
- Increased complexity — not trivial to code and hot reload with a bind mount.
- Larger image size — development tools (eg. SDKs) are captured inside the container image.
What is a common solution for accommodating compiled languages while maintaining the benefits of developing inside a Docker container?
Using the Builder Pattern.
What is the Builder Pattern?
A Docker development pattern that splits out the build step sequence from the run step sequence using separate Dockerfiles for each task.
How do you specify the name and the tag while building a Docker image using docker build
?
By using the --tag
option.
docker build --tag mini .
How do you automatically remove the container if it already exists when running docker run
?
By using the --rm
option.
How do you specify which Dockerfile to build an image from while running docker build
?
By using the --file
option.
docker build --tag my-builder --file Dockerfile.build .
What is a better solution for separating our build step than using separate Dockerfiles?
Using a multi-stage Dockerfile.
What makes a Dockerfile a multi-stage Dockerfile?
Using multiple FROM
instructions to logically separate the different parts of a container image — each FROM
instruction delineates a different stage.
What does an image built by using a multi-stage Dockerfile contain?
Only the content of the final stage.
How does Docker help us reference a build stage?
By numbering each stage starting from zero.
How do we name stages of a multi-stage Dockerfile?
By using the AS
keyword when defining a build stage using FROM
.
FROM node:14 AS builder
In a Dockerfile build stage, how do you instruct to copy files from a preceding stage?
By using the COPY
instruction and specifying the build stage to copy the content from using the --from
option.
COPY --from=builder /deps /app
How do you specify which build stage to use when running docker build
?
By using the --target
option with the build stage name or number.
docker build --tag app-builder --target builder .
What are the benefits of a multi-stage Dockerfile?
- Smaller image sizes are attained by selective inclusion of content.
- Smaller surface area open to intentional or accidental compromise.
- Logical separation of build steps according to purpose.
- Easier and more reliable maintenance of Dockerfile instructions.