Working with Podman Containers (with examples)

Working with Podman Containers (with examples)

In this article, we will explore various use cases of the podman ps command, which allows us to list Podman containers. We will cover different scenarios such as listing running and stopped containers, filtering containers by name, ancestor image, exit status code, and status, as well as filtering based on mounted volumes.

List currently running podman containers

To list all the currently running Podman containers, you can execute the following command:

podman ps

Motivation: This use case is helpful when you want to check the status of your running containers and get a quick overview of the containers currently active.

Explanation: The podman ps command without any additional options will display the list of running containers. It provides information such as the container ID, image, command, creation time, status, and names.

Example Output:

CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS             PORTS    NAMES
0a1b2c3d4e5f  docker.io/nginx:latest          nginx -g daemin...    2 minutes ago   Up 2 minutes ago   80/tcp   webserver

List all podman containers (running and stopped)

To list all Podman containers, including both running and stopped containers, you can use the --all option as shown below:

podman ps --all

Motivation: This use case is useful when you need to have an overview of all the containers, including those that are not currently running.

Explanation: The --all option includes all containers, regardless of their status (running, stopped, paused, or exited). The command will display information about all containers, similar to the previous use case.

Example Output:

CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS             PORTS    NAMES
0a1b2c3d4e5f  docker.io/nginx:latest          nginx -g daemin...    2 minutes ago   Exited (0) 2...    80/tcp   webserver
1a2b3c4d5e6f  docker.io/mysql:latest          mysqld                3 hours ago     Up 3 hours ago     3306/tcp db

Show the latest created container (includes all states)

To display information about the latest created container, regardless of its current status, you can use the --latest option:

podman ps --latest

Motivation: This use case allows you to quickly check the details of the most recently created container, even if it is not currently running.

Explanation: The --latest option displays information about the most recently created container, including its ID, image, command, creation time, status, and names.

Example Output:

CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS             PORTS    NAMES
0a1b2c3d4e5f  docker.io/nginx:latest          nginx -g daemin...    2 minutes ago   Up 2 minutes ago   80/tcp   webserver

Filter containers that contain a substring in their name

To filter containers based on a substring in their name, you can use the --filter option along with the desired name substring:

podman ps --filter "name=name"

Motivation: Filtering containers by name substring can be helpful when you have a large number of containers and want to narrow down the list to only those with specific names or containing specific words.

Explanation: The --filter option allows you to specify a condition to filter the containers. In this case, we use name=name to look for containers that contain the substring “name” in their names.

Example Output:

CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS             PORTS    NAMES
0a1b2c3d4e5f  docker.io/nginx:latest          nginx -g daemin...    2 minutes ago   Up 2 minutes ago   80/tcp   app1_name
1a2b3c4d5e6f  docker.io/mysql:latest          mysqld                3 hours ago     Up 3 hours ago     3306/tcp app2_name

Filter containers that share a given image as an ancestor

To filter containers based on a specific image ancestor, you can use the --filter option along with the ancestor argument, specifying the image and tag:

podman ps --filter "ancestor=image:tag"

Motivation: Filtering containers by ancestor image allows you to easily narrow down the list of containers based on a common base image, providing insights into containers derived from a specific source.

Explanation: The --filter option with ancestor=image:tag allows you to filter containers that share the given image as an ancestor. Replace image with the desired image name and tag with the specific tag.

Example Output:

CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS             PORTS    NAMES
1a2b3c4d5e6f  docker.io/mysql:latest          mysqld                3 hours ago     Up 3 hours ago     3306/tcp db
4a5b6c7d8e9f  docker.io/mysql:latest          mysqld                1 day ago       Exited (0) 1...    3306/tcp app_db

Filter containers by exit status code

To filter containers based on the exit status code, you can use the --filter option along with the exited argument and the desired code:

podman ps --all --filter "exited=code"

Motivation: Filtering containers by exit status code can be useful when you want to identify containers that exited with a specific code, for troubleshooting or monitoring purposes.

Explanation: The --all option is used in conjunction with --filter to include all containers in the list. The exited=code argument filters containers based on their exit status code, where code should be replaced with the desired exit status code.

Example Output:

CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS             PORTS    NAMES
4a5b6c7d8e9f  docker.io/mysql:latest          mysqld                1 day ago       Exited (0) 1...    3306/tcp app_db

Filter containers by status

To filter containers by their status (created, running, removing, paused, exited, or dead), you can use the --filter option with the status argument:

podman ps --filter "status=status"

Motivation: Filtering containers by status can help you focus on containers in a specific state, such as running containers for real-time monitoring or paused containers for troubleshooting.

Explanation: The --filter option with status=status allows you to filter containers based on their status. Replace status with the desired container status (created, running, removing, paused, exited, or dead) to filter by that specific status.

Example Output:

CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS             PORTS    NAMES
0a1b2c3d4e5f  docker.io/nginx:latest          nginx -g daemin...    2 minutes ago   Up 2 minutes ago   80/tcp   webserver

Filter containers that mount a specific volume or have a volume mounted in a specific path

To filter containers based on a specific volume mount or volume mounted in a specific path, you can use the --filter option along with the volume argument, specifying the volume or path:

podman ps --filter "volume=path/to/directory" --format "table .ID\t.Image\t.Names\t.Mounts"

Motivation: Filtering containers by mounted volumes allows you to find containers that have a specific volume mounted or are accessing a certain path within the container’s filesystem.

Explanation: The --filter option with volume=path/to/directory allows you to filter containers based on their volume mounts. Specify the desired exact volume name or the path to a directory within the container. The --format option is used to specify the desired output format, in this case, a table displaying the container ID, image, names, and mounts.

Example Output:

CONTAINER ID  IMAGE                           NAMES               MOUNTS
0a1b2c3d4e5f  docker.io/nginx:latest          webserver           /path/to/volume:/container/path

By understanding these use cases and examples of the podman ps command, you can effectively list, filter, and manage your Podman containers based on various criteria.

Related Posts

How to use the command 'cargo remove' (with examples)

How to use the command 'cargo remove' (with examples)

The ‘cargo remove’ command is a useful tool for removing dependencies from a Rust project’s Cargo.

Read More
How to use the command 'brew' (with examples)

How to use the command 'brew' (with examples)

Homebrew is a package manager for macOS and Linux, which allows users to easily install, manage, and update software packages.

Read More
How to use the command 'ufraw-batch' (with examples)

How to use the command 'ufraw-batch' (with examples)

The ‘ufraw-batch’ command is a powerful utility that allows you to convert RAW files from cameras into standard image files.

Read More