Course: Network and System Administration

In Class Lab: 21

Name: Installing Docker and running containers

Objective: Install Docker on RHEL from Docker's own repository, manage the docker service, and use Docker without sudo. Run containers from images, publish their ports, look at the Docker network, work inside a running container, mount content from your server into a container, read container logs, and clean up.

In Lab 13 you installed Nginx on your development server. It runs as one service, with one version, one configuration and one web root. What if you need a second web server with a newer version, or ten copies for testing? A container packages a program with everything it needs, and runs it as a separate, isolated set of processes on your server. In this lab you run several Nginx web servers in containers, next to the one from Lab 13.

You already know most of the pieces. In Lab 20 you added the EPEL repository; Docker comes from a repository too. In Lab 18 you managed services with systemctl and saw that everything is a process; containers are processes too. In Lab 14 you forwarded a port to another port; Docker does the same thing to reach a container. Building your own images comes in a later lab.

Note: This lab is done on your development server, NOT your production server. Connect with SSH from Terminal or PuTTY and log in as your own user. Parts 5 and 8 use two SSH sessions at the same time.

How to use the hints

Some steps give you every command. Others give you a Goal and ask you to work out the commands yourself. If you get stuck, open the hint. Once you have an answer, or if you are really stuck, open the solution and compare it with your own.

Lab overview

  1. Part 1: Add the Docker repository and install Docker - Task 1
  2. Part 2: Start Docker and use it without sudo - Task 2
  3. Part 3: Your first container - Task 3
  4. Part 4: Run a web server in a container - Task 4
  5. Part 5: The Docker network - Task 5
  6. Part 6: Inside a container - Task 6
  7. Part 7: Mount content from your server - Task 7
  8. Part 8: Container logs - Task 8
  9. Part 9: Images, tags and cleaning up - Tasks 9 and 10
  10. Part 10: Save your notes in GitLab - Task 11

If you get stuck, check the Troubleshooting section at the end of this lab.

The examples in this lab use the username angus. Container IDs, names, IP addresses and version numbers on your server will be different.

Images and containers

Three words come up all through this lab:

Word What it is
Image A read-only package: a small Linux file system with a program and everything it needs. Like a template.
Container A running (or stopped) copy of an image. It gets its own writable layer on top of the image, its own processes, and its own network address. Many containers can use one image.
Registry A server that stores images, such as Docker Hub. Like a repository for dnf, but for images.

A container is not a virtual machine. Your development server is a VM: it has its own kernel, running on UTM or VirtualBox. Containers on your server all share your server's kernel. That is why they start in a second and use little memory.


Part 1: Add the Docker repository and install Docker

Add the repository

Docker isn't in Red Hat's repositories. Red Hat ships Podman, its own container tool. You install Docker from Docker's repository, the same way you added EPEL in Lab 20.

dnf config-manager adds a repository from a URL. It comes from the dnf-plugins-core package, which also gave you dnf download in Lab 20. Check the repositories before and after:

sudo dnf install -y dnf-plugins-core ls /etc/yum.repos.d/ sudo dnf config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo ls /etc/yum.repos.d/ dnf repolist
Command What it does
dnf config-manager --add-repo Downloads a .repo file and saves it in /etc/yum.repos.d/.
ls /etc/yum.repos.d/ There is a new file, docker-ce.repo, next to epel.repo and redhat.repo.
dnf repolist Lists docker-ce-stable as well as the Red Hat, CRB and EPEL repositories.

Note: Many guides on the web use yum-config-manager and the centos URL. On RHEL, use the rhel URL above. yum-config-manager is an older name for dnf config-manager.

Task 1: Look at the Docker repository

Goal: Find out which repositories docker-ce.repo describes, and which of them are enabled.

Hint
Solution
grep "^\[\|^enabled" /etc/yum.repos.d/docker-ce.repo
[docker-ce-stable] enabled=1 [docker-ce-stable-debuginfo] enabled=0 [docker-ce-stable-source] enabled=0 [docker-ce-test] enabled=0 ... [docker-ce-nightly] enabled=0 ...

