Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Part 4 · Operations and troubleshooting · Lesson 02 · 50 minutes

Run software in containers

After this lesson you can
  • Pull an image, run a container from it and read its logs, stop it and remove it
  • Keep data in a volume and reach a container by a published port or by a network name
  • Build an image from a Containerfile and say why a digest and a rootless container are safer
Lab files of this lesson (9)

The lab folder as it stands after this lesson. Unzip it in ~/krlab.

More

The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.

Scenario

The team wants to try a new web server, and nobody wants to install it on the shared host. "Run it in a container, show me that it answers, and show me that its data survives when you throw the container away." You use podman, a container tool that needs no administrator rights. At the end you also package your own small program as an image.

Recall

What does ss -tln show, and what does the address 0.0.0.0:8080 mean?

The TCP ports that listen for connections, with numbers and no names (lesson 1 of Part 3). 0.0.0.0:8080 means that the port listens on every IPv4 address of the machine, not only on the loopback.

Lesson 7 of Part 3 says that chroot is not a security boundary. Which two kernel features go further?

Namespaces, which give a process its own view of processes, network and mounts, and control groups, which limit the CPU and the memory that it may use. A container is a process with both.

The idea

A container is an ordinary process that the kernel isolates with namespaces and limits with control groups. It is not a small virtual machine: it has no kernel of its own. An image is the read-only package that a container starts from: the files of a small system and a default command. A registry is a server that stores images (Docker Hub is docker.io). A volume is a folder that lives outside the container, so that data stays when the container is deleted. A Containerfile is the text recipe that builds an image.

What podman run does
YouPodmanRegistryContainerpodman run nginx:alpinepull the image if it is absentlayers of the imagestart the process, isolatedport 8088 answersYouPodmanRegistryContainer12345
  1. You → Podman: podman run nginx:alpine
  2. Podman → Registry: pull the image if it is absent
  3. Registry → Podman: layers of the image
  4. Podman → Container: start the process, isolated
  5. Container → You: port 8088 answers

Rootless means that your own user runs the container, not root. The root user inside the container is mapped to your user outside, so a flaw that lets a process escape gives it your rights and not those of the administrator. That is the main reason to prefer podman rootless on a shared server.

A full image name has four parts:

A full image name
docker.iothe registrylibrarythe namespacealpinethe name:3.20the tag, a label that can movedocker.iothe registrylibrarythe namespacealpinethe name:3.20the tag, a label that can move

The owner of a tag can move it to a new image. A digest (@sha256:...) names exactly one image, and it cannot move.

Worked example

The output comes from a virtual machine with Ubuntu 24.04 and podman 4.9, so numbers and times differ on your machine. The long ids that podman prints for a new container are left out. Install podman first if it is missing: sudo apt install -y podman. Always write the full image name, so that no search order decides what you get.

1. Run a container once

podman run makes a container from an image and starts it. --rm deletes the container when it ends. The first run pulls the image from the registry.

mkdir -p ~/krlab/linux-operations && cd ~/krlab/linux-operations
podman run --rm docker.io/library/alpine:3.20 echo "hello from a container"
hello from a container

2. Run a web server in the background and reach it

Read the command part by part. A rootless container may publish only ports of 1024 and above. podman ps lists the running containers, and --format chooses what to print for each one.

A podman run command
podman runrun a container-din the background (detach)--name krlab-webgive it a name-p 8088:80host port 8088 to port 80docker.io/library/nginx:alpinethe imagepodman runrun a container-din the background (detach)--name krlab-webgive it a name-p 8088:80host port 8088 to port 80docker.io/library/nginx:alpinethe image
podman run -d --name krlab-web -p 8088:80 docker.io/library/nginx:alpine
sleep 2
podman ps --format "{{.Names}} {{.Image}} {{.Status}} {{.Ports}}"
curl -s -o /dev/null -w "%{http_code}\n" http://127.0.0.1:8088/
krlab-web docker.io/library/nginx:alpine Up 2 seconds 0.0.0.0:8088->80/tcp
200

3. Read the logs, run a command inside, stop and remove

podman logs NAME prints what the container wrote. podman exec NAME COMMAND runs a command inside a running container. stop ends it, ps -a lists stopped containers too, and rm deletes one.

podman logs krlab-web 2>&1 | tail -n 1
podman exec krlab-web id
podman stop krlab-web
podman ps -a --format "{{.Names}} {{.Status}}"
podman rm krlab-web
10.0.2.100 - - [21/Nov/2026:12:54:34 +0000] "GET / HTTP/1.1" 200 896 "-" "curl/8.5.0" "-"
uid=0(root) gid=0(root)
krlab-web
krlab-web Exited (0) Less than a second ago
krlab-web

The container said uid=0(root). Check what the host sees. -C sleep of ps selects processes by command name. The last command removes the container: -f forces the removal of a running container, and -t 0 skips the ten-second wait.

podman run -d --name krlab-sleep docker.io/library/alpine:3.20 sleep 300
ps -o user,comm -C sleep
podman rm -f -t 0 krlab-sleep
USER     COMMAND
you      sleep
krlab-sleep

Other sleep lines on your machine belong to other programs: look for the one that belongs to you. Inside, the process is root. On the host it belongs to you. That is the rootless mapping.

4. Keep data in a volume

podman volume create makes a named volume, and -v NAME:/path mounts it in a container. Write a file from one container and read it from another.

