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

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

Podman is a simple management tool that provides a command-line interface for managing pods, containers, and images. It is comparable to the Docker-CLI command-line tool and can be used as a drop-in replacement for Docker. Here, we will explore various use cases of the ‘podman’ command and understand how to utilize it effectively.

Use case 1: List all containers (both running and stopped)

Code:

podman ps --all

Motivation:

Listing all containers, including running and stopped ones, can be helpful for management and debugging. This command provides an overview of all the containers present on the system.

Explanation:

  • podman ps: This command lists all running containers by default.
  • --all: This option lists all containers, including the ones that are stopped.

Example output:

CONTAINER ID  IMAGE             COMMAND     CREATED       STATUS          PORTS  NAMES
ab12cd34ef56  nginx:latest      nginx -g    5 minutes ago Up 3 minutes          web_server
1a2b3c4d5e6f  busybox:latest    /bin/sh     10 minutes ago Exited (0) 5 minutes ago  echo_container

Use case 2: Create a container from an image, with a custom name

Code:

podman run --name container_name image

Motivation:

Creating containers from images is a fundamental operation when working with Podman. Specifying a custom name for the container allows for easier management and identification.

Explanation:

  • podman run: This command creates and runs a new container.
  • --name: This option assigns a custom name to the container.
  • container_name: This argument specifies the desired name for the container.
  • image: This argument specifies the image to use for creating the container.

Example output:

ab12cd34ef56

Use case 3: Start or stop an existing container

Code:

podman start|stop container_name

Motivation:

Starting or stopping containers is essential for managing their life cycle. This command allows users to control the status of existing containers.

Explanation:

  • podman start: This command starts a stopped container.
  • podman stop: This command stops a running container.
  • container_name: This argument specifies the name of the container to start or stop.

Example output:

Started container_name

Use case 4: Pull an image from a registry (defaults to Docker Hub)

Code:

podman pull image

Motivation:

Pulling images from a registry allows users to access and use them locally. By default, Podman pulls images from Docker Hub, the most popular container registry.

Explanation:

  • podman pull: This command pulls an image from a registry.
  • image: This argument specifies the name and tag of the image to pull.

Example output:

Getting image source signatures
Copying blob sha256:abcd1234 to cache
...
...
Status: Downloaded newer image for image:latest

Use case 5: Display the list of already downloaded images

Code:

podman images

Motivation:

Having a clear overview of the images present locally can help with management and resource allocation. This command provides detailed information about the downloaded images.

Explanation:

  • podman images: This command lists all the downloaded images and their details.

Example output:

REPOSITORY      TAG     IMAGE ID      CREATED       SIZE
nginx           latest  abcd1234      5 minutes ago  69MB
busybox         latest  1a2b3c4d5e6f  10 minutes ago 4.84MB

Use case 6: Open a shell inside an already running container

Code:

podman exec --interactive --tty container_name sh

Motivation:

Accessing the shell inside a running container allows for executing commands and interacting with the container’s environment. This command enables users to open a shell session within a container.

Explanation:

  • podman exec: This command executes a command inside a running container.
  • --interactive: This option keeps STDIN open, allowing for an interactive session.
  • --tty: This option allocates a pseudo-TTY.
  • container_name: This argument specifies the name of the running container.
  • sh: This argument represents the shell to be invoked within the container.

Example output:

/ # 

Use case 7: Remove a stopped container

Code:

podman rm container_name

Motivation:

Removing stopped containers helps in freeing up resources and decluttering the system. This command allows for the removal of containers that are no longer needed.

Explanation:

  • podman rm: This command removes one or more stopped containers.
  • container_name: This argument specifies the name of the stopped container to remove.

Example output:

container_name

Use case 8: Display the logs of one or more containers and follow log output

Code:

podman logs --follow container_name container_id

Motivation:

Accessing the logs of containers can help in troubleshooting and monitoring. This command displays the logs of one or multiple containers, and the --follow option allows for following the log output in real-time.

Explanation:

  • podman logs: This command displays the logs of one or more containers.
  • --follow: This option continuously streams the log output.
  • container_name container_id: These arguments specify the names or IDs of the containers whose logs should be displayed.

Example output:

2021/07/01 10:00:01 Container container_name: Successfully started.
2021/07/01 10:01:02 Container container_id: Exited with status code 0.

Conclusion:

The ‘podman’ command is a versatile tool that simplifies managing pods, containers, and images. By exploring the use cases mentioned above, users can gain a better understanding of its capabilities and leverage it effectively for their container-related tasks. Whether it is listing containers, creating them, pulling images, or accessing logs, ‘podman’ provides a comprehensive set of features for container management.

Related Posts

How to use the command "aws dynamodb" (with examples)

How to use the command "aws dynamodb" (with examples)

The “aws dynamodb” command line interface (CLI) is used to interact with Amazon Web Services (AWS) DynamoDB, a fully managed NoSQL database service.

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

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

The ’legit’ command is a complementary command-line interface for Git. It provides additional functionality and simplifies certain tasks when working with Git repositories.

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

How to use the command 'VBoxManage unregistervm' (with examples)

The ‘VBoxManage unregistervm’ command is used to unregister a virtual machine (VM) in VirtualBox.

Read More