Only docker-ce-stable is enabled. The test and nightly repositories hold versions that aren't finished, so they are off.

Going further: grep "^baseurl\|^gpgkey" /etc/yum.repos.d/docker-ce.repo shows where the packages come from, and the key dnf uses to check them.

Install Docker

Docker is several packages:

Package What it is
docker-ce The Docker engine: the dockerd service. CE means Community Edition.
docker-ce-cli The docker command you type.
containerd.io containerd, the service that really starts and stops containers for dockerd.
docker-buildx-plugin Builds images. You use it in a later lab.
docker-compose-plugin Runs several containers together. Not used in this course.

Install them without -y this time, so you can see each question:

sudo dnf install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

dnf asks twice. First, Is this ok [y/N]: to install the packages. Type y. Then it asks to import Docker's GPG key, the key it uses to check that the packages really come from Docker:

Importing GPG key 0x621E9F35: Userid : "Docker Release (CE rpm) <docker@docker.com>" Fingerprint: 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35 From : https://download.docker.com/linux/rhel/gpg Is this ok [y/N]:

Compare the fingerprint with the one in Docker's install guide for RHEL. If it matches, type y. This is the same idea as matching SSH key fingerprints in Lab 7.

Check what you installed, with rpm from Lab 20:

rpm -q docker-ce docker-ce-cli containerd.io dnf info docker-ce | grep "^Version\|^From repo\|^Repository"

Note: If dnf reports a problem with podman or runc, see the Troubleshooting section.


Part 2: Start Docker and use it without sudo

Start the service

Installing Docker doesn't start it. Enable and start it in one command, as in Lab 14, then check it, as in Lab 18:

sudo systemctl enable --now docker systemctl status docker systemctl is-active docker containerd

systemctl status shows active (running) and enabled, so Docker starts again after a reboot.

Now ask Docker for its version, as your own user:

docker version
Client: Docker Engine - Community Version: ... ... permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: ...

The Client part works: that is the docker command. The Server part fails. The docker command doesn't do the work itself. It sends each request to the dockerd service through a socket, a special file that programs use to talk to each other. Look at it:

ls -l /var/run/docker.sock
srw-rw----. 1 root docker 0 Nov 19 10:12 /var/run/docker.sock

Question: The permissions start with s. Is this the setuid bit from Lab 15? Who can read and write this file?

Answer

No. The first character of ls -l is the file type, not a permission: - is a file, d a directory, l a link, and s a socket. Setuid shows up in the owner's x spot, as in -rwsr-xr-x.

The permissions are rw-rw----: the owner root and the group docker can read and write, others get nothing. You aren't root and aren't in docker, so you are denied.

Join the docker group

sudo docker version would work, but typing sudo for every command gets old. Add yourself to the docker group with usermod -aG, from Labs 2 and 6:

sudo usermod -aG docker $USER id

id doesn't show docker yet. As you learned in Lab 6, group changes only apply after you log in again.

Warning: Anyone in the docker group can control Docker, and Docker runs as root. A member can start a container that mounts / from your server and change any file, so being in docker is as good as having sudo. Only add people you would trust with root, and only on servers you control.

Before you log out, install bash-completion. Docker's command line has many options, and the docker-ce-cli package includes completion for them. Completion is loaded when you log in:

sudo dnf install -y bash-completion rpm -ql docker-ce-cli | grep completion

rpm -ql, from Lab 20, lists the package's files. One is /usr/share/bash-completion/completions/docker.

Now log out, log back in, and check:

exit
id docker version

id lists docker, and docker version shows both Client and Server. Type docker with a space and press Tab twice: bash lists Docker's commands.

Task 2: How does systemd start Docker?

Goal: Find the command systemd runs to start Docker, and the other units the docker service needs. Then find out which unit creates /var/run/docker.sock, and where its owner, group and permissions are set.

Hint
Solution
systemctl cat docker systemctl list-unit-files | grep docker systemctl cat docker.socket

In docker.service:

