2. Separating Application Build and Execution with Multi-Stage Builds Flashcards

1
Q

What extra step do we need to specify in the Dockerfile when using compiled languages?

A

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”]
~~~
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the two main problems with using compiled languages to develop our app in a Docker container?

A
  1. Increased complexity — not trivial to code and hot reload with a bind mount.
  2. Larger image size — development tools (eg. SDKs) are captured inside the container image.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a common solution for accommodating compiled languages while maintaining the benefits of developing inside a Docker container?

A

Using the Builder Pattern.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the Builder Pattern?

A

A Docker development pattern that splits out the build step sequence from the run step sequence using separate Dockerfiles for each task.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you specify the name and the tag while building a Docker image using docker build?

A

By using the --tag option.

docker build --tag mini .
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you automatically remove the container if it already exists when running docker run?

A

By using the --rm option.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you specify which Dockerfile to build an image from while running docker build?

A

By using the --file option.

docker build --tag my-builder --file Dockerfile.build .
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a better solution for separating our build step than using separate Dockerfiles?

A

Using a multi-stage Dockerfile.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What makes a Dockerfile a multi-stage Dockerfile?

A

Using multiple FROM instructions to logically separate the different parts of a container image — each FROM instruction delineates a different stage.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does an image built by using a multi-stage Dockerfile contain?

A

Only the content of the final stage.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does Docker help us reference a build stage?

A

By numbering each stage starting from zero.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do we name stages of a multi-stage Dockerfile?

A

By using the AS keyword when defining a build stage using FROM.

FROM node:14 AS builder
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

In a Dockerfile build stage, how do you instruct to copy files from a preceding stage?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you specify which build stage to use when running docker build?

A

By using the --target option with the build stage name or number.

docker build --tag app-builder --target builder .
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the benefits of a multi-stage Dockerfile?

A
  1. Smaller image sizes are attained by selective inclusion of content.
  2. Smaller surface area open to intentional or accidental compromise.
  3. Logical separation of build steps according to purpose.
  4. Easier and more reliable maintenance of Dockerfile instructions.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the three principles that we can use as a good starting point for structuring multi-stage Dockerfiles?

A
  1. Stage functions — establish the purpose of the different stages.
  2. Shared content — identify any common stage content.
  3. Size matters — optimize for size, but maintain readability (balance is key).
17
Q

How do you enable BuildKit for Docker builds?

A

By defining the DOCKER_BUILDKIT environment variable and setting it to 1.

export DOCKER_BUILDKIT=1
18
Q

What is the advantage of using BuildKit?

A

BuildKit enables higher performance docker builds and caching possibility to decrease build times and increase productivity for free.