Essential Docker Commands for Managing Images

First, let’s discuss the common commands for Docker images.

Search for Images

You can use the docker search command to search for images stored in Docker Hub.

Command format:

docker search [OPTIONS] TERM

Parameters:

Name, shorthand Default Description
--automated false Only list automated build images
--filter, -f Filter results based on specified criteria
--limit 25 Maximum number of search results
--no-trunc false Do not truncate output, display full output
--stars, -s 0 Only show results with stars not less than this value

Example 1:

docker search java

After executing this command, Docker will search for image repositories containing the keyword “java” in Docker Hub. You will see a table similar to the following:

NAME                    DESCRIPTION                STARS     OFFICIAL   AUTOMATED
java                    Java is a concurrent, ...   1281      [OK]       
anapsix/alpine-java     Oracle Java 8 (and 7) ...   190                  [OK]
isuper/java-oracle      This repository conta ...   48                   [OK]
lwieske/java-8          Oracle Java 8 Contain ...   32                   [OK]
nimmis/java-centos      This is docker images ...   23                   [OK]
...

This table contains five columns with the following meanings:

1. NAME: Image repository name.

2. DESCRIPTION: Image repository description.

3. STARS: Number of stars for the image repository, indicating its popularity, similar to GitHub stars.

4. OFFICIAL: Indicates whether it is an official repository; images marked with [OK] are created and maintained by the official project teams of each software. From the results, we can see that the java image repository is an official repository, while others are not.

5. AUTOMATED: Indicates whether it is an automated build image repository.

Example 2:

docker search -s 10 java

Download Images [Important]

Use the docker pull command to download images from Docker Registry.

Command format:

docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Parameters:

Name, shorthand Default Description
--all-tags, -a false Download all tags of the image
--disable-content-trust true Ignore image verification

Example 1:

docker pull java

After executing this command, Docker will download the latest version of the Java image from the java repository in Docker Hub.

Example 2:

This command can also specify the desired image tag and Docker Registry address, for example:

docker pull reg.itmuch.com/java:7

This allows you to download the Java image with tag 7 from the specified Docker Registry.

List Images [Important]

Use the docker images command to list the downloaded images.

After executing this command, you will see a table similar to the following:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
java                latest              861e95c114d6        4 weeks ago         643.1 MB
hello-world         latest              c54a2cc56cbb        5 months ago        1.848 kB

This table contains five columns with the following meanings:

1. REPOSITORY: Name of the image’s repository.

2. TAG: Image tag. The default is latest, indicating the most recent version.

3. IMAGE ID: Image ID, indicating the unique identifier of the image.

4. CREATED: Time the image was created.

5. SIZE: Size of the image.

Command format:

docker images [OPTIONS] [REPOSITORY[:TAG]]

Parameters:

Name, shorthand Default Description
--all, -a false List all images locally (including intermediate image layers, which are filtered out by default)
--digests false Display digest information
--filter, -f Display images that meet the conditions
--format Display images using a Go template file
--no-trunc false Do not truncate output, display complete image information
--quiet, -q false Only display image IDs

Example:

docker images
docker images java
docker images java:8
docker images --digests
docker images --filter "dangling=true"   # Display dangling images

Delete Local Images [Important]

Use the docker rmi command to delete specified images.

Command format:

docker rmi [OPTIONS] IMAGE [IMAGE...]

Parameters:

Name, shorthand Default Description
--force, -f false Force delete
--no-prune false Do not remove the process images of the image, by default remove

Example 1: Delete the specified image by name.

docker rmi hello-world

This means deleting the hello-world image.

Example 2: Delete all images.

docker rmi -f $(docker images)

The -f parameter means to force deletion.

Save Images

Use the docker save command to save images.

Command format:

docker save [OPTIONS] IMAGE [IMAGE...]

Parameters:

Name, shorthand Default Description
--output, -o Write to a file, instead of STDOUT

Example 1:

docker save busybox > busybox.tar
docker save --output busybox.tar busybox

Load Images

Use the docker load command to load images.

Command format:

docker load [OPTIONS]

Parameters:

Name, shorthand Default Description
--input, -i Load from file instead of STDIN
--quiet, -q false Load silently

Example 1:

docker load < busybox.tar.gz
docker load --input fedora.tar

Build Images [Important]

Build images using a Dockerfile.

Command format:

docker build [OPTIONS] PATH | URL | -

Parameters:

Name, shorthand Default Description
--add-host Add custom host-to-IP mappings, format is (host:ip)
--build-arg Set build-time variables
--cache-from Images to use as cache sources
--cgroup-parent Optional parent cgroup for the container
--compress false Use gzip to compress build context
--cpu-period 0 Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota 0 Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-shares, -c 0 CPU weight (relative weight)
--cpuset-cpus Specify allowed CPUs to execute
--cpuset-mems Specify allowed memory to execute
--disable-content-trust true Ignore verification
--file, -f Specify the name of the Dockerfile, default is ‘PATH/Dockerfile’
--force-rm false Remove intermediate containers
--iidfile Write the image ID to a file
--isolation Container isolation technology
--label Set metadata used by the image
--memory, -m 0 Set memory limit
--memory-swap 0 Set the maximum value of swap to memory + swap; if set to -1 means unlimited swap
--network default Set the network mode for RUN instructions during build
--no-cache false Do not use cache during image build
--pull false Always try to update to the new version of the image
--quiet, -q false Silent mode, only output image ID after successful build
--rm true Immediately remove intermediate containers after successful build
--security-opt Security options
--shm-size 0 Specify the size of the /dev/shm directory
--squash false Compress the built layers into a new layer
--tag, -t Set tag, format: name:tag, tag is optional
--target Set the target build stage during build
--ulimit Ulimit options

Further Reading

Docker commands: https://docs.docker.com/engine/reference/commandline/docker/

Leave a Comment