[Unit] ... After=network-online.target docker.socket firewalld.service containerd.service time-set.target Wants=network-online.target containerd.service Requires=docker.socket [Service] ... ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Line It means
ExecStart= systemd runs dockerd, the Docker engine.
Wants= It also starts containerd.
Requires= It needs docker.socket. -H fd:// tells dockerd to use the socket that systemd hands it.
After= It starts after firewalld, because Docker adds its own firewall rules (Part 5).

In docker.socket:

[Socket] ListenStream=/run/docker.sock SocketMode=0660 SocketUser=root SocketGroup=docker

docker.socket creates the socket, so that is where root, docker and 660 (rw-rw----) come from. /var/run is a link to /run, so both paths are the same file.


Part 3: Your first container

Run the hello-world image. It prints a message and exits:

docker run hello-world

docker hello

Note: The screenshot was taken on an older server, so the prompt is different. On a Mac with Apple silicon, step 2 says (arm64v8) instead of (amd64).

Read the four steps in the message. You didn't have the image, so Docker pulled it from Docker Hub, made a container from it, and ran it. The container printed the message, and stopped.

Look at the image and the container:

docker image ls docker ps docker ps -a
REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest 1b44b5a3e06a 3 months ago 10.1kB CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8a5f2c1d9e47 hello-world "/hello" 40 seconds ago Exited (0) 39 seconds ago gifted_lovelace
Command What it does
docker image ls Lists the images on your server. The image is only about 10 kB.
docker ps Lists running containers. hello-world isn't running any more, so the list is empty.
docker ps -a Lists all containers, running and stopped. Exited (0) is the exit status from Lab 19: it worked.

You never gave the container a name, so Docker made one up from an adjective and a famous scientist or engineer, such as gifted_lovelace. A stopped container stays on disk until you remove it. Look at the details of the image with:

docker image inspect hello-world | less

Press q to quit.

Task 3: Remove the hello-world container

Goal: Remove the hello-world container, using its name. Check that the container is gone, but the image is still there. Then run hello-world once more, so that the container removes itself when it stops.

Hint
Solution
docker ps -a docker rm gifted_lovelace docker ps -a docker image ls docker run --rm hello-world docker ps -a

Use your own container's name. docker rm prints the name back. The image stays, so the second run doesn't pull it again. With --rm, no stopped container is left behind.


Part 4: Run a web server in a container

Pull the image

docker run pulls an image if you don't have it. You can also pull it first:

docker pull nginx docker image ls

The nginx image is much bigger than hello-world, because it holds a small Debian Linux system as well as Nginx.

Run it in the background with &

Start a container from it, and send it to the background with &, as in Lab 18. Press Enter after the command to get your prompt back:

docker run -p 8081:80 nginx &

-p 8081:80 publishes a port: connections to port 8081 on your server go to port 80 inside the container. The first number is always your server's port, and the second is the container's. This is the same idea as the port forwarding in Lab 14 and in VirtualBox.

Test it, and list the running containers:

curl -I http://localhost:8081 docker ps

The container's own messages, and an access log line for each curl, print onto your screen. The docker command is still running in your shell, and it shows you everything the container prints. Note the generated name in the NAMES column.

Bring it back to the foreground and stop it, then remove it:

fg

Press Ctrl+C, then:

docker ps docker ps -a docker rm <CONTAINER ID> docker ps -a

docker rm container

Note: The screenshot was taken on an older server. Newer Docker versions also show an IPv6 line, [::]:8081->80/tcp, in PORTS.

Run it detached

& works, but the container's output keeps landing on your screen, and the container is tied to your shell. Docker has its own way to run in the background, -d for detached. Give the container a name too:

docker run -d --name web1 -p 8082:80 nginx docker ps
Option What it does
-d Detached. Docker starts the container in the background, prints its full ID, and gives you your prompt back.
--name web1 Names the container web1, so you don't need the ID.
-p 8082:80 Your server's port 8082 goes to the container's port 80.

Now you have two Nginx web servers on one server: the one from Lab 13 on port 80, and web1 on port 8082. Compare them:

