Skip to content

Lesson 1.4: Working with Images

Welcome to Lesson 1.4! Now that you understand Docker's architecture, it's time to get hands-on with the building blocks of containers: images. In this lesson, you'll learn how to find, download, manage, and remove images. By the end, you'll be comfortable with the image lifecycle.


Learning Objectives

TIP

By the end of this lesson, you will be able to:

  • Search for images on Docker Hub.
  • Pull images from a registry using docker pull.
  • List and inspect images stored locally.
  • Understand image tags and how to use them.
  • Remove images you no longer need.
  • Explain the difference between official and community images.

1. What Are Docker Images? (Quick Recap)

A Docker image is a read-only template that contains everything needed to run a container:

  • A base operating system (e.g., Ubuntu, Alpine).
  • Application code.
  • Dependencies (libraries, binaries).
  • Configuration (environment variables, default commands).

Images are built in layers, and each layer is cached. This makes pulling and building images efficient.

Images are stored in registries. The default registry is Docker Hub, but you can use others like Google Container Registry, Amazon ECR, or private registries.


2. Searching for Images

Before pulling an image, you might want to see what's available. You can search Docker Hub directly from the command line.

The docker search command lets you find images on Docker Hub.

Syntax:

bash
docker search [OPTIONS] TERM

Example: Search for Nginx images:

bash
docker search nginx

Sample output:

NAME                               DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                              Official build of Nginx.                        15000     [OK]       
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   2000                 [OK]
nginx/nginx-ingress               NGINX and  NGINX Plus Ingress Controllers fo…   50
...

Columns:

  • NAME: Image name (repository).
  • DESCRIPTION: Short description.
  • STARS: Number of stars (popularity).
  • OFFICIAL: Indicates if it's an official image (maintained by Docker or the software authors).
  • AUTOMATED: Whether it's an automated build from a GitHub/Bitbucket repo.

Filtering results: You can limit output with --limit or filter by stars. For example, show only official images:

bash
docker search --filter "is-official=true" nginx

2.2. Browsing on Docker Hub

You can also visit hub.docker.com in your browser to explore images, read documentation, and see tags.


3. Pulling Images

The docker pull command downloads an image (or specific layers) from a registry to your local machine.

Syntax:

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

3.1. Pulling the Latest Tag

If you don't specify a tag, Docker pulls the latest tag by default.

bash
docker pull ubuntu

This is equivalent to docker pull ubuntu:latest.

Output will show the layers being downloaded:

Using default tag: latest
latest: Pulling from library/ubuntu
6e3729cf69e0: Pull complete
Digest: sha256:2e863c7b3c38b04...b3c
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

3.2. Pulling a Specific Tag

Tags allow you to specify a particular version of an image. For example, to pull Ubuntu 22.04:

bash
docker pull ubuntu:22.04

Common tags include:

  • nginx:1.23 (specific version)
  • python:3.11-slim (slim variant, smaller size)
  • alpine:3.18 (lightweight Linux distribution)

TIP

Why use tags? Without tags, you might accidentally get an updated version that breaks compatibility. Tags give you control.

3.3. Pulling by Digest

Every image has a unique digest (SHA256 hash) that never changes, even if the tag is updated. You can pull by digest for absolute reproducibility:

bash
docker pull ubuntu@sha256:2e863c7b3c38b04...b3c

To find the digest of a pulled image, use docker images --digests.

3.4. Pulling from a Different Registry

To pull from a registry other than Docker Hub, include the registry hostname:

bash
docker pull myregistry.local:5000/myapp:latest

4. Listing Images

After pulling, you can see which images are stored locally.

4.1. docker images or docker image ls

Both commands list images.

bash
docker images

Output example:

REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
ubuntu       latest    ba6acccedd29   2 weeks ago   72.8MB
ubuntu       22.04     ba6acccedd29   2 weeks ago   72.8MB
nginx        latest    605c77e624dd   3 weeks ago   141MB

Columns:

  • REPOSITORY: Name of the image.
  • TAG: Tag of the image.
  • IMAGE ID: Unique identifier (truncated).
  • CREATED: When the image was built.
  • SIZE: Virtual size (sum of all layers).

Show digests:

bash
docker images --digests

This adds a DIGEST column.

Filter images: Show only images with a specific repository or tag:

bash
docker images ubuntu
docker images --filter "reference=ubuntu:22.04"

4.2. Inspecting an Image

To see detailed metadata (JSON format), use:

bash
docker image inspect ubuntu:latest

This shows environment variables, default command, architecture, layers, etc.


5. Tagging Images

Tags are not just for pulling; you can also add tags to your local images. This is useful for:

  • Preparing to push to a registry.
  • Creating aliases for specific versions.

5.1. docker tag

Syntax:

bash
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Example: Tag the ubuntu:latest image as myubuntu:v1:

bash
docker tag ubuntu:latest myubuntu:v1

Now when you run docker images, you'll see two entries: the original ubuntu:latest and myubuntu:v1, but they share the same IMAGE ID. Tags are just references to the same image.

Use case: Before pushing an image to your own repository, you'd tag it with your Docker Hub username:

bash
docker tag myapp:latest yourusername/myapp:1.0

Then you can push it.


6. Removing Images

Over time, unused images can consume disk space. Use docker rmi to remove them.

6.1. docker rmi (remove image)

Syntax:

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

Remove by repository:tag:

bash
docker rmi myubuntu:v1

Remove by IMAGE ID:

bash
docker rmi ba6acccedd29

WARNING

If the image is being used by any container (even stopped), Docker will prevent removal unless you force it.

6.2. Force Removal

If an image is used by a container, you can force removal with -f:

bash
docker rmi -f ba6acccedd29

Warning: This only removes the image tag/ID, not the container. The container may still exist but will show <none> for its image.

6.3. Pruning Unused Images

Docker provides a convenient command to remove all unused images (dangling and unreferenced):

bash
docker image prune

Add -a to remove all unused images (not just dangling):

bash
docker image prune -a

You'll be prompted to confirm. Use -f to skip confirmation.


7. Official vs. Community Images

TypeDescriptionExamples
Official ImagesMaintained by Docker or software authors. Trusted, up-to-date.ubuntu, nginx, mysql
Community ImagesUploaded by individuals/organizations. May vary in quality.Various third-party images

TIP

You can identify official images by the [OK] under OFFICIAL in docker search, or on Docker Hub by the "Official Image" badge.


Hands-On Tasks

Complete these tasks to practice working with images.

  • [ ] Task 1: Search and Explore

    1. Search for "alpine" images on Docker Hub using docker search.
    2. Identify an official alpine image and a community one with many stars.
    3. Visit the Docker Hub page for the official alpine image (https://hub.docker.com/_/alpine) and note the available tags.
  • [ ] Task 2: Pull Multiple Images

    1. Pull the following images:
      • alpine:latest
      • alpine:3.18
      • nginx:alpine (a small Nginx image)
      • hello-world (if not already)
    2. Observe the download output. Notice how layers are reused if they already exist (e.g., alpine base layers).
  • [ ] Task 3: List and Inspect

    1. List all images with docker images.
    2. Show digests with docker images --digests.
    3. Inspect the alpine image: docker image inspect alpine:latest. Look at the "Env", "Cmd", and "Architecture" fields.
    4. View the history of the alpine image: docker history alpine:latest. Notice the small layer sizes.
  • [ ] Task 4: Tagging

    1. Tag the alpine:latest image as myalpine:test.
    2. Verify the new tag appears in docker images (same IMAGE ID).
    3. Tag the same image as myalpine:1.0.
  • [ ] Task 5: Remove Images

    1. Remove the myalpine:test tag:
      bash
      docker rmi myalpine:test
      (The underlying image remains because it has other tags.)
    2. Try to remove the alpine:latest image using its IMAGE ID. If any containers are using it, it will fail. (We'll clean up containers later.)
    3. Run docker image prune -a and type y when prompted. Observe which images are removed.
  • [ ] Task 6: Pull by Digest (Optional)

    1. Find the digest of the hello-world image: docker images --digests hello-world.
    2. Pull it again by digest: docker pull hello-world@sha256:<digest>.
    3. Verify the image already exists (no download).

Summary

Key Takeaways

  • Use docker search to find images on Docker Hub.
  • docker pull downloads images; you can specify tags for versions.
  • docker images lists local images, with options to show digests and filter.
  • docker tag creates additional references to an image.
  • docker rmi and docker image prune remove unwanted images.
  • Official images are trusted, community images should be used with caution.

Check Your Understanding

  1. What command would you use to find an official MySQL image on Docker Hub?
  2. How do you pull a specific version of the Python image, say version 3.9?
  3. What is the difference between docker images and docker image ls?
  4. If you have an image tagged as myapp:latest and you want to tag it as yourname/myapp:1.0 for pushing to Docker Hub, what command do you use?
  5. Can you remove an image that is currently being used by a stopped container? What happens?
  6. What is the purpose of docker image prune -a?
Click to see answers
  1. docker search --filter "is-official=true" mysql
  2. docker pull python:3.9
  3. They are the same command; docker image ls is the newer syntax.
  4. docker tag myapp:latest yourname/myapp:1.0
  5. Docker will prevent removal. You'll need to remove the container first or use docker rmi -f (which removes the tag but leaves the container with <none> image).
  6. It removes all unused images (not just dangling ones without tags).

Additional Resources


Next Up

In the next lesson, we'll finally run containers in more depth: interactive vs. detached modes, managing container lifecycle, and inspecting containers. You'll put these images to work!