Domain 7System MaintenanceCORE

Managing Containers

Comprehensive guide to managing rootless containers with Podman in RHEL, covering image inspection, bind mounts, SELinux flags, and persistent systemd user units.

Domain: Deploy, configure, and maintain systems•Updated: 2026-08-30
#Podman#Containers#Systemd#SELinux#Registries#RHCSA#RHEL

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), and Red 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 docker daemon service in RHEL 9/10; use podman instead.
  • Verify:
podman --version

3. Detach from a running container without shutting it down.

  • Key Combination: Press Ctrl + P followed by Ctrl + Q.
  • Why: Escapes an interactive container terminal session (podman run -it or podman attach) while leaving the underlying container process executing in the background.
  • Gotchas: Pressing Ctrl + C or typing exit, 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 inspect queries 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 inspect on remote images; podman inspect requires 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.conf defines search locations, mirrors, and blocked registries evaluated when pulling unqualified image names.
  • Gotchas: Editing /etc/containers/registries.conf for 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 (ENV directives) 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_t context type so confined container process domains (container_t) can read and write to them.
  • Gotchas: Using temporary chcon commands instead of semanage fcontext, which causes permissions to revert upon a filesystem restorecon or 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_t with unique MCS categories). Appending :z (lowercase) relabels the host volume with a shared SELinux label accessible by multiple containers.
  • Gotchas: Using :Z on host directories shared between multiple containers; uppercase :Z locks 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-linger initializes 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: skopeo is 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 pull to 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:
  1. Namespaces (Process, Network, Mount, User, IPC, UTS isolation)
  2. Control Groups (cgroups) (Resource allocation and limits)
  3. 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: Podman is 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 podman usage.
  • 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 ubuntu container image specifies /bin/bash as its entry command. Since no interactive terminal (-it) or background process was attached, /bin/bash receives EOF and exits immediately. Standard podman ps displays only active running containers; -a shows 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: ~/data points to host user storage, -v specifies the volume mapping, and :Z automatically applies the required private SELinux container context flags to the path.
  • Gotchas: Omitting :Z or :z on 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 Cmd or Entrypoint array 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-release overrides default container shell behavior, executing the target file read and printing to stdout. --rm automatically removes the container filesystem container upon completion.
  • Gotchas: Leaving orphaned stopped containers behind by omitting --rm on 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 adjusting net.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 (.container files under ~/.config/containers/systemd/) is Red Hat’s modern standard for automatically managing containers as native systemd units. Legacy podman generate systemd remains functional for backwards compatibility.
  • Gotchas: Forgetting to run systemctl --user daemon-reload after 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/mariadb receives SELinux container_file_t labels so the container process can write to host storage.

  • -p 3206:3306: Binds host port 3206 to database port 3306 inside 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 (.container file): Automatically converts container specifications into native user systemd units managed by systemctl --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 :Z or semanage fcontext on /opt/mariadb. SELinux will block MariaDB from initializing database tables, causing container crash loops.

  • Passing -p 3306:3206 instead 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)