curl -I http://localhost curl -I http://localhost:8082
HTTP/1.1 200 OK Server: nginx/1.20.1 ... HTTP/1.1 200 OK Server: nginx/1.29.3 ...

Your version numbers may be different. The container runs a much newer Nginx than RHEL ships, and the two don't get in each other's way.

What is listening on port 8082? Use ss, from Lab 12:

sudo ss -tlnp | grep 8082

The process is docker-proxy. Docker started it to accept connections on 8082 and pass them to the container.

Tip: docker ps is wide and wraps in a small window. --format picks the columns: docker ps -a --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}".

Warning: Docker writes its own firewall rules for published ports. A port you publish with -p can be reached from other computers even though firewall-cmd --list-ports (Lab 14) doesn't show it. Only publish what you mean to share. To keep a port on the server only, publish it on localhost: -p 127.0.0.1:8082:80.

Task 4: Let Docker choose the port

Goal: Start a second detached container named web2, and let Docker choose your server's port with -P (capital P). Find the port it chose, and get the headers with curl -I. Then stop web1, and show that web2 is still running and web1 still exists.

Hint
Solution
docker run -d --name web2 -P nginx docker ps docker port web2 curl -I http://localhost:32768 docker stop web1 docker ps docker ps -a
80/tcp -> 0.0.0.0:32768 80/tcp -> [::]:32768

Use the port from your own output. docker ps only shows web2; docker ps -a shows web1 as Exited.

docker stop

Note: This screenshot is from an older server and an older Nginx, and it also shows a stopped third container. The steps are the same.


Part 5: The Docker network

The docker0 bridge

Start web1 again. A stopped container keeps its name, its ports and its files, so docker start is all you need:

docker start web1 docker ps

When Docker started, it made a new network interface, docker0. It is a bridge: a virtual switch that the containers plug into. Look at it with the commands from Labs 9 and 12:

ip -br address ip address show docker0 ip route
lo UNKNOWN 127.0.0.1/8 ::1/128 enp0s1 UP 192.168.64.7/24 ... docker0 UP 172.17.0.1/16 ... veth1a2b3c4@if2 UP ... veth5d6e7f8@if2 UP ... default via 192.168.64.1 dev enp0s1 proto static metric 100 172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 192.168.64.0/24 dev enp0s1 proto kernel scope link src 192.168.64.7 metric 100
You see It means
docker0 ... 172.17.0.1/16 Your server's address on the Docker network. It is the containers' gateway.
veth...@if2 One virtual cable per running container, from the container to docker0.
172.17.0.0/16 dev docker0 A connected route, as in Lab 12: your server reaches containers directly, without a gateway.

Question: What size is the Docker network: /8, /16 or /24? Is it a private or a public range?

Answer

/16: the first two numbers, 172.17, are the network, so there is room for about 65,000 addresses. It is inside 172.16.0.0/12, one of the private ranges from Lab 12, so it never appears on the internet.

Docker can make several networks. List them:

docker network ls

bridge is the default network, the one on docker0. Every container you started is on it.

Docker also put docker0 in its own firewall zone. Check with the command from Lab 14:

sudo firewall-cmd --get-active-zones
docker interfaces: docker0 public interfaces: enp0s1

Task 5: Talk to a container directly

Goal: Find the IP address of web2 on the Docker network. Get the headers with curl -I, straight to that address, without going through a published port. Which port do you use, and why?

Hint
Solution
docker inspect web2 | grep IPAddress curl -I http://172.17.0.3

Use the address from your own output. You use port 80, the port Nginx listens on inside the container. The random port from Task 4 only exists on your server, where docker-proxy and Docker's firewall rules forward it to 172.17.0.3:80.

Another way: --format prints only the address: docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" web2.

Going further: See the forwarding rule Docker added, like the one you made in Lab 14, Part 6: sudo nft list ruleset | grep 8082. It shows a dnat to rule that sends port 8082 to web1's address, port 80.

Watch the traffic with bmon

In Lab 20 you installed bmon. Use it to watch traffic on docker0. Open a second SSH session to your development server, and place it next to the first. In the second session:

bmon -p docker0

