Images - how to work with them

Docker - Overview and how to use it

5 min read

Published Jul 13 2025


12
0
0
0

CLIContainersDevOpsDockerImagesNetworksVolumes

Listing images

docker image ls

Lists all images available on the local system.


This returns a list with the following columns:

  • Repository - The images name.
  • Tag - The images tag.
  • Image ID - Internal reference ID for the image.
  • Created - How long ago was the image created.
  • Size - How much storage space does the image take up.

docker image ls -

Lists all images available on the local system and all intermediate images too. You can also use --all instead of -a.


To filter the results returned you can use the --filter option

docker image ls --filter <filter_condition>

Some examples of different filters:

docker image ls --filter "dangling=true"

Filters for dangling images, which are images not tagged or referenced by any container. These often appear after builds, have no tag and usually safe to delete.


docker image ls --filter "label=env=production"

Filter for images with a label of env=production. (Labels can be added when building an image).


docker image ls --filter "label=env"

Filters for images with a label that has a key of env, ignoring the keys value.


docker image ls --filter "before=ubuntu:22.04"

Filters for all images built before a specific image, which can be useful for cleaning up older images.


docker image ls --filter "since=myapp:1.0"

Opposite of before. Shows images built after the given image.


docker image ls --filter "reference=myapp*"

Filters image names using wildcards, this shows all images with names that start with myapp.




Inspecting images

Show low-level metadata (JSON) about an image:

docker image inspect <imagename:tag>

This will give you the metadata for the specified image, you can also use the imageId that is out put with the list command instead of the name:tag.

eg. both these lines do the same thing on my machine:

docker image inspect postgres:latest
docker image inspect d4db79e6968f

When you use docker image inspect, it returns a large JSON object with a lot of technical metadata about the image. This can be overwhelming to read in raw format. To make it more human-friendly, you can extract specific fields using Go template syntax:

docker image inspect nginx:latest --format '{{.Architecture}}'

Returns just the architecture value, eg. amd64.


docker image inspect nginx --format '{{.RepoTags}} {{.Size}}'

Returns both the tag and the image size, eg. [nginx:latest] 14000000


docker image inspect nginx --format 'ID: {{.Id}}, OS: {{.Os}}, Arch: {{.Architecture}}'

Returns specific values formatted with keys for each one.


You can also pipe the response to the jq shell command to format it that way. eg:

docker image inspect nginx:latest | jq '.[0].Architecture'

Will output "amd64"


docker image inspect nginx | jq '.[0] | {id: .Id, os: .Os, arch: .Architecture}'

Will output:

{
  "id": "sha256:abcd1234...",
  "os": "linux",
  "arch": "amd64"
}

Common fields you might use:

  • .Id – The image ID
  • .RepoTags – Tag names for the image
  • .Architecture – CPU architecture
  • .Os – Operating system
  • .RootFS.Layers – All layers of the image
  • .Config.Env – Environment variables
  • .Config.Entrypoint / .Config.Cmd – Entrypoint/command



Image history

You can refer to the image with imagename:tag or the imageId:

docker image history <image>

This shows:

  • The image’s layers
  • Which commands created them (like RUN, COPY, etc.)
  • When each layer was created
  • The size of each layer
  • Whether a layer is cached, inherited, or manual

Example:

docker image history nginx:latest

Might return something like:

IMAGE CREATED CREATED BY SIZE COMMENT
7e4d58f0e5f3 2 weeks ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B
<missing> 2 weeks ago /bin/sh -c #(nop) EXPOSE 80 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ENTRYPOINT ["/docker-entry… 0B
<missing> 2 weeks ago /bin/sh -c #(nop) COPY file:... in / 1.2kB
<missing> 2 weeks ago /bin/sh -c apt-get update && apt-get install… 52MB
<missing> 2 weeks ago /bin/sh -c #(nop) ENV PATH=/usr/local/sbin… 0B
<missing> 2 weeks ago /bin/sh -c #(nop) LABEL maintainer=NGINX D… 0B
<missing> 2 weeks ago /bin/sh -c #(nop) FROM debian:bullseye 0B

The columns returned are:

  • IMAGE - Layer ID (or <missing> if merged or inherited).
  • CREATED - When the layer was added.
  • CREATED BY - Dockerfile instruction or shell command used to make the layer.
  • SIZE - Size of the layer.
  • COMMENT - Rarely used; sometimes includes system comments (e.g., buildkit)

It is useful to see if an image has unexpected behaviour (e.g., slow startup, big size), history helps you see what added which content. eg. You might notice one RUN command caused a 300MB increase which is a clue to refactor your Dockerfile. You can reverse-engineer the Dockerfile from the history output — especially if you don’t have the original Dockerfile.


You can filter out specific values by using the --format option:

docker image history myapp --format "{{.Size}}"

Or make the output more readable by doing something like:

docker image history myapp --format "Created by: {{.CreatedBy}} | Size: {{.Size}}"

Note:

  • Layers may show <missing> in the IMAGE column if they were squashed or not stored separately.
  • The base image (FROM) typically appears as the bottom layer.
  • History does not include files themselves — only the commands and metadata.



Tagging images

You can add new tags to images by calling:

docker image tag myapp:1.0 myapp:latest



Deleting images

These commands below delete the local copy of the image, they don't change the one stored in the repository.


You can remove an image if it is not in use in a container (including stopped containers) by calling:

docker image rm <image>

If any container is using that image then the command will fail and give an error message.


You can force the removal of an image that is being used by a stopped container by calling with the -f or --force option:

docker image rm -f myapp:1.0

This will still fail if the image is being used by an active container though, it only forces stopped container image removals.


Delete unused/dangling images (those not tagged or used):

docker image prune

Add -a option to remove all unused images, even tagged ones not in use:

docker image prune -a



Pulling an image

The pull command fetches images from a container registry and stores a local copy. It pulls each layer down, unless it has a copy of a particular layer with the same hash already.

docker pull <repository>/<image>:<tag>

So for example, to pull the latest Nginx image:

docker pull nginx

Docker assumes:

  • Registry: Docker Hub (registry-1.docker.io) - public image and on Docker Hub so no login required.
  • Repository: library/nginx
  • Tag: latest

So it's effectively:

docker pull docker.io/library/nginx:latest

If you are not getting the image from the Docker Hub, you would need to enter the full repository domain as part of the image name and have logged in first:


docker login myregistry.com

Logs in to your repository, and will ask you for username and password.

docker pull myregistry.com/myapp:latest

Then when you are logged in you can pull the image, you reference the image with the full path.

docker logout myregistry.com

One finished you, you can log out.



If pulling fails then you can ask for more verbose information:

docker pull myregistry.com/myapp --debug




Pushing an image

You use push to upload an image to a container registry.

docker push <registry>/<username>/<repo>:<tag>

You need to log in to the registry first, and have the correct permissions, before you can push an image.




Saving and loading images zipped as tar files

You can export and import images as tarballs:

docker image save -o myapp.tar myapp:1.0

Will save the image as a file.


docker image load -i myapp.tar

Will load an image from a file.


Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact