1. Using Volumes to Develop Applications in Containers Flashcards

1
Q

What can be said about the mutability of container images?

A

They are immutable — they don’t change.

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

How are container images defined?

A

They are defined declaratively using a text file called Dockerfile.

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

What does a Dockerfile contain?

A

A sequence of special instructions that inform Docker on how to create the container image.

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

What kind of details do Dockerfile instructions define?

A
  1. Which base image to use
  2. Which source files need to be copied from the host into the image
  3. The command that our container derived from the image will run
  4. And so on…
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What process do we need to get an image built with an application inside it in Docker?

A

Docker’s image build process.

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

What happens when an image build is invoked using Docker’s CLI?

A

Docker reads and acts on the instructions in the Dockerfile, and makes use of any build artefacts such as compiled binaries to assemble the image that embodies the software application or service we want the container to run.

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

What is a Docker image?

A

It’s a template for the container.

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

Where does Docker’s image build process fit into the software development cycle?

A

Just before we get to the point where we need to run the application, and after we compile it.

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

What is the drawback of complex image definitions and how can this be alleviated?

A

Complex image definitions can take a significant amount of time to build and this can severely impact the productivity of a software developer. Our best bet for alleviating this is to develop inside the container where our code is destined to run.

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

What are the three implications of developing apps inside a container?

A
  1. Source code is part of the container’s filesystem
  2. Can run application and tests using the CLI
  3. Changes don’t persist on container deletion (ephemeral by nature)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does Docker use to get around the ephemeral nature of data that’s located within a container’s file system?

A

Volumes.

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

What are Docker volumes?

A

A Docker volume is an area of persistent storage that is located outside of the container.

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

Where are Docker volumes located?

A

On the host that’s running Docker, on the network, or even cloud storage.

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

How does Docker use to work with volumes?

A

Volume plug-ins — a plug-in system for implementing storage solutions.

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

How does Docker provide access to data stored outside of the container?

A

It mounts the storage area into the container during the life of the container and unmounts the storage when it gets removed.

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

What are the three types of Docker volumes?

A
  1. Tmpfs Mount
  2. Named or Anonymous Volume
  3. Bind Mount
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are Tmpfs mounts?

A

Temporary areas of storage that mounts a storage location in the memory onto a destination in the container’s file system.

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

What is the primary use case of Tmpfs mounts?

A

Writing sensitive data to the container’s file system during its execution without ever touching a disk and becoming open to compromise.

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

What are named or anonymous volumes?

A

A designated area of storage that resides within a protected area under Docker’s control.

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

How can named or anonymous volumes be manipulated?

A

Using Docker CLI.

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

What do bind mounts allow us to do?

A

Mount an arbitrary directory on the host onto an arbitrary target location inside a container.

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

What can be said about the changes made inside a bind mount in terms of reflection?

A

Changes made in the bind mound from the container are reflected on the host and vice versa.

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

What is the explicit approach of creating a Docker volume?

A

Running the volume subcomand in the Docker CLI along with the verb create and specifying a name for the volume.

docker volume create code-volume

24
Q

What is the explicit approach of creating a Docker volume?

A

Running the volume subcomand in the Docker CLI along with the verb create and specifying a name for the volume.

docker volume create code-volume

25
Q

What is the explicit approach of creating a Docker volume?

A

Running the volume subcomand in the Docker CLI along with the verb create and specifying a name for the volume.

docker volume create code-volume

26
Q

What is the implicit approach of creating a Docker volume?

A

Providing a --volume option as part of the docker run command. The argument specifies the volume name and the mount to use inside the container.

docker run --volume code-volume:/app

27
Q

What happens when using the implicit approach to create a Docker volume if the volume already exists and what happens if it doesn’t?

A

It gets mounted if it exists and it gets created and mounted if it doesn’t.

28
Q

What is the implicit approach of creating a Docker volume?

A

Providing a --volume option as part of the docker run command. The argument specifies the volume name and the mount to use inside the container.

docker run --volume code-volume:/app

29
Q

What happens when using the implicit approach to create a Docker volume if the volume already exists and what happens if it doesn’t?

A

It gets mounted if it exists and it gets created and mounted if it doesn’t.

30
Q

What Docker CLI sub-command can be used to list, inspect, and remove volumes? Why can this be done?

A

The sub-command volume. This is because volumes are first-class citizens of Docker.

docker volume [ls/inspect/rm]

31
Q

What are the advantages of using named volumes?

A
  1. They are Docker API objects, which means we can manage them easily
  2. They are a part of Docker’s internal data store, which means they are isolated from nefarious or accidental exposure or removal.
  3. We know where they reside, so they’re easy to identify and backup.
  4. Better performance when using Docker Desktop.
32
Q

What are the disadvantages of using named volumes

A
  1. They are owned by the root user, which means you must run your container as the root user in order to write to the volume. (Bad for security, avoid.)
33
Q

What can be said about running a container as the root user?

A

It is generally considered a bad practice for security reasons and should be avoided if at all possible.

34
Q

What is a bind mount?

A

A location on the host mounted into a container.

35
Q

How do you issue a bind mount when running a container?

A

By using the --volume option to specify the absolute path to the file or directory on the host and also the absolute path in the container where the directory is to be mounted separated by a column.

docker run --volume /path/on/host:/path/in/container ...
36
Q

What can be said about bind mounts in comparison to named volumes?

A