In the first session, send 20 requests to web1, one a second:

for i in {1..20}; do curl -s http://localhost:8082 > /dev/null; echo "request $i"; sleep 1; done
Part What it does
for i in {1..20}; do ...; done Runs the commands between do and done 20 times, with $i set to 1, 2, 3 ... 20. You learn loops in Lab 22.
curl -s ... > /dev/null Fetches the page silently and throws it away (Lab 5).
sleep 1 Waits a second between requests.

The RX and TX numbers for docker0 in bmon go up while the loop runs, and drop back when it ends. Press q to quit bmon. Keep the second session open for Part 8.


Part 6: Inside a container

Open a shell in a running container

docker exec runs a command inside a running container. With -it, you get a shell:

docker exec -it web1 bash
Option What it does
-i Interactive: keeps the input open, so you can type.
-t Gives you a terminal, so you get a prompt.

The prompt changes to something like root@a3c9e1f04b72:/#. Look around:

whoami hostname cat /etc/os-release uname -r ls /usr/share/nginx/html exit
Command Inside the container
whoami root, the container's root user.
hostname The container's short ID.
cat /etc/os-release Debian, not Red Hat. The image has its own files and programs.
uname -r The same kernel version as your server.
ls /usr/share/nginx/html The container's web root, with its own index.html.

exit leaves the shell, but the container keeps running. Compare with your server:

cat /etc/redhat-release uname -r docker ps

Same kernel, different Linux. A container brings its own files, but uses your server's kernel. That is the difference from a VM.

Containers are just processes on your server. Look for the Nginx master processes, as in Lab 18:

ps -ef | grep "nginx: master"

There are three: one for the Lab 13 server, and one each for web1 and web2. The container ones are children of a containerd-shim process.

A throwaway container

docker run -it starts a new container with a shell instead of Nginx. Add --rm, from Task 3, so nothing is left behind:

docker run -it --rm nginx bash ls / exit docker ps -a

No new container is listed.

Task 6: Where do changes go?

Goal: From inside web1, replace the container's index.html with a line of your own, and check it with curl from your server. Then stop and remove web1, and start a new web1 with the same command as in Part 4. Is your page still there?

Hint
Solution
docker exec -it web1 bash echo "<h1>Changed inside web1</h1>" > /usr/share/nginx/html/index.html exit curl http://localhost:8082 docker stop web1 docker rm web1 docker run -d --name web1 -p 8082:80 nginx curl -s http://localhost:8082 | grep "<h1>"

The new web1 shows Welcome to nginx! again. Each container writes its changes to its own writable layer on top of the image. The image never changes, so a new container starts clean, and the old layer was deleted with docker rm. To keep files, you keep them outside the container, in Part 7.

Going further: Before removing the old web1, docker diff web1 lists the files the container changed: C changed, A added, D deleted.


Part 7: Mount content from your server

A read-only mount

-v mounts a directory from your server into a container. The container sees the server's files, and they stay on the server when the container is removed. Start a container that serves the Lab 13 web root, read-only:

docker run -d --name mydevweb1 -v /usr/share/nginx/html:/usr/share/nginx/html:ro -p 8083:80 nginx curl http://localhost:8083 curl http://localhost:8083/about.html
Part What it does
-v Mounts a directory, as SERVER-PATH:CONTAINER-PATH:OPTIONS. Both paths must be absolute.
/usr/share/nginx/html (first) The directory on your server: the Lab 13 web root.
/usr/share/nginx/html (second) Where it appears in the container. It hides the image's own files there.
:ro Read-only: the container can't change the files.

Port 8083 shows your home page from Lab 13, served by the container's Nginx. Change a file on the server, with sudo tee from Lab 5:

echo "<p>Updated from the server</p>" | sudo tee -a /usr/share/nginx/html/about.html curl http://localhost:8083/about.html curl http://localhost/about.html

Both web servers show the new line at once. They read the same files. Now try to change a file from inside the container:

docker exec mydevweb1 touch /usr/share/nginx/html/test.html
touch: cannot touch '/usr/share/nginx/html/test.html': Read-only file system

