There is a new version of this tutorial available for Rocky Linux 8.4.

How to Install and Use Podman to run Containers On Rocky Linux 8

Podman is a free and open-source container platform built to develop, manage and deploy containers and pods on Linux environment. Redhat developed Podman in 2018. It is a containerization engine that works differently than Docker. Podman does not depend on a daemon to work, unlike Docker which uses Docker CLI and Docker daemon. Being dependent on daemon leads to a single point of failure.

Podman is designed according to OCI (Open Container Initiative) standards that allow Podman to interact directly with the kernel, containers and images. It is also more secure than Docker as it does not require root access. Podman can be used as a drop-in replacement for Docker since both are OCI-compliant.

This article will show you how to install Podman and use it to create and manage images and containers.

Prerequisites

  1. A Rocky Linux based server

  2. A non-sudo user with root privileges.

  3. Ensure that the server is updated.

    $ sudo dnf update
    

Install Podman

Podman is included in the container-tools module, along with Buildah and Skopeo. It is also available in the AppStream repository for Rocky Linux 8. We will be using the module method.

Install Podman using the dnf module command.

$ sudo dnf module install container-tools

Check the version of Podman to see if it is installed correctly.

$ podman --version
podman version 3.2.3

Search and Download Container Images

To search for the image of Nginx, use the following command.

$ podman search nginx

Podman Search Image Result

In the output, you will see the name of the registry the image is from and a description of the images.

To download the image, use one of the following commands.

$ podman pull docker.io/library/nginx

OR

$ podman pull nginx

You can view the downloaded images via the following command.

$ podman images
REPOSITORY               TAG         IMAGE ID      CREATED     SIZE
docker.io/library/nginx  latest      f8f4ffc8092c  3 days ago  138 MB

Run Containers

Use the following command to run a container using the Nginx image. We have named the container as webserver.

$ podman run -d --name webserver nginx 

We can use the same image to launch another container with a different name.

$ podman run -d --name webserver2 nginx

We can launch an unlimited number of containers using the same image.

List and Stop Containers

To list all the running containers, use the following command.

$ podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS             PORTS       NAMES
19b6668bc627  docker.io/library/nginx:latest  nginx -g daemon o...  31 seconds ago  Up 31 seconds ago              webserver
35a286ba5a55  docker.io/library/nginx:latest  nginx -g daemon o...  2 seconds ago   Up 3 seconds ago               webserver2

Stop the running container.

$ podman stop webserver
webserver

Verify if it has stopped.

$ podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED             STATUS                 PORTS       NAMES
35a286ba5a55  docker.io/library/nginx:latest  nginx -g daemon o...  About a minute ago  Up About a minute ago              webserver2

To list all the containers, including the stopped ones, you need to use -a flag.

$ podman ps -a
CONTAINER ID  IMAGE                           COMMAND               CREATED             STATUS                     PORTS       NAMES
19b6668bc627  docker.io/library/nginx:latest  nginx -g daemon o...  2 minutes ago       Exited (0) 35 seconds ago              webserver
35a286ba5a55  docker.io/library/nginx:latest  nginx -g daemon o...  About a minute ago  Up About a minute ago                  webserver2

Start a stopped Container

Use the following command to start a stopped container.

$ podman start webserver
webserver

Verify if it has started.

$ podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED        STATUS             PORTS       NAMES
19b6668bc627  docker.io/library/nginx:latest  nginx -g daemon o...  3 minutes ago  Up 16 seconds ago              webserver
35a286ba5a55  docker.io/library/nginx:latest  nginx -g daemon o...  2 minutes ago  Up 2 minutes ago               webserver2

Delete Container

You need to stop a container before deleting it.

$ podman stop webserver2

Delete the container.

$ podman rm webserver2

You can delete a running container by using the --force flag.

$ podman rm webserver2 --force
35a286ba5a553d5f88e3d9795780f893cfb58bf4a126c4912d1ec56b9d0e5a27

Kill Container

Stopping and Killing a Container are two different things that eventually achieve the same thing. The difference is that Stopping a container shuts it down gracefully while Killing a container is forcibly ending it, resulting in data loss.

Use the following command to kill the container.

$ podman kill -s 9 webserver2

The above command uses the SIGNAL 9 (SIGKILL) option to kill the container.

To kill all the containers, use --all or -a flag and to kill only the latest container, use the --latest or -l flag.

Delete Image

You can delete the images using the rmi command.

$ podman rmi registry.redhat.io/rhel8/rsyslog

You can delete multiple images by separating them using commas.

$ podman rmi registry.redhat.io/rhel8/rsyslog registry.redhat.io/ubi8/ubi

To remove all the images on your system, use the -a flag.

$ podman rmi -a

View Container Logs

To view the container logs, use the following command.

$ podman logs webserver
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
......

You can restrict the logs to the last 5 lines by using the --tail option.

$ podman logs --tail=5 webserver
2021/10/05 10:13:52 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
2021/10/05 10:13:52 [notice] 1#1: OS: Linux 4.18.0-305.19.1.el8_4.x86_64
2021/10/05 10:13:52 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 262144:262144
2021/10/05 10:13:52 [notice] 1#1: start worker processes
2021/10/05 10:13:52 [notice] 1#1: start worker process 23

By default, you won't get any timestamps on the logs. Use the -t flag to add timestamps to your logs.

$ podman logs -t webserver
2021-10-05T09:25:02.026967459Z /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
2021-10-05T09:25:02.026967459Z /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
2021-10-05T09:25:02.033956297Z /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
2021-10-05T09:25:02.043751152Z 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
2021-10-05T09:25:02.064561317Z 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
.....

Inspecting Containers

Inspecting a container will print the information about a container.

$ podman inspect webserver
[
    {
        "Id": "19b6668bc6278a66b3ffc98ae1515af25f5bebcd20bf26de803cae41c4485f59",
        "Created": "2021-10-05T09:25:01.784949744Z",
        "Path": "/docker-entrypoint.sh",
        "Args": [
            "nginx",
            "-g",
            "daemon off;"
        ],
        "State": {
            "OciVersion": "1.0.2-dev",
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 4423,
....

Default command will print out a long output in JSON format. To filter the output, you need to use the --format option. To find out when the container was started, run the following command.

$ podman inspect webserver --format '{{.State.StartedAt}}'
2021-10-05 10:13:52.794806322 +0000 UTC

Accessing Container Shell

You can access the Shell prompt of any container using the exec option.

$ podman exec -it webserver2 /bin/bash

Pods

Podman has a unique feature that Docker lacks. Podman can create Pods from containers that operate together. This lets you manage multiple containers in aggregate.

To create a Pod, use the following command.

$ podman pod create --name mypod

Add the Containers to the newly created pod.

$ podman run --pod mypod --name myimage1 image:latest
$ podman run --pod mypod --name myimage2 diff-image:latest

You can now manage containers using simple one-line commands.

$ podman kill mypod      # Kill all containers
$ podman restart mypod   # Restart all containers
$ podman stop mypod      # Stop all containers
$ podman pod ps			# List all pods
$ podman pod top mypod   # Display running processes in a pod
$ podman pod inspect mypod # Inspect a Pod
$ podman pod rm mypod    # Remove the pod

Conclusion

This concludes our tutorial on installing and using Podman to run Containers. There are a lot of things you can do with Podman which we haven't covered. If you have any questions, post them in the comments below.

Share this page:

0 Comment(s)