4. Making Configuration Data Available to Containerised Applications Flashcards

1
Q

What is application configuration?

A

An app’s configuration is everything that is likely to vary between deploys (staging, production, dev environments, etc).

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

What are some examples of application configuration?

A
  1. Database server URL for an app’s backend.
  2. Port the app listens on for client requests.
  3. An application’s logging level for debugging purposes.
  4. The version of the app or API served by the environment.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the Twelve Factor App recommendations around configuration?

A
  1. Separate code and config — don’t define configuration as constants in the application’s source code.
  2. Store config in env vars — do use environment variables to define the application’s configuration.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the benefits of using environment variables rather than the app’s source code to define configuration?

A
  1. No leakage of sensitive information hard-coded in software applications.
  2. Straightforward onboarding of new environments to host software applications.
  3. No need to re-test software applications due to changes to the configuration.
  4. Configuration defined in environment variables is agnostic concerning languages and operating systems.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What Dockerfile instruction do we use to define environment variables?

A

The ENV instruction.

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

What can be said about the availability and persistence of environment variables defined in the Dockerfile for building images vs running containers?

A

Environment variables we define in the Dockerfile are available for builds. They can be used by other Dockerfile instructions.

These variables persist in the container. They are present in containers derived from the image build using the Dockerfile.

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

What is the recommended method of defining environment variables in the Dockerfile?

A

Using the ENV instruction followed by the variable name, an equal sign, and variable value. Then, if you need more than one variable, you can use the backslash to define each one in a new line.

ENV REDIS_HOST="redis_server" \
         REDIS_PORT="6379"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What kind of flexibility does Docker provide in regards to the point and time that environment variables are define?.

A

Environment variables can either be defined inside the Dockerfile or at the point of build. The latter meaning that the environment variables can be assigned when image builds are initiated.

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

How can we define an environment variable at the point of build?

A
  1. Arguments are provided on the command line when an image build is initiated with a Dockerfile.
  2. A corresponding variable is defined in the image’s Dockerfile using the ARG instruction.
  3. The value provided for the variable on the command line is substituted into the Dockerfile.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do we define in the Dockerfile a command line argument to be passed when initiating a build?

A

By using the ARG instruction followed by the argument name, an equals sign and the default value.

ARG VERSION="1.18.0"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do we specify the value of an argument defined in the Dockerfile when running an image build?

A

By using the --build-arg option followed by the argument name, an equals sign and the argument value.

docker build --build-arg VERSION="1.19.5"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What benefit does the fact that Docker ARG instruction allows us to re-assign values to variables in command-line allow us to do?

A

It allows us to build different image variants without altering the Dockerfile by choosing the value of the variable at the point in time of the build.

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

What facts about the ARG instruction should you take into consideration when deciding whether to use it to define a variable?

A
  1. It’s for values only known at build-time.
  2. Useful for variables required only for builds.
  3. Scoped from the line in which it is defined.
  4. Not visible when inspecting an image.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

In what process/location can we access a variable defined using the ARG instruction?

A

Only during the build process. Variables defined using the ARG instruction don’t trickle down to containers that are derived from the image.

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

What facts about the ENV instruction should you take into consideration when deciding whether to use it to define a variable?

A
  1. General-purpose mechanism for defining variables.
  2. Useful for persisting variables in images.
  3. ENV variables trump same-named ARG variables.
  4. Visible in image’s configuration.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What options do we have when defining application configuration in Docker?

A
  1. Authoring (Dockerfile) — ENV instruction.
  2. Building (Dockerfile and CLI Options) — ARG instruction, --build-arg.
  3. Running (CLI Options) — --env, --env-file.
17
Q

How can we set environment variables at runtime for Docker images/containers?

A

By using the -e or --env CLI option. If already defined in Dockerfile, the CLI definition overrides the set values.

18
Q

What happens if we at runtime define an environment variable that is already defined in Dockerfile?

A

The CLI definition overrides the pre-defined value.

19
Q

How can you provide Docker environment variables that are already defined in the environment?

A

By using the --env option and passing it the environment variable name only.

export REDIS_HOST=redis_server
docker run --rm --env REDIS_HOST redis
20
Q

How can you provide Docker with a whole file of environment variables at runtime?

A

By using the --env-file option and passing it the full path to the file.

redis.env file content:

REDIS_HOST=redis_server
REDIS_PORT=6379
~~~
```

the command:
~~~
docker run –rm –env-file $(pwd)/redis.env
~~~

21
Q

In Dockerfile, how do you set an environment variable based on an argument which doesn’t have a defined value but may or may not have a value defined at runtime?

A

By using the :- operator in a curly braces and quotes.

ARG NODE_ENV
ENV NODE_ENV "{NODE_ENV:-production}"
22
Q

How do you specify a script to be run rather than a command-line tool when a container is run?

A

By using the ENTRYPOINT instruction and passing it the path to the script inside the container — the script must have execution rights (chmod).

ENTRYPOINT ["/app/docker-entrypoint.sh"]