Removal of Containers and Images

Overview

Teaching: 5 min
Exercises: 5 min
Questions
  • How do you cleanup old containers?

  • How do you delete images?

Objectives
  • Learn how to cleanup after working with containers

You can cleanup/remove a container with podman rm

podman rm <CONTAINER NAME>

Remove old containers

Start an instance of the tutorial container, exit it, and then remove it with podman rm

Solution

podman run matthewfeickert/intro-to-docker:latest
podman ps -a
podman rm <CONTAINER NAME>
podman ps -a
CONTAINER ID        IMAGE         COMMAND             CREATED            STATUS                     PORTS               NAMES
<generated id>      <image:tag>   "/bin/bash"         n seconds ago      Exited (0) t seconds ago                       <name>

<generated id>

CONTAINER ID        IMAGE         COMMAND             CREATED            STATUS                     PORTS               NAMES

You can remove an image from your computer entirely with podman rmi

podman rmi <IMAGE ID>

Remove an image

Pull down the Python 2.7 image (2.7-slim tag) from Docker Hub and then delete it.

Solution

podman pull python:2.7-slim
podman images python
podman rmi <IMAGE ID>
podman images python
2.7: Pulling from library/python
<some numbers>: Pull complete
<some numbers>: Pull complete
<some numbers>: Pull complete
<some numbers>: Pull complete
<some numbers>: Pull complete
<some numbers>: Pull complete
<some numbers>: Pull complete
<some numbers>: Pull complete
Digest: sha256:<the relevant SHA hash>
Status: Downloaded newer image for python:2.7-slim
docker.io/library/python:2.7-slim

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
python              2.7-slim            d75b4eed9ada        14 hours ago        886MB
python              3.9-slim            e440e2151380        23 hours ago        918MB

Untagged: python@sha256:<the relevant SHA hash>
Deleted: sha256:<layer SHA hash>
Deleted: sha256:<layer SHA hash>
Deleted: sha256:<layer SHA hash>
Deleted: sha256:<layer SHA hash>
Deleted: sha256:<layer SHA hash>
Deleted: sha256:<layer SHA hash>
Deleted: sha256:<layer SHA hash>
Deleted: sha256:<layer SHA hash>
Deleted: sha256:<layer SHA hash>
Deleted: sha256:<layer SHA hash>

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
python              3.9-slim            e440e2151380        23 hours ago        918MB

Helpful cleanup commands

What is helpful is to have a command to detect and remove unwanted images and containers for you. This can be done with prune, which depending on the context will remove different things.

  • podman container prune removes all stopped containers, which is helpful to clean up forgotten stopped containers.
  • podman image prune removes all unused or dangling images (images that do not have a tag). This is helpful for cleaning up after builds.
  • podman system prune removes all stopped containers, dangling images, and dangling build caches. This is very helpful for cleaning up everything all at once.

Key Points

  • Remove containers with podman rm <CONTAINER NAME>

  • Remove images with podman rmi <IMAGE ID>

  • Perform faster cleanup with podman container prune, podman image prune, and podman system prune