podman volume create krlab-data
podman run --rm -v krlab-data:/data docker.io/library/alpine:3.20 sh -c 'echo kept > /data/note.txt'
podman run --rm -v krlab-data:/data docker.io/library/alpine:3.20 cat /data/note.txt
krlab-data
kept

The first container is gone, and the data stayed. podman volume inspect krlab-data shows where the folder is on the host (under ~/.local/share/containers/storage/volumes/). A bind mount uses a folder of your own instead: -v "$PWD/site:/usr/share/nginx/html:ro", where :ro makes it read-only for the container. Edit a file in site and the container serves the new text at once.

5. Connect containers with a network

podman network create makes a private network. Containers on it find each other by container name. A container on --network none has only a loopback.

podman network create krlab-net
podman run -d --name krlab-api --network krlab-net docker.io/library/nginx:alpine
sleep 2
podman run --rm --network krlab-net docker.io/library/alpine:3.20 wget -qO- http://krlab-api/ | head -n 2
podman rm -f krlab-api && podman network rm krlab-net
krlab-net
<!DOCTYPE html>
<html>
krlab-api
krlab-net

The second container reached the first by its name, and nothing was published on the host.

6. Build an image from a Containerfile

Save the program as greet/greet.sh and the recipe as greet/Containerfile. FROM names the base image, RUN runs a command while the image is built, COPY puts a file in, USER sets the user of the container, ENTRYPOINT is the fixed command and CMD is its default argument.

greet/greet.sh

#!/bin/sh
echo "hello, ${1:-world}"

greet/Containerfile

FROM docker.io/library/alpine:3.20
RUN adduser -D -u 10001 greeter
COPY greet.sh /usr/local/bin/greet.sh
USER greeter
ENTRYPOINT ["/usr/local/bin/greet.sh"]
CMD ["world"]

podman build -t NAME:TAG FOLDER builds the image and -t gives it a name. --entrypoint replaces the fixed command for one run.

chmod +x greet/greet.sh
podman build -t krlab-greet:1 greet > /dev/null
podman run --rm krlab-greet:1
podman run --rm krlab-greet:1 Ada
podman run --rm --entrypoint id krlab-greet:1
hello, world
hello, Ada
uid=10001(greeter) gid=10001(greeter) groups=10001(greeter)

The program runs as an ordinary user inside the container, in addition to being rootless outside. Do this for every image that you build.

7. Pin an image by its digest

podman image inspect prints the facts of an image, and --format picks one field. The value changes when the owner publishes a new image under the same tag.

D=$(podman image inspect docker.io/library/alpine:3.20 --format '{{.Digest}}')
echo $D
podman run --rm docker.io/library/alpine@$D echo pinned
sha256:d9e853e87e55526f6b2917df91a2115c36dd7c696a35be12163d44e6e2a4b6bc
pinned

A build or a deployment that names the digest gets the same bytes every time, and an attacker who takes over the tag changes nothing for you.

Your turn

A colleague wrote a counter tool and its image recipe. Save both files. The tool adds its first argument to /data/log.txt and prints the number of lines in that file.

count/count.sh

#!/bin/sh
# count.sh - adds its first argument to /data/log.txt and prints the number of lines
echo "$1" >> /data/log.txt
wc -l < /data/log.txt

count/Containerfile

FROM docker.io/library/alpine:3.20
RUN adduser -D -u 10001 counter && mkdir /data && chown counter /data
COPY count.sh /usr/local/bin/count.sh
USER counter
ENTRYPOINT ["/usr/local/bin/count.sh"]

There are no steps here. Make the script executable and build the image krlab-count:1. Create the volume krlab-count-data (if you have run this task before, remove the volume first with podman volume rm krlab-count-data, or the counter starts at 6). Run the image three times with the words one, two and three, with the volume mounted at /data. Then run it once with the word solo and no volume. Last, print the file /data/log.txt from the volume with cat, by a container that replaces the entrypoint.

Clean up: remove the volumes and the images that you made (podman volume rm, podman rmi).

Check yourself

You delete a container and its data is gone. What did you miss, and what do you do next time?

The data lived in the writable layer of the container, which is deleted with it. Put data that must stay in a volume or a bind mount, and mount it at the path where the program writes.

Why is the tag "latest" a poor choice for a production deployment?

A tag can move. "latest" is a label, so the next pull may bring a different image, and two servers can run different code under one name. Use a version tag, and for strict control the digest.

A process is root inside a rootless container. Why is that less dangerous than root in a rootful container?

Root inside is mapped to your own user on the host. If the process escapes, it has only the rights of that user. In a rootful container the same escape gives root on the host.

Two containers on the same network cannot reach each other by name. What do you check first?

That both run on a network that you created (--network NAME): the default network may not give names. Then check the spelling of the container name, and that the target container runs.

Field notes

  • Keep images small and from a source you trust. Fewer packages mean fewer flaws to patch. Scan an image for known flaws before you run it in production, and rebuild it when the base image changes.
  • Never put a secret in a Containerfile or an environment variable that you commit. podman history shows every build step. Pass a secret at run time, for example with podman secret.
  • A published port is a firewall question as well (lesson 3 of Part 3). -p 127.0.0.1:8088:80 publishes the port on the loopback only.
  • Do not run a container as root unless it needs to, and add --read-only and --cap-drop=all to see what a service really needs.
  • A container is not a virtual machine. It shares the host kernel, so a kernel flaw affects every container on that host.