Docker Document
Docker Document
Docker
1
Study Guide | Docker Certified Associate
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
To grant a user permission to run Docker commands, add the user to the Docker group. The user will have access to
Docker after their next login.
We can test our Docker installation by running a simple container. This container should output some text, and then exit.
It's a good idea to verify the key fingerprint. This is an optional step, but highly recommended. We should receive an output
indicating that the key was found:
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
2. Install packages:
3. To provide a user with permission to run Docker commands, add the user to the Docker group. The user will have
access to Docker after their next login.
4. We can test our Docker installation by running a simple container. This container should output some text, and then
exit.
Currently, the default driver for CentOS and Ubuntu systems is overlay2 .
The devicemapper storage driver is sometimes used on CentOS/RedHat systems, especially in older Docker versions. We
Remember, add the flag --storage-driver <driver name> to the call to dockerd .
Note: This is the recommended method for setting the storage driver.
sudo vi /etc/docker/daemon.json
{
"storage-driver": "devicemapper"
}
1. After any changes are made to /etc/docker/daemon.json , remember to restart Docker. It is also a good idea to
check the status of Docker after restarting, as a malformed config file will cause Docker to encounter startup failure.
Use the following commands:
Running a Container
Documentation:
Docker Run
• IMAGE: Run a container using an image called hello-world . In this example, the tag is unspecified, so the latest tag
will automatically be used.
• COMMAND and ARGS: Run a command inside the container. This command runs a container using the busybox
image. Inside the container, plus it runs the command echo with the arguments hello world! .
• TAG: This command specifies a certain tag, running a container with tag 1.15.11 of the nginx image.
• -d: Runs the container in detached mode. The command immediately exits, and the container continues to run in the
background.
• --name NAME: Gives the container a specified name instead of the usual randomly-assigned name.
• --restart RESTART: Specifies when Docker should automatically restart the container. Valid
values include:
• on-failure: Only if the container fails (exits with a non-zero exit code).
always: Always restart the container whether it succeeds or fails. Also starts the container automatically upon
• daemon startup.
• unless-stopped: Always restart the container whether it succeeds or fails, and on daemon startup unless the
container is manually stopped.
• -p HOST_PORT:CONTAINER_PORT: Publish a container's port. This process is necessary to access a port on a running
container. The HOST_PORT is the port that listens on the host machine, and traffic to that port is mapped to the
CONTAINER_PORT on the container.
• --memory-reservation MEMORY: Set a soft limit on memory usage. This limit is used only if Docker detects
memory contention on the host.
Managing Containers
• docker container rm <container name or ID> : Delete a container (must be stopped first).
1. To upgrade the Docker engine, first we must stop the Docker service and install newer versions of docker-ce and
docker-ce-cli .
docker version
To set a new default driver, modify /etc/docker/daemon.json . The log-driver option sets the driver, and log-opts
can be used to provide driver-specific configuration.
For example:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3",
"labels": "production_status",
"env": "os,customer"
}
}
Remember to utilize sudo systemctl restart docker after making any changes to /etc/docker/daemon.json .
We can also override the default driver setting for individual containers using the --log-driver' and '--log-opt
flags with docker run .
Namespaces: A Linux related technology that isolates processes by partitioning the resources that are available to them.
Namespaces prevent processes from interfering with one another. Docker leverages namespaces to isolate resources for
containers.
• user namespaces: Requires special configuration. Allows container processes to run as root inside the container while
mapping that user to an unprivileged user on the host.
Control Groups (cgroups): Control groups limit processes to a specific set of resources. Docker uses cgroups to
enforce rules around resource usage by containers, such as limiting memory or CPU usage.
Layered File System: Images and containers use a layered file system. Each layer contains only the differences from the
previous layer.
example of a Dockerfile :
Dockerfile Directives:
• ENV : Sets environment variables that are visible in later build steps as well as during container runtime.
• RUN : Executes a command and commits the result to the image file system.
CMD : Sets the default command for containers, and this gets overridden if a command gets specified at container
• runtime.
• ENTRYPOINT : Sets the default executable for containers. This can still be overridden at container runtime, but
requires a special flag. When ENTRYPOINT and CMD are both used, ENTRYPOINT sets the default executable, and
CMD sets default arguments.
Directives:
• WORKDIR : Sets the working directory, both for subsequent build steps and for the container at runtime. We can use
WORKDIR multiple times. If the WORKDIR begins with a forward slash / , then it will set an absolute path. Otherwise,
it will set the working directory relative to the previous working directory.
• COPY : Copies files from the build host into the image file system.
• ADD : Copies files from the build host into the image file system. Unlike COPY , ADD can also extract an archive into the
image file system and add files from a remote URL.
• STOPSIGNAL : Sets a custom signal that will be used to stop the container process.
• HEALTHCHECK : Sets a command that will be used by the Docker daemon to check whether the container is healthy.
Multi-Stage Build: A build from a Dockerfile with more than one the FROM directive. It is used to selectively copy files into
final stage, keeping the resulting image as small as possible.
Managing Images
Documentation:
• docker image inspect IMAGE --format "GO_TEMPLATE" : Provide a Go template to retrieve specific data fields
about the image.
• docker image rm IMAGE : Delete an image. An image can only face deletion if no containers or other image tags
reference it.
• docker image rm -f IMAGE : Force deletion of an image, even if gets referenced by something else.
cd ~/
mkdir alpine-hello
cd alpine-hello
vi Dockerfile
FROM alpine:3.9.3
RUN echo "Hello, World!" > message.txt
CMD cat message.txt
4. Run a container from the image and export its file system to an archive:
5. Import the archive to a new image and check how many layers the new image has:
We can operate our own private registry for free using the registry image. Run
Authenticate against a registry. We can omit the REGISTRY_URL to authenticate with Docker Hub:
There are two ways to authenticate with a private registry that uses an untrusted or self-signed certificate.
▪ Insecure: Adds the registry to the insecure-registries list in daemon.json , or pass it to dockerd
with the --insecure-registry flag.
Service: A collection of one or more replica containers running the same image in a swarm.
Here are some common tasks and the commands that are associated with them:
4. Publish a port for the service. By default, the port will listen on all nodes in the cluster (workers and
managers). Requests can be routed to a container on any node, regardless of which node is accessed by the
client. The format will look like this:
Note: We can use templates to pass dynamic data to certain flags when creating a service, for instance this
example sets an environment variable containing the node hostname for each replica container.
docker service ls
3. To inspect a service:
4. To change a service:
5. There are two different ways to change the number of replicas for a service:
7. We can create global services. Instead of running a specific number of replicas, they run exactly one task on
each node in the cluster. The command appears as:
Do▪cker inspect provides a way to get detailed information about Docker objects. We can use the general form
docker inspect OBJECT or an object-type-specific form docker container inspect CONTAINER .
Use the --format flag with Docker inspect to retrieve specific data fields using a Go template:
Docker Compose
Documentation:
▪ Docker Compose Link
Docker Compose: A tool used to manage complex, multi-container applications running on a single host. To
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
redis:
image: redis:alpine
Run an application using a compose file from the directory where the file is located:
docker-compose up -d
docker-compose ps
Stop a Docker compose application from the directory where the compose file is located:
docker-compose down
We can define a stack using a Docker Compose file, and then run it in the swarm with:
Remember that we can redeploy the stack with the same compose file to make changes to it.
Node Labels
Documentation:
▪ Docker Node Update Link
▪ Placement Constraints Link
Node Label: Custom metadata about a node in the cluster. To
docker node ls
We can use node constraints to control which nodes a service's tasks will run on based upon node labels. Here
is an example:
To use various expressions for constraints, such as inequality, we can run, for instance:
--constraint node.labels.availability_zone!=east
Use --placement-pref to spread tasks evenly based on the value of a specific label:
Storage Models
Filesystem Storage:
Block Storage:
▪
• Stores data in blocks using special block storage devices.
• Used by devicemapper .
▪ Object Storage:
We can inspect containers and images to locate the actual location of their data files on disk.
▪ loop-lvm : This is the default mode, but it is recommended for testing only, not for production use.
▪ direct-lvm : A production-ready mode, which requires additional configuration and a special block
storage device.
Remember, this assumes that there is a block storage device called `/dev/xvdb`.
{
"storage-driver": "devicemapper",
"storage-opts": [
"dm.directlvm_device=/dev/xvdb",
"dm.thinp_percent=95",
"dm.thinp_metapercent=1",
"dm.thinp_autoextend_threshold=80",
"dm.thinp_autoextend_percent=20",
"dm.directlvm_device_force=true"
]
}
Docker Volumes
Documentation:
▪ Managing Data Storage Link
▪ Using Bind Mounts Link
▪ Using Volumes Link
▪ Bind Mount: Mounts a specific directory on the host to the container. It is useful for sharing
configuration files, plus other data between the container and host.
▪ Named Volume: Mounts a directory to the container, but Docker controls the location of the volume on disk
dynamically.
There are different syntaxes for adding bind mounts or volumes to containers:
-v syntax
A bind mount. The fact that the source begins with a forward slash / makes this a bind mount.
A named volume. The fact that the source is just a string means that this is a volume. If no volume exists with the
provided name, then it will be automatically created.
--mount syntax
A bind mount:
A named volume:
We can mount the same volume to multiple containers, allowing them to share data.
We can also create and manage volumes by themselves without running a container.
Image Cleanup
Documentation:
▪ Docker System DF Link
docker system df
docker system df -v
Once we have verified that the images are not being used by a container, we can use the following command to
delete all of them:
Networking
Docker Networking
Documentation:
▪ Docker Container Networks Link
Docker Container Networking Model (CNM): A conceptual model that describes the components and
concepts of Docker networking.
▪ Sandbox: An isolated unit containing all networking components associated with a single container.
▪ Network Driver: A pluggable driver that provides a specific implementation of the CNM.
Native Network Drivers: Network drivers that come shipped with Docker.
Host
This driver connects the container directly to the host's networking stack. It provides no isolation between
containers or between containers and the host.
Bridge
This driver uses virtual bridge interfaces to establish connections between containers running on the same host.
Overlay
This driver uses a routing mesh to connect containers across multiple Docker hosts, usually in a Docker
swarm.
MACVLAN
This driver connect containers directly to the host's network interfaces, but uses special configuration to
provide isolation.
None
This driver provides sandbox isolation, but it does not provide any implementation for networking between
containers or between containers and the host.
Bridge is the default driver, so any network that is created without specifying the driver will be a bridge
network.
By default, container and services on the same network can communicate with each other simply using their
container or service names. Docker provides DNS resolution on the network that allows this to work. We can
supply a network alias to provide an additional name by which a container or service is reached.
Here are some useful commands for when one must interact with Docker networks:
Publish a port on a container. The host port is the port that will listen on the host. Requests to that port on the
host will be forwarded to the CONTAINER_PORT inside the container.
-P , --publish-all : Publish all ports documented using EXPOSE for the image. These ports are all
published to random ports.
We can also publish ports for services. By default, the routing mesh causes the host port to listen on all
nodes in the cluster. Requests can be forwarded to any task, even if they are on another node.
We can publish service ports in host mode. This mode is much more restrictive. It does not use a routing mesh.
Requests to the host port on a host are forwarded to the task running on that same host. Therefore, in this mode,
we cannot have more than one task for the service per host, and hosts that are not running a task for the service
won't listen on the host port.
Network Troubleshooting
Documentation:
▪ Troubleshooting Container Networking Link
We can use the nicolaka/netshoot image to perform network troubleshooting. It comes packaged with a
variety of useful networking-related tools.
We can inject a container into another container's networking sandbox for troubleshooting purposes:
{
"dns": ["8.8.8.8"]
}