The lifecycle of a Docker image encompasses the operations needed to obtain, audit, and delete these immutable templates.
So far, Docker has been very kind to us. When we ran docker run hello-world, it automatically handled finding the image, downloading it, and running it.
This is great, but to keep our machine clean we also need to know how to manage and control the images we download and keep on our hard drive.
| Command | Description |
|---|---|
docker pull <image_name> | Downloads an image from a repository (Docker Hub). |
docker images | Lists all local images. |
docker rmi <image_id> | Deletes one or more images. |
docker tag <image_id> <new_name> | Tags an image with a new name. |
docker save -o <file.tar> <image_name> | Saves an image to a tar file. |
docker load -i <file.tar> | Loads an image from a tar file. |
Playground
Here you can play with the different commands and see their effect, without fear of filling up your computer’s hard drive 😉
Now we will look at the main commands in depth: Download (pull), List (images), and Delete (rmi) 👇.
Download Images (docker pull)
Although docker run downloads the image if it doesn’t have it, sometimes we want to download it before running it.
Why would we want to do this?
- To save time (pre-fetching) before a demo or deployment.
- To ensure we have the latest updated version.
The command is simple:
docker pull <image_name>:<tag>
For example, to download the latest version of Ubuntu:
docker pull ubuntu
Or if we want a specific version (vital in production), we use the tag:
docker pull python:3.9-alpine
If you don’t specify the tag (what comes after the colon), Docker assumes by default that you want the docker pull ubuntu is the same as docker pull ubuntu:latest.
List Images (docker images)
Great, we’ve downloaded some stuff. But what exactly do I have on my computer taking up space? To see your local “inventory”, we use:
docker images
You can also use the more modern syntax docker image ls
You’ll see a table like this:
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 2 months ago 141MB
ubuntu 20.04 d5ca7a445605 5 months ago 72.8MB
hello-world latest feb5d9fea6a5 6 months ago 13.3kB
- REPOSITORY: The image name (e.g., nginx).
- TAG: The specific version (e.g., latest, 20.04).
- IMAGE ID: The unique ID of that image. It’s a shortened hash (SHA256). It will be useful to refer to it unambiguously.
- SIZE: How much space it takes up on your disk.
Delete Images (docker rmi)
We’ve reached the critical point. You’ve been testing things and now you have 15GB of unused images. Time to clean up.
The command to delete an image is docker rmi (ReMove Image).
Mnemonic rule:
rm= Remove (for containers).rmi= Remove Image (for images).
We can delete by name or by ID:
docker rmi nginx
# Or using the ID
docker rmi 605c77e624dd
General Cleanup (prune)
Sometimes we have dozens of old images, dangling versions (which appear as <none>), and digital garbage. Going one by one is a pain.
Docker has a command to “take out the trash”:
docker image prune
This will delete all “dangling” images (those that have no name or tag and are useless).
If you’re feeling brave and want to delete ALL images that are not currently being used by a running container (be careful, you might delete things you wanted to keep):
docker image prune -a