While named volumes impose a big restriction on us, bind mounts give us greater flexibility for data persistence.

37
Q

What is the build context?

A

A location on the host that contains the application source and any other items that need to be embodied in the image, as well as a Dockerfile.

38
Q

How do you specify what image to use in the Dockerfile?

A

By using the FROM instruction.

FROM node:14
39
Q

How do you specify the working directory in the Dockerfile?

A

By using the WORKDIR instruction.

WORKDIR /app
40
Q

How do you instruct the contents of the Dockerfile’s current directory to be copied across to the working directory inside the container?

A

By using the COPY instruction and passing it two dots separated by spaces. The first one indicates the Dockerfile’s current directory and the second one specifies the working directory inside the container.

COPY . .
41
Q

How do you instruct commands to be run once the app source is copied across to the working directory inside a container in the Dockerfile?

A

By using the RUN instruction and giving it the commands to run, separated by &&.

RUN npm install && npm install -g nodemon
42
Q

How do you specify a port on which the containerised application listens for requests in the Dockerfile?

A

By using the EXPOSE instruction and passing it the port number.

EXPOSE 3000
43
Q

How do you specify the container’s default command (what gets executed) in the Dockerfile?

A

By using the CMD instruction and and passing it each word of the full command as an array.

CMD [ "node", "src/index.js" ]
44
Q

How do you build a Docker image?

A

By using the build subcommand and passing it the name of the app using the -t option as well as the path to the build context.

docker build -t myapp:1.0 .

ps. The dot (.) at the end specifies the current working directory.)

45
Q

What happens when we invoke an image build?

A

Docker steps through each Dockerfile instruction until it gets to the end of the sequence, and we have a new image built for the application.

46
Q

What are the two key concepts of handling dynamic changes in Docker?

A
  1. Watch for changes — edits to source code are automatically detected inside running container.
  2. Perform hot reload — process monitor performs a hot reload by restarting the application in the container.
47
Q

What are the three key outcomes of mounting our source conte into the working directory using bind mount?

A
  1. Changes made to the source located on the host are reflected in the container via the bind mount volume.
  2. The hot reload utility automatically detects any changes to the source files and restarts the server.
  3. The changes can be tested to check they have implemented desired behaviour.
48
Q

What is the issue with the file/folder permissions that needs handling when working with bind mounts?

A

The default user in the container is the root user, whose user ID and group ID don’t match the user developing the app. This mismatch can cause some unexpected behaviour.

eg.
If you create a file on the Docker host by using the touch command, you will se that the owner of this file is the local user.

However, when we run a container that uses a bind mount and use the touch command inside the container, the owner of the created file will be the container’s root user, which means that it’s owned by the root user on host. If you try to write to this file, you can’t. You don’t have the correct permissions.

49
Q

How can we fix the permissions issue raised by using a bind mount?

A

By creating a new user group and adding it a new user whose ID matches the local user in the docker image using Linux command groupadd and useradd.

ps. This can be done in the Dockerfile like so:

RUN groupadd -r --gid 1000 user \
    && useradd -r --uid 1000 -g user user

Having built an image from the docker file containing these lines, when we bind the host directory into the container, we also specify on the command line that the container must run as the user we’ve created.

docker run --volume $(pwd):/src --user user  
50
Q

What is the issue with the file/folder permissions that needs handling when working with bind mounts?

A

The default user in the container is the root user, whose user ID and group ID don’t match the user developing the app. This mismatch can cause some unexpected behaviour.

eg.
If you create a file on the Docker host by using the touch command, you will se that the owner of this file is the local user.

However, when we run a container that uses a bind mount and use the touch command inside the container, the owner of the created file will be the container’s root user, which means that it’s owned by the root user on host. If you try to write to this file, you can’t. You don’t have the correct permissions.

51
Q

How do you instruct Dockerfile to add a new system user group for the container image?

A

By using the RUN instruction to issue a Linux groupadd command with the --system option and specifying the group id using the --gid option, as well ass specifying the user group name.

RUN groupadd --system --gid 1000 usergroup
52
Q

How do you instruct Dockerfile to create a new system user add it to an existing user group for the container image?

A

By using the RUN instruction to issue a Linux useradd command with the --system option, specifying the user ID using the --uid option, and specifying the group ID or name using the --gid option, as well as specifying in the new user’s name.

RUN ... \
    && useradd --system --uid 1000 --gid usergroup user
53
Q

How do you define dynamic variables in a Dockerfile which you can use the Command-Line to overwrite?

A

By using the ARG instruction.

ARG UID=1000

ps. You can then use this variable elsewhere in the Dockerfile.

$UID

…and you can overwrite its value while running the docker build command by passing it in using the --build-arg option.

docker build --build-arg UID=1001 ...
54
Q

How do you publish a port on the container to a port on the host while using the docker run command?

A

By specifying the container’s port to be published and the host’s port for it to be published to using the --publish option.

docker run --publish 3000:3000 
55
Q

What option to the docker run command do we use to bind mount a volume?

56
Q

How do you specify what command to run in combination with the ENTRYPOINT instruction specified in Dockerfile while running a container image on the CLI?

A

By specifying the command after the container image name specification.

docker run ..... todo:1.0 dev
ps. If the Dockerfile looks like:

ENTRYPOINT [“npm”, “run”]
CMD [“prod”]

~~~
```

…by default, it will run npm run prod. When we pass it the dev argument as seen above, it will instead run npm run dev.