Open In App

How to Safely Remove Containers with Podman

Last Updated : 08 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Podman is a container management tool that helps in creating, deleting, or managing containerized environments. It is daemonless, unlike Docker, and is considered a more secure alternative in some cases. It also supports rootless containers that allow us to run containers inside Podman without having any root privileges. In this article, we will learn how to safely remove containers with a podman.

Why Safely Removing Containers is Important

For containers that aren't in use, safely removing those ensures that we are freeing up the resources that are used by the containers as well, for example, RAM, storage, etc. We also ensure that there are no stale containers running that might pose any potential security risk in the future.

Steps to Remove Containers Gracefully in Podman

To remove containers gracefully in Podman, we should stop the container first, and then remove the container instance from the Podman. We should also ensure to free up any associated volumes that were mounted with the container.

# stop the container
podman stop <container_id>
# remove the container
podmand rm <container_id>

Step 1: Create a Dockerfile, and start the container

In this step, first, we will create a basic Dockerfile, that uses a Ubuntu image, and we will start a container using the image built from the Dockerfile.

Filename: Dockerfile

FROM ubuntu:latest
CMD tail -f /dev/null

Build image from the Dockerfile

podman build -t my-hello-world .
Create Dockerfile, and start container

Start docker container using the image:

podman run my-hello-world

Step 2: Stopping a container using podman

Before removing a container, we should stop the container first so as to gracefully shutdown all the operations running in it. First, find the container id that is running:

podman ps

Output:

podman-ps

Now, copy the container id from above, and in the below command to stop the container:

podman stop 8d19da88349c

Output:

podman-stop

Step 3: Remove the container

Once the container is stopped, we will remove the container using the below command, and the container ID we copied above:

podman rm 8d19da88349c

Output:

podman-rm

Article Tags :

Similar Reads