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.
2.1. Using docker search
The docker search command lets you find images on Docker Hub.
Syntax:
docker search [OPTIONS] TERMExample: Search for Nginx images:
docker search nginxSample 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:
docker search --filter "is-official=true" nginx2.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:
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.
docker pull ubuntuThis 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:latest3.2. Pulling a Specific Tag
Tags allow you to specify a particular version of an image. For example, to pull Ubuntu 22.04:
docker pull ubuntu:22.04Common 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:
docker pull ubuntu@sha256:2e863c7b3c38b04...b3cTo 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:
docker pull myregistry.local:5000/myapp:latest4. Listing Images
After pulling, you can see which images are stored locally.
4.1. docker images or docker image ls
Both commands list images.
docker imagesOutput 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 141MBColumns:
- 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:
docker images --digestsThis adds a DIGEST column.
Filter images: Show only images with a specific repository or tag:
docker images ubuntu
docker images --filter "reference=ubuntu:22.04"4.2. Inspecting an Image
To see detailed metadata (JSON format), use:
docker image inspect ubuntu:latestThis 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:
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]Example: Tag the ubuntu:latest image as myubuntu:v1:
docker tag ubuntu:latest myubuntu:v1Now 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:
docker tag myapp:latest yourusername/myapp:1.0Then 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:
docker rmi [OPTIONS] IMAGE [IMAGE...]Remove by repository:tag:
docker rmi myubuntu:v1Remove by IMAGE ID:
docker rmi ba6acccedd29WARNING
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:
docker rmi -f ba6acccedd29Warning: 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):
docker image pruneAdd -a to remove all unused images (not just dangling):
docker image prune -aYou'll be prompted to confirm. Use -f to skip confirmation.
7. Official vs. Community Images
| Type | Description | Examples |
|---|---|---|
| Official Images | Maintained by Docker or software authors. Trusted, up-to-date. | ubuntu, nginx, mysql |
| Community Images | Uploaded 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
- Search for "alpine" images on Docker Hub using
docker search. - Identify an official alpine image and a community one with many stars.
- Visit the Docker Hub page for the official alpine image (https://hub.docker.com/_/alpine) and note the available tags.
- Search for "alpine" images on Docker Hub using
[ ] Task 2: Pull Multiple Images
- Pull the following images:
alpine:latestalpine:3.18nginx:alpine(a small Nginx image)hello-world(if not already)
- Observe the download output. Notice how layers are reused if they already exist (e.g., alpine base layers).
- Pull the following images:
[ ] Task 3: List and Inspect
- List all images with
docker images. - Show digests with
docker images --digests. - Inspect the alpine image:
docker image inspect alpine:latest. Look at the "Env", "Cmd", and "Architecture" fields. - View the history of the alpine image:
docker history alpine:latest. Notice the small layer sizes.
- List all images with
[ ] Task 4: Tagging
- Tag the
alpine:latestimage asmyalpine:test. - Verify the new tag appears in
docker images(same IMAGE ID). - Tag the same image as
myalpine:1.0.
- Tag the
[ ] Task 5: Remove Images
- Remove the
myalpine:testtag:bash(The underlying image remains because it has other tags.)docker rmi myalpine:test - Try to remove the
alpine:latestimage using its IMAGE ID. If any containers are using it, it will fail. (We'll clean up containers later.) - Run
docker image prune -aand typeywhen prompted. Observe which images are removed.
- Remove the
[ ] Task 6: Pull by Digest (Optional)
- Find the digest of the
hello-worldimage:docker images --digests hello-world. - Pull it again by digest:
docker pull hello-world@sha256:<digest>. - Verify the image already exists (no download).
- Find the digest of the
Summary
Key Takeaways
- Use
docker searchto find images on Docker Hub. docker pulldownloads images; you can specify tags for versions.docker imageslists local images, with options to show digests and filter.docker tagcreates additional references to an image.docker rmianddocker image pruneremove unwanted images.- Official images are trusted, community images should be used with caution.
Check Your Understanding
- What command would you use to find an official MySQL image on Docker Hub?
- How do you pull a specific version of the Python image, say version 3.9?
- What is the difference between
docker imagesanddocker image ls? - If you have an image tagged as
myapp:latestand you want to tag it asyourname/myapp:1.0for pushing to Docker Hub, what command do you use? - Can you remove an image that is currently being used by a stopped container? What happens?
- What is the purpose of
docker image prune -a?
Click to see answers
docker search --filter "is-official=true" mysqldocker pull python:3.9- They are the same command;
docker image lsis the newer syntax. docker tag myapp:latest yourname/myapp:1.0- 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). - It removes all unused images (not just dangling ones without tags).
Additional Resources
- Docker pull reference
- Docker images reference
- Docker tag reference
- Docker rmi reference
- Docker image prune reference
- Docker Hub Quickstart
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!