Skip to content

Docker - Images

Docker images are the blueprints for containers. They are read-only templates containing the application code, libraries, dependencies, tools, and other files needed for an application to run. Containers are runnable instances of images.

When you run a command like docker run hello-world:

  • docker: The Docker client command.
  • run: The command to create and start a container.
  • hello-world: The name of the image to use as the template for the container. If this image isn’t found locally, Docker attempts to download (pull) it from a configured registry (Docker Hub by default).

To see the list of Docker images currently stored on your local machine, use the docker images command (or docker image ls).

docker images

The output displays information about each image:

  • REPOSITORY: The name of the image repository (e.g., ubuntu, nginx, yourusername/myapp).
  • TAG: A specific version or variant identifier within the repository (e.g., 22.04, latest, 1.0, stable-alpine). If omitted, latest is often implied.
  • IMAGE ID: A unique identifier for the image.
  • CREATED: How long ago the image was built.
  • SIZE: The amount of disk space the image occupies.

Example Output:

REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest fbbc894d6882 7 months ago 13.3kB
ubuntu 22.04 a7be6190844e 2 weeks ago 77.8MB
nginx stable-alpine c7150a2f507e 3 days ago 23.5MB
my-apache-server 1.0 e4b1a9f8d2c1 5 minutes ago 198MB

While docker run automatically pulls an image if it’s not present locally, you can explicitly download an image using docker pull.

# Pull the specific version 8.0 of the MySQL image
docker pull mysql:8.0

This downloads the image layers from the registry (usually Docker Hub) and stores them locally.

To remove one or more local images and free up disk space, use the docker rmi command (or docker image rm). You can refer to images by their ID, or repository:tag combination.

# Remove the hello-world image by repository and tag
docker rmi hello-world:latest
# Remove the ubuntu image by its IMAGE ID (use the first few unique characters)
docker rmi a7be6190844e
# Remove multiple images
docker rmi mysql:8.0 my-apache-server:1.0

Note: You cannot remove an image that is currently being used by a container (even a stopped one). You must remove the container(s) first (docker rm <container_id>) before removing the image. Use the -f flag (docker rmi -f <image>) to force removal, but use this with caution.

To remove unused images (dangling images - those without tags, often left over from builds) easily, use:

docker image prune

To remove all unused images (not just dangling ones):

docker image prune -a

To get detailed, low-level information about an image in JSON format, including its layers, environment variables, exposed ports, and build history, use docker inspect.

docker inspect nginx:stable-alpine

This is useful for understanding how an image was built and configured.

You can create additional tags that refer to an existing image. This is often used before pushing an image to a registry.

# Tag the local image 'my-apache-server:1.0' with a new name suitable for Docker Hub
docker tag my-apache-server:1.0 yourdockerhubusername/my-web-app:v1

Now, yourdockerhubusername/my-web-app:v1 points to the same image ID as my-apache-server:1.0. This doesn’t duplicate the image data.