5.1 Command docker images
If you already have Docker images stored on your system, there are special commands to manage them. First and foremost, we're talking about docker images
and docker rmi
. In this lecture, we'll take a closer look at how to use these commands to efficiently manage Docker images.
The docker images
command displays a list of images stored locally on your system. It shows info like the repository name, tag, image ID, creation date, and size. This info lets you quickly evaluate which images are available and how much space they take up.
Syntax:
docker images [OPTIONS] [REPOSITORY[:TAG]]
Where:
-
OPTIONS
: additional parameters for filtering and formatting the output. -
[REPOSITORY[:TAG]]
: (optional) a filter by repository name and/or tag.
Basic Usage Example
This example outputs a list of all images stored locally.
docker images
Sample output of the docker images
command:
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 5a7e5b1a3b8c 2 days ago 133MB
ubuntu 20.04 ba6acccedd29 3 weeks ago 72.9MB
myapp 1.0 d1e5e2ff5ff2 1 month ago 89.5MB
Column Descriptions:
- REPOSITORY: the name of the repository the image belongs to.
- TAG: the tag indicating the image version.
- IMAGE ID: a unique identifier for the image.
- CREATED: the time since the image was created.
- SIZE: the size of the image in memory.
5.2 Image Filtering
Image filtering lets you narrow down the results of the docker images
command to see only the images that meet your criteria. This is handy if you’ve got a lot of images stored on your system and you need to quickly find a specific one by repository name, tag, or other parameters.
1. Filter by Repository
docker images nginx
This example will show only images from the nginx
repository.
2. Filter by Tag
docker images ubuntu:20.04
This example will display the ubuntu
image with the tag 20.04
.
3. Using the -a (all)
Parameter
By default, the docker images
command only shows the latest versions of images. To display all images, including intermediate layers, use the -a
option.
docker images -a
4. Formatting Output
To make the output easier to read or integrate with other tools, you can customize the output format using the --format
option. This is especially useful in scripts and for automating tasks.
docker images --format "{{.Repository}}:{{.Tag}} {{.ID}}"
5.3 The docker rmi
Command
The docker rmi
command is used to remove one or more Docker images. It's handy for saving disk space and keeping the system clean, especially if the images are no longer in use.
Syntax:
docker rmi [OPTIONS] IMAGE [IMAGE...]
Where:
- OPTIONS: additional options to control the removal process.
- IMAGE: the name, tag, or ID of the image to be removed.
Basic Usage Example
This example removes an image with the name nginx
.
docker rmi nginx
Removing an Image by ID
This example removes an image with the ID 5a7e5b1a3b8c
.
docker rmi 5a7e5b1a3b8c
Force Removal
If an image is in use by a container or locked, standard removal might not work. Use the -f (force)
option for force removal.
docker rmi -f myapp:1.0
Removing Multiple Images
To remove multiple images at once, list them separated by spaces.
docker rmi nginx ubuntu:20.04 myapp:1.0
5.4 Practical Examples
Example 1: Viewing All Images
To get the full list of all images stored on your local machine, run the command:
docker images
This command will display a table with repository names, tags, image IDs, creation dates, and sizes.
Example 2: Viewing Images with Formatted Output
To get a list of images with custom formatting, for instance, to show only the repository name and tag:
docker images --format "{{.Repository}}:{{.Tag}}"
This example shows how to use the --format
option to customize the output, which is handy when writing scripts.
Example 3: Removing All Unused Images
If you want to remove all images not used by containers, first get their list, then perform the deletion:
docker images -q
docker rmi $(docker images -q)
The docker images -q
command outputs just the IDs of all locally stored images, which are then passed to docker rmi
for removal.
Example 4: Removing Dangling Images
Dangling images
are images without tags that are left after image updates or failed builds. To remove them, use the command:
docker rmi $(docker images -f "dangling=true" -q)
Here -f "dangling=true"
filters out images without tags, and -q
outputs only their IDs, which are passed into the removal command.
GO TO FULL VERSION