Without -it, docker exec runs one command and returns.

Common Pitfall: If the server path doesn't exist, Docker creates it as an empty directory owned by root, and the container shows 403 Forbidden (Lab 13). Check the path with ls -ld before you run the container.

Note: Docker on RHEL doesn't apply SELinux labels to containers by default, so these mounts work without changes. You may see guides that add :z or :Z to -v. Those relabel the files on your server (Lab 16). Never use them on a system directory such as /usr/share/nginx/html, or the Lab 13 server loses access.

Task 7: Serve your own site

Goal: Make a directory ~/inclass/lab-21/site with an index.html of your own. Serve it from a container named mysite on port 8084, read-write this time. Change the page on your server and check it. Then, from inside the container, create inside.html in the web root. Who owns it on your server? Remove the container. Are the files still there?

Hint
Solution
mkdir -p ~/inclass/lab-21/site echo "<h1>Hello from $USER's container</h1>" > ~/inclass/lab-21/site/index.html docker run -d --name mysite -v $HOME/inclass/lab-21/site:/usr/share/nginx/html -p 8084:80 nginx curl http://localhost:8084 echo "<p>Edited on the server</p>" >> ~/inclass/lab-21/site/index.html curl http://localhost:8084 docker exec mysite sh -c 'echo "<p>Made inside the container</p>" > /usr/share/nginx/html/inside.html' ls -l ~/inclass/lab-21/site curl http://localhost:8084/inside.html docker stop mysite docker rm mysite ls -l ~/inclass/lab-21/site

inside.html is owned by root on your server. Root in the container is root on the server, which is why the docker group is as good as sudo. Both files survive docker rm, because they live on your server.

You can still delete inside.html without sudo: rm asks about a write-protected file, but deleting is a permission of the directory, which you own (Lab 15).


Part 8: Container logs

Nginx in a container doesn't write log files. It prints its access log to standard output and its error log to standard error (Lab 5), and Docker keeps them for you. Look at how the image sets this up:

docker exec web1 ls -l /var/log/nginx
lrwxrwxrwx 1 root root 11 ... access.log -> /dev/stdout lrwxrwxrwx 1 root root 11 ... error.log -> /dev/stderr

Read web1's logs:

docker logs web1 docker logs --tail 5 web1

Now watch them live. In the first session:

docker logs -f web1

-f follows the log, like tail -f in Lab 18. The session waits for new lines. In the second session, send some requests:

for i in {1..10}; do curl -s -I http://localhost:8082 > /dev/null; echo "request $i"; sleep 1; done

Each request shows up in the first session as an access log line, with the source address 172.17.0.1: your server, on docker0. Press Ctrl+C in the first session. That stops following, not the container:

docker ps

Note: Some guides use docker attach to watch a container. It connects your terminal to the container's main process, so Ctrl+C stops the container itself. Use docker logs -f to watch.

Task 8: Find a 404

Goal: Request a page that doesn't exist from web1, such as /missing.html. Then use docker logs and grep to show only the lines about missing.html: both the access log line with 404 and the error log line.

Hint
Solution
curl -I http://localhost:8082/missing.html docker logs web1 | grep missing docker logs web1 2>&1 | grep missing

The first grep only filters standard output. The error log lines, on standard error, skip the pipe and print to your screen unfiltered. 2>&1 sends standard error into the pipe too, so grep sees both:

172.17.0.1 - - [19/Nov/2026:18:42:10 +0000] "HEAD /missing.html HTTP/1.1" 404 0 "-" "curl/7.76.1" "-" 2026/11/19 18:42:10 [error] 29#29: *12 open() "/usr/share/nginx/html/missing.html" failed (2: No such file or directory), ...

The times are in UTC: the container doesn't use your server's time zone from Lab 18.


Part 9: Images, tags and cleaning up

Find images

Docker Hub holds many thousands of images. Search it from the command line:

docker search nginx --limit 5 docker search debian --limit 5
Column It means
NAME The image name. Names with a /, such as bitnami/nginx, belong to a person or company.
STARS How many people bookmarked it.
OFFICIAL [OK] for official images, looked after by Docker and the software's makers. Prefer these.

