(RHCSA) Manage Containers

This is part of the RHCSA Exam Study Guide series

image

This is part of the independent and unofficial RHCSA Exam Study Guide series providing free 🤑 resources to prepare for the exam.

This post covers the objectives under the section:

“MANAGE CONTAINERS”

It contains two main parts:

  • Resources: with links to learn and practice for each objective.
  • Cheatsheet: containing some examples of commands and actions performed in each objective (when applied).

📚 Resources:

FIND AND RETRIEVE CONTAINER IMAGES FROM A REMOTE REGISTRY
INSPECT CONTAINER IMAGES
PERFORM CONTAINER MANAGEMENT USING COMMANDS SUCH AS PODMAN AND SKOPEO

(Covered in previous and next items)

PERFORM BASIC CONTAINER MANAGEMENT SUCH AS RUNNING, STARTING, STOPPING, AND LISTING RUNNING CONTAINERS
RUN A SERVICE INSIDE A CONTAINER
CONFIGURE A CONTAINER TO START AUTOMATICALLY AS A SYSTEMD SERVICE
ATTACH PERSISTENT STORAGE TO A CONTAINER

📑 Cheatsheet:

(Requires RHEL 8.3 or above, and container-tools module)

FIND AND RETRIEVE CONTAINER IMAGES FROM A REMOTE REGISTRY
  • Configure (user-level) container registries

    • create $HOME/.config/containers/registries.conffile to override the system-wide settings, and configure user level container regisitries.
    [registries.search]
    registries = ['registry.access.redhat.com', 'registry.redhat.io', 'docker.io']
      
    [registries.insecure]
    registries = []
      
    [registries.block]
    registries = []
    
  • podman search caddysearches the predefined container registries for container images containing the word ‘caddy’.

  • podman login quay.ioauthenticates to ‘quay.io’ container registry.

  • podman search quay.io/postgresql-10searches for the particular image on the specified registry.

  • podman search -f is-official --limit 3 --no-trunc alpinesearches for a particular image on all repositories, and displays only official images, maximum of 3, and full description.

  • podman pull registry.redhat.io/ubi8/ubipull specified image from specified registry.

INSPECT CONTAINER IMAGES
  • podman imageslists images downloaded to the system or created on the system.

  • podman inspect caddyinspect local image named ‘caddy’.

  • skopeo inspect docker://registry.redhat.io/ubi8/ubi-init | lessinspect remote image, using less pager.

PERFORM CONTAINER MANAGEMENT USING COMMANDS SUCH AS PODMAN AND SKOPEO
  • podman tag docker.io/library/mariadb localstableadd localstable tag to local image.
PERFORM BASIC CONTAINER MANAGEMENT SUCH AS RUNNING, STARTING, STOPPING, AND LISTING RUNNING CONTAINERS
  • podman exec myubi ls -laexecute ls -la command inside the myubi running container, and detach.

  • podman attach myubiattach to myubi running container.

  • podman stop myubi && podman rm myubistop and remove myubi container.

  • podman run --rm alpine ls /etcstart container from alpine image, run the command ls /etc, exit and remove the container.

  • podman run --name=myalpine -it alpine /bin/shstart container from alpine image, in interactive mode providing the /bin/sh shell, apply myalpine name to the container.

  • podman run -d mysqlstart container from mysql image and detach the session, container keeps running.

  • podman ps -ashow running and stopped containers.

  • podman start myubistart container named myubi detached.

  • podman start -a -i myubistart container named myubi attached and interactive mode.

RUN A SERVICE INSIDE A CONTAINER
  • itemdescription.
CONFIGURE A CONTAINER TO START AUTOMATICALLY AS A SYSTEMD SERVICE
  • podman create --name myhttpd docker.io/library/httpdcreate a container (do not start/run it).

  • podman generate systemd --name myhttpd > ~/.config/systemd/user/container-myhttpd.servicegenerate the container’s systemd unit file (create directory if needed). Inside the unit file, ExecStart run podman start command so, the container must exist in the host system.

  • Auto-generate a systemd unit file that auto-generate a container:

    • Create a new user (i.e.: containeruser) and open a terminal session with it.

    • podman create --name myhttpd2 httpdcreate a container (do not start/run it).

    • podman generate systemd --new --files --name myhttp2generate the container’s systemd unit file. Inside the unit file, ExecStart run podman run command so, the container will be created on start.

    • cp container-myhttpd2.service ~/.config/systemd/user/install it as user service.

  • Auto-starting containers using systemd

    • systemctl --user daemon-reload
    • systemctl --user --now enable container-myhttpd.service
    • loginctl enable-linger && loginctl show-user containerusermake the service autostart without the need for containeruser to log in.
    • verify with:
      • systemctl --user status container-myhttpd.service
      • systemctl --user | grep container
      • podman ps -a
ATTACH PERSISTENT STORAGE TO A CONTAINER
  • podman run --name="log_test" -v /dev/log:/dev/log --rm ubi logger "LOG THIS"create container log_test, mount the host /dev/log directory inside the container, create log message from the container.
MORE EXAMPLES
  • podman infodisplay podman system information.

  • podman inspect --format='{{.Config.ExposedPorts}}' myalpineinspect local container named myalpine displaying only the selected item from the JSON object.

  • podman run -dp 8080:80 --name http-serv docker.io/library/httpdcreate a container from the image, name it as http-serv and expose it’s port n. 80 to port 8080 in the host system.

  • podman run -it -e HISTSIZE -e SECRET="Mysecret" --name ubi8-vars ubi8create container ubi8-vars from ubi8 image, in interactive mode, setting up the specified environment variables.

  • podman port http-servcheck the http-serv container port mapping status.

  • podman unshare ls -la mysharesshows myshares directory properties, UID, GID, etc, as it is viewed by a rootless container.

  • podman unshare chown 1000:1000 -R myshareschange user and group owner inside the ‘user namespace’ for myshares directory recursively.

  • podman run -u 1000 -it -v /home/user1/myshares:/mnt/persistent:Z myubi /bin/bashstart container from myubi image, in interactive mode (bash shell), as user 1000, mount myshares directory at /mnt/persistent with a private unshared label (SELinux).

    • <Ctrl> + p, <Ctrl> + qdetach from the container’s interactive mode and go back to the host shell (container keeps running).
  • podman volume create hostvolumecreate new volume.

  • podmand volume inspect hostvolumedisplay information about the volume.

  • mntPoint=$(podman volume inspect hostvolume --format {{.Mountpoint}})save the volume mountpoint to mntPoint varable for easier manipulation.

  • podman run -it -v hostvolume:/mnt/sharedvol myubi /bin/bashstart container form myubi image, interactive mode in a bash shell, mount the hostvolume created at /mnt/sharedvol directory.


🎉 Congratulations!

You achieved the end of the learning for the RHCSA Exam!

(practice, reinforce, excercise, etc, check notes at:)

Red Hat Certified System Administrator (RHCSA) Exam Study Guide



Footnotes:

  • Follow me on Twitter to get more posts like this and other quick tips in your feed.
  • If you have any doubts or tips about this article, I’d appreciate knowing and discussing it via email.
  • Do you have any other Linux tips? Would you like to publish that in this blog? Please send an email to all drops.
  • As English is not my native language, I apologize for the errors. Corrections are welcome.
  • Contact: contact [@] alldrops [.] info.

Read more on linux drops: