Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


RHCSA - Manage Containers: Perform Basic Container Management such as Running, Starting, Stopping, & Listing Running Containers

Listing Containers

To list running containers you use the podman ps command and if you want to list all containers, whether they are running or stopped you use the podman ps -a command. You will use these commands in the upcoming exercises to validate the state of containers.

Running a Container

Running a container means creating and starting a new instance of a containerized application. This process involves specifying various parameters, such as which image to use, port mappings, environment variables, and more, using the podman run command.

Without specifying the -d (detach) option, the container will run in the foreground, which is usually not what we want. Also fo containers that you wish to serve content over the network you may need to specify the -p <localport>:<containerport> option.

Run an nginx container:

To run an nginx container:

podman run -d -p 8080:80 nginx

The containers ID will print to screen when running the command. For me the ID was:

01521c4608c6b12a45067256f8a38bba87e6a316c0bf4b95cb4cca42ada051ec

List details for our running container:

podman ps

The output will resemble the following, which confirms localhost 8080 points to container port 80. Also note that the container ID is shortened:

CONTAINER ID  IMAGE                          COMMAND               CREATED        STATUS        PORTS                 NAMES
01521c4608c6  docker.io/ubuntu/nginx:latest  nginx -g daemon o...  3 minutes ago  Up 3 minutes  0.0.0.0:8080->80/tcp  recursing_kepler

Confirm the container is working as expected:

curl http://localhost:8080

The nginx welcome page should display:

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Stopping a Container

Stopping a container in halts the execution of all processes within the container, effectively pausing its operation. This action is initiated using the podman stop command followed by the container's name or ID. When a container is stopped, it gracefully terminates its running processes, allowing them to clean up and release any resources they were using. Stopping a container is useful when you want to temporarily halt its operation, save its state, or perform maintenance tasks.

Stop the running nginx container:

The nginx container in the previous exercise should still be running. To stop it, run the following, replacing the container ID I have used with the container ID you received hen running the podman container ls command:

podman stop 01521c4608c6

The container ID will print to screen when running this command:

01521c4608c6

Running podman ps will no longer display the container as it is not running. You need to include the -a option to display stopped containers:

podman ps -a

The output will show STATUS of Exited:

CONTAINER ID  IMAGE                          COMMAND               CREATED         STATUS                    PORTS                 NAMES
01521c4608c6  docker.io/ubuntu/nginx:latest  nginx -g daemon o...  14 minutes ago  Exited (0) 2 minutes ago  0.0.0.0:8080->80/tcp  recursing_kepler

Starting a Container

Starting a container refers to initializing a previously created container that is in a stopped state. When you start a container, it transitions from an inactive state to an active one, allowing it to execute its designated tasks and services. This operation uses the podman start command followed by the container's name or ID. Starting a container doesn't change its configuration; it retains the settings and data defined during its creation.

Start the stopped nginx container:

To start back up the nginx container you use the podman start command with the container ID as an argument:

podman start 01521c4608c6

The container ID will print to screen when running this command:

01521c4608c6

Confirm the container is running:

podman ps

The output will resemble:

CONTAINER ID  IMAGE                          COMMAND               CREATED        STATUS        PORTS                 NAMES
01521c4608c6  docker.io/ubuntu/nginx:latest  nginx -g daemon o...  3 minutes ago  Up 3 minutes  0.0.0.0:8080->80/tcp  recursing_kepler

Rather than running a curl test, this time we will connect to the container using the podman exec -it command to run a shell process:

podman exec -it 01521c4608c6 /bin/sh

As the /bin/sh shell was chosen as the executable you get dropped into the container running that shell. From there you can run a limited set of commands, such as:

df -h
ps -ef
free -h

Use Ctrl+D to detach from the container.

Restarting Containers

Restarting a container in involves stopping the container if it's currently running and then immediately starting it again. This action is typically performed using the podman restart command followed by the container's name or ID. When a container is restarted, it goes through the process of stopping and then starting, which can be useful for applying configuration changes, refreshing the container's environment, or recovering from minor issues without having to recreate the container entirely.

Restart the nginx container:

To restart the nginx container you use the podman restart command with the container ID as an argument:

podman restart 01521c4608c6

The container ID will print to screen when running this command:

01521c4608c6

Confirm the container is running:

podman ps

The output will resemble:

CONTAINER ID  IMAGE                          COMMAND               CREATED         STATUS         PORTS                 NAMES
01521c4608c6  docker.io/ubuntu/nginx:latest  nginx -g daemon o...  24 minutes ago  Up 34 seconds  0.0.0.0:8080->80/tcp  recursing_kepler

This time, to check if the container restarted ok, we will use the podman top command which displays the processes running within the container:

podman top 01521c4608c6

The output should resemble:

USER      PID  PPID  %CPU   ELAPSED          TTY  TIME  COMMAND
root      1    0     0.000  7m46.568465998s  ?    0s    nginx: master process nginx -g daemon off; 
www-data  13   1     0.000  7m46.568578753s  ?    0s    nginx: worker process 
www-data  14   1     0.000  7m46.568619465s  ?    0s    nginx: worker process

Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.