Docker Hub isn't the only registry:

Registry Run by
Docker Hub Docker. The default for docker pull.
Quay.io Red Hat.
registry.access.redhat.com Red Hat, for RHEL-based images such as ubi9.
Amazon ECR AWS, for your own private images.

Note: Anyone can put an image on Docker Hub. An image you run is software you install, as with EPEL in Lab 20. Use official images, or images from people you trust.

Tags

Every image has a tag, after a :. When you leave it out, Docker uses latest, so nginx means nginx:latest. Many images come in several flavors. Look at the TAG column:

docker image ls

Task 9: A smaller nginx

Goal: Pull the alpine tag of nginx. Compare its size with nginx:latest. Run it as a throwaway, detached container on port 8085, and check its Server header. Which Linux is inside it?

Hint
Solution
docker pull nginx:alpine docker image ls nginx docker run -d --rm --name tiny -p 8085:80 nginx:alpine curl -I http://localhost:8085 docker exec tiny cat /etc/os-release docker stop tiny docker ps -a

The alpine image is several times smaller. It is built on Alpine Linux, a very small Linux made for containers, and runs the same Nginx version. With --rm, docker stop also removes tiny. You use Alpine again when you build your own images in a later lab.

Clean up with command substitution

Stopping and removing containers one at a time is fine for two or three, but not for fifty. -q prints only the IDs:

docker ps -q docker ps -a -q

Put that list straight into another command with $( ), the command substitution from Lab 7:

docker stop $(docker ps -q) docker ps docker ps -a
Command What it does
$(docker ps -q) Runs first, and is replaced by the IDs of the running containers.
docker stop $(docker ps -q) Stops all of them.

Warning: $( ) with docker ps -a -q takes in every container on the server, not only yours from today. On a shared server, check the list before you stop or remove anything.

Task 10: Clean up

Goal: Remove all your stopped containers with one command. Then remove the hello-world and nginx:alpine images, and keep nginx:latest for later labs. Finish with no containers at all, and only the nginx image.

Hint
Solution
docker rm $(docker ps -a -q) docker ps -a docker image rm hello-world nginx:alpine docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest ...

Another way: docker container prune removes every stopped container, after asking. docker image prune -a removes every image no container uses. Read the question before you answer y.

The files on your server stay: the Lab 13 web root, with the line you added to about.html, and ~/inclass/lab-21/site.


Part 10: Save your notes in GitLab

As in Labs 14 to 20, keep your own notes in your GitLab repository. Start the file:

cd ~/git/gitlab/itfdn-140-inclass-$USER git pull echo "# Docker and containers ## Basics An image is a template; a container is a copy of it with its own writable layer. \`docker run -d --name NAME -p SERVER:CONTAINER IMAGE\`. " >> docker-tips.md cat docker-tips.md

Goal: Add at least three more tips or commands to docker-tips.md, for example about the docker group, docker exec, -v, or docker logs. Add a link to the file at the end of README.md. Commit both files in one commit, and push. In GitLab, click the link in your README and check that it opens your notes.

Hint
Solution
vim docker-tips.md

For example:

## Access `sudo usermod -aG docker $USER`, then log in again. The docker group is as good as root. ## Working with containers `docker ps -a` lists stopped containers too. `docker exec -it NAME bash` opens a shell. Changes inside are lost with `docker rm`. `-v /server/path:/container/path:ro` mounts files from the server. ## Logs and cleanup `docker logs -f NAME`; use `2>&1` before grep. `docker rm $(docker ps -a -q)` removes all containers.

Add the link, then check, commit and push:

echo "" >> README.md echo "[Docker and containers](docker-tips.md)" >> README.md git status git diff git add README.md docker-tips.md git commit -m "docs: add docker tips and link from README" git push git log --oneline

Lab summary

You have now:

Questions to consider

