Managing Containers
Theory Questions
1. Identify Linux features that containers depend on.
- Command(s):
podman info
* **Why:** Containers rely on core Linux kernel primitives to provide isolated application environments:
* **Namespaces:** Isolate system resources (PID, Mount, Network, IPC, UTS, User).
* **Control Groups (cgroups v2):** Resource metering, limiting, and throttling (CPU, Memory, I/O).
* **SELinux / AppArmor:** Mandatory Access Control (MAC) providing process and filesystem separation.
* **OverlayFS / Storage Drivers:** Copy-on-Write (CoW) layered filesystem drivers.
* **Gotchas:** Confusing containers with Virtual Machines (VMs). Containers share the host kernel and do not run hypervisors.
* **Verify:**
```bash
podman info | grep -i cgroup
2. Identify Red Hat’s enterprise container solution.
- Tool / Ecosystem:
Podman(Pod Manager),Buildah(image building),Skopeo(image inspection/copy), andRed Hat OpenShift(Kubernetes enterprise platform). - Why: Red Hat replaced daemon-based engines (Docker) with daemonless, modular, rootless-by-default container engines like Podman on RHEL.
- Gotchas: Looking for the
dockerdaemon service in RHEL 9/10; usepodmaninstead. - Verify:
podman --version
3. Detach from a running container without shutting it down.
- Key Combination:
Press
Ctrl + Pfollowed byCtrl + Q. - Why: Escapes an interactive container terminal session (
podman run -itorpodman attach) while leaving the underlying container process executing in the background. - Gotchas: Pressing
Ctrl + Cor typingexit, which terminates the main PID 1 process inside the container and stops it. - Verify:
podman ps
4. Run an application container in the background.
- Command(s):
podman run -d --name my-app [registry.access.redhat.com/ubi9/ubi](https://registry.access.redhat.com/ubi9/ubi)
- Why: The
-d(--detach) flag instructs Podman to run the container process asynchronously in the background and print the container ID. - Gotchas: Running containers in the background whose main process exits immediately (e.g., standard shell without a long-running process).
- Verify:
podman ps | grep my-app
5. Inspect container images that have not yet been pulled locally.
- Command(s):
skopeo inspect docker://[registry.access.redhat.com/ubi9/ubi](https://registry.access.redhat.com/ubi9/ubi)
- Why:
skopeo inspectqueries remote container registries directly to retrieve image manifest data, layer details, environment variables, and labels without downloading the image layers to local storage. - Gotchas: Attempting to run
podman inspecton remote images;podman inspectrequires the image to exist locally. - Verify:
skopeo inspect docker://[registry.access.redhat.com/ubi9/ubi](https://registry.access.redhat.com/ubi9/ubi) | grep -i "digest"
6. Get an overview of the registries currently in use.
- Command(s):
podman info --format '{{.Registries}}'
# OR
cat /etc/containers/registries.conf
- Why:
/etc/containers/registries.confdefines search locations, mirrors, and blocked registries evaluated when pulling unqualified image names. - Gotchas: Editing
/etc/containers/registries.conffor user-specific search overrides without checking local drop-in path~/.config/containers/registries.conf. - Verify:
podman info | grep -A 5 "registries"
7. Determine whether a container requires environment variables.
- Command(s):
podman inspect <image_name> --format '{{.Config.Env}}'
# OR
skopeo inspect docker://<registry_path> | grep -i "env"
- Why: Inspects container metadata instructions (
ENVdirectives) defined during image build time, which outline required or configurable runtime variables (such as database passwords or usernames). - Gotchas: Starting complex containers (like MariaDB or MySQL) without mandatory environment variables, causing the container process to crash immediately upon launch.
- Verify:
podman inspect mariadb | grep -i "ENV"
8. Set the correct SELinux context type on host directories used as bind mounts.
- Command(s):
semanage fcontext -a -t container_file_t "/opt/data(/.*)?"
restorecon -Rv /opt/data
- Why: Host directories mounted into containers must be labeled with the
container_file_tcontext type so confined container process domains (container_t) can read and write to them. - Gotchas: Using temporary
chconcommands instead ofsemanage fcontext, which causes permissions to revert upon a filesystemrestoreconor reboot. - Verify:
ls -Zd /opt/data
9. Automatically set the correct SELinux context when bind-mounting a directory into a container.
- Command(s):
# Private/exclusive bind-mount flag (:z):
podman run -v /host/dir:/container/dir:Z -d image_name
# Shared bind-mount flag (:z):
podman run -v /host/dir:/container/dir:z -d image_name
- Why: Appending
:Z(uppercase) relabels the host volume with a private unshared SELinux MCS label (container_file_twith unique MCS categories). Appending:z(lowercase) relabels the host volume with a shared SELinux label accessible by multiple containers. - Gotchas: Using
:Zon host directories shared between multiple containers; uppercase:Zlocks the folder exclusively to the single target container. - Verify:
ls -Zd /host/dir
10. Ensure a user-created container starts as a systemd service at system boot (not just on user login).
- Command(s):
loginctl enable-linger <username>
- Why: By default, user systemd instances and user-level background processes terminate when an unprivileged user logs out.
loginctl enable-lingerinitializes user systemd instances at host boot and keeps them running independently of active user login sessions. - Gotchas: Enabling systemd user units for containers without running
loginctl enable-linger. The container will stop as soon as the SSH session closes. - Verify:
loginctl show-user <username> | grep Linger
(Should output Linger=yes)
11. Identify the tool for working with container images without downloading them first.
- Command:
skopeo
- Why:
skopeois a command-line utility designed to inspect, copy, sync, and delete remote container images directly across registries without requiring local container engine daemons to pull layers first. - Gotchas: Using
podman pullto inspect an image when bandwidth or storage overhead is a concern. - Verify:
skopeo --version
12. List the three Linux features needed in container environments.
- Features:
- Namespaces (Process, Network, Mount, User, IPC, UTS isolation)
- Control Groups (cgroups) (Resource allocation and limits)
- SELinux / Security Profiles (Process separation and mandatory access control)
- Why: These kernel subsystems collectively enforce operational boundaries, resource caps, and security isolation for containerized processes.
- Gotchas: Thinking container virtualization relies on hypervisors or hardware virtualization instructions (VT-x/AMD-V).
- Verify:
podman info | grep -E "cgroup|security"
13. Identify the container engine on RHEL 9.
- Command:
podman
- Why:
Podmanis the default, native, daemonless container engine provided in RHEL 9 and 10 for managing OCI containers and pods. - Gotchas: Installing third-party Docker repositories on RHCSA exam systems. Exam tasks expect native
podmanusage. - Verify:
podman version
14. Identify the file defining the currently used registries.
- Path / Command:
/etc/containers/registries.conf
/etc/containers/registries.conf.d/*.conf
- Why: Specifies system-wide target container registries (such as
registry.access.redhat.com,docker.io,quay.io) and short-name alias mappings. - Gotchas: Specifying unqualified image names without configured search registries.
- Verify:
grep -A 10 "unqualified-search-registries" /etc/containers/registries.conf
15. Diagnose why podman ps does not show a container started with podman run ubuntu.
- Command(s):
podman ps -a
- Why: The default
ubuntucontainer image specifies/bin/bashas its entry command. Since no interactive terminal (-it) or background process was attached,/bin/bashreceives EOF and exits immediately. Standardpodman psdisplays only active running containers;-ashows all containers including exited ones. - Gotchas: Assuming container creation failed completely when it actually ran and exited cleanly.
- Verify:
podman ps -a | grep ubuntu
16. Start a rootless container that bind-mounts a directory in the user’s home.
- Command(s):
mkdir -p ~/data
podman run -d --name app1 -v ~/data:/app/data:Z [registry.access.redhat.com/ubi9/ubi](https://registry.access.redhat.com/ubi9/ubi) sleep infinity
- Why:
~/datapoints to host user storage,-vspecifies the volume mapping, and:Zautomatically applies the required private SELinux container context flags to the path. - Gotchas: Omitting
:Zor:zon SELinux-enforcing RHEL hosts, causing “Permission denied” errors inside the container process when accessing mounted data. - Verify:
podman exec -it app1 ls -ld /app/data
17. Find the default command a container image uses when started.
- Command(s):
podman inspect <image_name> --format '{{.Config.Cmd}}'
- Why: Inspects the target image payload to extract default
CmdorEntrypointarray parameters declared inside the image configuration file. - Gotchas: Overriding default commands unintentionally by appending trailing shell arguments to
podman run. - Verify:
podman inspect ubi9 --format '{{.Config.Cmd}}'
18. Start an Ubuntu-based container that prints /etc/os-release and then exits.
- Command(s):
podman run --rm docker.io/library/ubuntu cat /etc/os-release
- Why: Passing
cat /etc/os-releaseoverrides default container shell behavior, executing the target file read and printing to stdout.--rmautomatically removes the container filesystem container upon completion. - Gotchas: Leaving orphaned stopped containers behind by omitting
--rmon one-off execution tasks. - Verify: Check terminal output for
NAME="Ubuntu".
19. Run a podman nginx container with host port 82 forwarding to container port 80.
- Command(s):
podman run -d --name web-server -p 82:80 host.containers.internal/nginx
# OR using standard image:
podman run -d --name web-server -p 82:80 quay.io/nginx/nginx-unprivileged
- Why: Syntax
-p <host_port>:<container_port>maps incoming network traffic arriving on host port 82 into container socket port 80 using rootless netavark/slirp4netns port forwarding. - Gotchas: Reversing port numbers (e.g.,
-p 80:82). The host port always precedes the colon. Using host ports below 1024 as an unprivileged user without adjustingnet.ipv4.ip_unprivileged_port_start. - Verify:
curl http://localhost:82
20. Generate a systemd unit file for a named container.
- Command(s):
# Step 1: Create container with persistent name
podman run -d --name container-app ubi9 sleep infinity
# Step 2: Generate systemd unit file using Quadlet (RHEL 9 Recommended Standard):
mkdir -p ~/.config/containers/systemd/
cat << 'EOF' > ~/.config/containers/systemd/container-app.container
[Container]
ContainerName=container-app
Image=[registry.access.redhat.com/ubi9/ubi](https://registry.access.redhat.com/ubi9/ubi)
Exec=sleep infinity
[Service]
Restart=always
[Install]
WantedBy=default.target
EOF
# Step 3: Reload user systemd generator
systemctl --user daemon-reload
Alternative (Legacy Podman generate systemd approach):
mkdir -p ~/.config/systemd/user/
cd ~/.config/systemd/user/
podman generate systemd --name container-app --files --new
systemctl --user daemon-reload
- Why: Quadlet (
.containerfiles under~/.config/containers/systemd/) is Red Hat’s modern standard for automatically managing containers as native systemd units. Legacypodman generate systemdremains functional for backwards compatibility. - Gotchas: Forgetting to run
systemctl --user daemon-reloadafter generating or updating unit files. - Verify:
systemctl --user status container-app
Lab Tasks
Containers Practice
Task: Log in to Red Hat registries, download mariadb, run the container with specific port, password, database, and bind-mount specifications, and configure persistent user systemd startup.
- Command(s):
# Step 1: Log in to Red Hat container registry (Use Red Hat credentials or registry access token)
podman login registry.redhat.io
# Step 2: Create host directory for bind mount and apply persistent SELinux container context
mkdir -p /opt/mariadb
semanage fcontext -a -t container_file_t "/opt/mariadb(/.*)?"
restorecon -Rv /opt/mariadb
# Step 3: Pull mariadb image
podman pull registry.redhat.io/rhel9/mariadb-105
# Step 4: Run mariadb container with specified parameters
podman run -d --name mariadb-server \
-p 3206:3306 \
-e MYSQL_ROOT_PASSWORD="password" \
-e MYSQL_DATABASE="mydb" \
-v /opt/mariadb:/var/lib/mysql:Z \
registry.redhat.io/rhel9/mariadb-105
# Step 5: Configure user linger to allow user systemd startup at boot without active login
loginctl enable-linger $(whoami)
# Step 6: Create systemd user unit directory and generate Quadlet unit file
mkdir -p ~/.config/containers/systemd/
cat << 'EOF' > ~/.config/containers/systemd/mariadb-server.container
[Unit]
Description=MariaDB Container Service
[Container]
ContainerName=mariadb-server
Image=registry.redhat.io/rhel9/mariadb-105
Environment=MYSQL_ROOT_PASSWORD=password MYSQL_DATABASE=mydb
PublishPort=3206:3306
Volume=/opt/mariadb:/var/lib/mysql:Z
[Service]
Restart=always
[Install]
WantedBy=default.target
EOF
# Step 7: Reload user systemd daemon and enable service
systemctl --user daemon-reload
systemctl --user enable --now mariadb-server.container
-
Why:
-
podman login: Authenticates to Red Hat subscription registries (registry.redhat.io). -
semanage fcontext&:Z: Guarantees host folder/opt/mariadbreceives SELinuxcontainer_file_tlabels so the container process can write to host storage. -
-p 3206:3306: Binds host port3206to database port3306inside the container. -
-e MYSQL_ROOT_PASSWORD="password" -e MYSQL_DATABASE="mydb": Configures mandatory database initialization parameters. -
loginctl enable-linger: Ensures systemd launches the unprivileged user daemon and container services automatically upon host system boot. -
Quadlet (
.containerfile): Automatically converts container specifications into native user systemd units managed bysystemctl --user. -
Gotchas:
-
Omitting
loginctl enable-linger. Without linger enabled, user containers start only after the user logs in via SSH and shut down as soon as the user logs out. -
Omitting
:Zorsemanage fcontexton/opt/mariadb. SELinux will block MariaDB from initializing database tables, causing container crash loops. -
Passing
-p 3306:3206instead of-p 3206:3306. Host port must always be specified first. -
Verify Runtime Container:
# Check active container:
podman ps | grep mariadb-server
# Verify host port mapping:
nc -zv localhost 3206
# Verify database initialization inside container:
podman exec -it mariadb-server mysql -u root -ppassword -e "SHOW DATABASES;"
(Output must display mydb in the database list)
- Verify Reboot Persistence:
# Reboot system:
sudo reboot
# Post-reboot checks (Log in as unprivileged user):
loginctl show-user $(whoami) | grep Linger
systemctl --user status mariadb-server
podman ps | grep mariadb-server
(Confirms mariadb-server container launches automatically via systemd user unit upon system reboot)