Discuss these with others in class:

  1. What is the difference between an image and a container? How many containers can use one image?
  2. Why is adding a user to the docker group about as risky as giving them sudo?
  3. A container and a VM can both run Nginx. What does each one share with the server it runs on, and what does it bring for itself?
  4. Your Lab 13 Nginx and three container Nginx servers ran on one server. Why didn't they fight over port 80?
  5. Why is it a bad idea to keep important data only inside a container? Where should it go instead?
  6. Why might a port published with -p be reachable from your laptop even though firewalld doesn't list it? When would you use -p 127.0.0.1:PORT:80?
  7. What should you check before you run an image you found with docker search?

Troubleshooting

Q. dnf config-manager says "No such command: config-manager".

A. The command comes from the dnf-plugins-core package. Run sudo dnf install -y dnf-plugins-core.

Q. dnf install of Docker fails with a conflict about podman, buildah or runc.

A. Red Hat's container tools are installed, and containerd.io replaces runc. Add --allowerasing to the install command, which lets dnf remove the conflicting packages: sudo dnf install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin --allowerasing.

Q. dnf says the GPG key fingerprint doesn't match, or the key import fails.

A. Don't install. Check the URL in /etc/yum.repos.d/docker-ce.repo is download.docker.com/linux/rhel, and compare the key again with Docker's install guide.

Q. permission denied while trying to connect to the Docker daemon socket.

A. Check id for the docker group. If it isn't there, run sudo usermod -aG docker $USER. If it is in /etc/group but not in id, log out and back in (Lab 6). Check the socket with ls -l /var/run/docker.sock: it should be root docker. If it isn't, restart Docker: sudo systemctl restart docker.socket docker.

Q. Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

A. Docker isn't running. Check systemctl status docker, and start it with sudo systemctl enable --now docker. Read the reason with sudo journalctl -u docker | tail.

Q. toomanyrequests: You have reached your pull rate limit.

A. Docker Hub limits how many images can be pulled from one internet address without logging in. Wait an hour, or make a free Docker Hub account and run docker login.

Q. Bind for 0.0.0.0:8082 failed: port is already allocated.

A. Another container, or another program, already uses that server port. Check with docker ps and sudo ss -tlnp | grep 8082, then stop it or pick a different port.

Q. Conflict. The container name "/web1" is already in use.

A. A stopped container has that name. docker ps -a shows it. Remove it with docker rm web1, or start it again with docker start web1.

Q. You cannot remove a running container.

A. Stop it first with docker stop. docker rm -f stops and removes in one step.

Q. unable to remove repository reference ... container ... is using its referenced image.

A. A container, even a stopped one, still uses the image. Remove the container with docker rm, then the image.

Q. docker stop or docker rm says "requires at least 1 argument".

A. The $( ) list was empty: there were no containers to stop or remove. Check with docker ps -a.

Q. A container starts, but fails with "driver failed programming external connectivity" or an iptables error.

A. Docker's firewall rules are missing. This happens when firewalld is restarted or the rules are flushed (such as sudo nft flush ruleset in Lab 14) while Docker runs. Check systemctl status firewalld, then sudo systemctl restart docker, and start the container again.

failed container

Q. curl http://localhost:8083 shows 403 Forbidden.

A. The mounted directory is empty, or doesn't exist on the server, so Docker made an empty one. Check the path in -v with ls -ld. If the files are there, check SELinux with sudo ausearch -m avc -ts recent (Lab 16).

Q. curl to a published port says "Connection refused".

A. The container isn't running, or the port is wrong. Check docker ps and the PORTS column. If the container keeps stopping, docker logs NAME tells you why.

Q. My terminal is full of Nginx messages after docker run ... &.

A. The docker command is still connected to the container and prints what it prints. Press Enter to see your prompt. Use -d instead of &.

Q. bmon: command not found.

A. bmon was installed in Lab 20, Part 7. Run sudo dnf install -y bmon (EPEL must be enabled).

Q. git push is rejected.

A. Someone, perhaps you in the GitLab web page, changed the repository. Run git pull, then git push again, as in Lab 10.

If you are still stuck, post your question, and the commands and output you are seeing, in this week's Teams channel, or reach out by email for help.


Lab complete