Learn Docker · 2/12

Working with Docker Images: The Blueprints

7 min read

When you start working with Docker, you hear two words constantly: Images and Containers. If containers are the running applications doing the heavy lifting, images are the exact blueprints used to create them.

In this guide, we will break down exactly what Docker images are, where to find them, how to manage them on your machine, and how to properly version them so your deployments never break unexpectedly.


1. What is a Docker Image? (Understanding the Blueprint)

The Problem

Before containers existed, sharing software was a nightmare. You would write an application on your laptop, send the code to a server, and it would crash. Why? Because the server had a different operating system, a missing library, or an older version of Python. The phrase "It works on my machine!" is the most famous excuse in software engineering history. We needed a way to package the application and its entire environment together so it behaves exactly the same everywhere.

The Solution: Docker Images

Definition: A Docker image is a read-only, immutable template that contains a set of instructions for creating a Docker container. It packages the application code, runtime, system tools, system libraries, and settings needed to make an application run.

Because images are immutable (read-only), once an image is created, it cannot be changed. If you need to update your code, you build a new image.

How it works conceptually

Imagine you are managing a massive car factory. You don't design a new car from scratch every time a customer places an order. Instead, you have a strictly defined, unchangeable schematic (the blueprint).

  • The schematic is the Docker Image. You can't drive it. It just sits there on a hard drive.
  • The factory machine is the Docker Engine.
  • The actual car that rolls off the assembly line is the Docker Container.

You can feed that single schematic into the factory machine and produce one car, or ten thousand identical cars. If a car breaks down, you don't fix it—you throw it away and instantly print a new one from the schematic.

Under the Hood: Layers

In the industry, you will often hear about "image layers." Docker images are not just one massive block of data. They are built in stacked layers using a Union File System.

  1. Base Layer: A minimal OS (like Ubuntu or Alpine).
  2. Dependency Layer: Installing Python or Node.js.
  3. Application Layer: Your specific source code.

If you update your source code, Docker only rebuilds the final application layer. It reuses the underlying OS and dependency layers from its cache. This is why pulling or building Docker images is incredibly fast after the first time.


2. Exploring the Docker Hub: Finding and Pulling Images

The Problem

If you had to manually install an operating system, configure networking, and install database binaries from scratch every time you needed a database, you would waste countless hours. You need a centralized place to download pre-configured environments.

The Solution: Docker Hub

Definition: Docker Hub is a centralized, cloud-based registry maintained by Docker where individuals and organizations host and share Docker images.

Docker Hub is to Docker what NPM is to Node.js, or pip is to Python.

Pulling Images

To download an image from Docker Hub to your local machine, we use the docker pull command. Let's say we need a web server. Nginx is an industry-standard web server.

bash
# Syntax: docker pull <image_name>
docker pull nginx

When you run this, Docker talks to Docker Hub, requests the nginx image, and downloads its layers one by one.

Industry Standards for Choosing Images

When browsing Docker Hub (usually via your web browser), you will see millions of images.

  1. Official Images: Always look for the "Official Image" badge. These are maintained by Docker and the original software creators (like the Node.js team or the PostgreSQL team).
  2. Verified Publishers: Images provided directly by commercial vendors (like Canonical or IBM).
  3. Avoid Random Images: Never run docker pull random_guy99/super-fast-db. It might work, or it might contain a cryptocurrency miner that sets your server on fire.

3. Managing Local Images: Inspecting and Removing

The Problem

Docker images are large. A standard Ubuntu image might be 70MB, but a fully loaded machine learning image could be 4GB. If you keep pulling images and building new ones, your hard drive will eventually groan under the weight of gigabytes of unused data, causing your machine to run out of space. Additionally, you often need to know exactly what is inside an image before you run it.

The Solution: Image Management Commands

Docker provides a specific management command structure to handle local images: docker image <command>.

Listing Local Images

To see what blueprints you currently have on your machine:

bash
docker image ls

(Note: You will frequently see engineers use the older command docker images. Both do exactly the same thing, but docker image ls is the modern standard.)

Output example:

text
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
nginx        latest    a6bd71f48f68   2 days ago     187MB
ubuntu       latest    ba6acccedd29   2 weeks ago    72.8MB

Inspecting Images

Sometimes you need to know what ports an image exposes or what environment variables it requires.

bash
docker image inspect nginx

This outputs a massive JSON object detailing everything about the image: its architecture, its entrypoint command, its layers, and its default configuration.

Removing Images

When you are done with an image, delete it.

bash
# Syntax: docker image rm <image_id_or_name>
docker image rm ba6acccedd29

(Industry shortcut: docker rmi <image_id> is widely used).

Note: You cannot delete an image if a running (or stopped) container is currently using it. You must destroy the car before you shred the blueprint.

The Nuke Button (Industry Favorite)

When your hard drive is 99% full and you just want to clean up everything you aren't actively using, you use the prune command.

bash
docker image prune -a

This safely deletes all unused images from your system. It is one of the most frequently used commands by developers on a Friday afternoon.


4. Image Tagging and Versioning Basics

The Problem

Software updates constantly. If you build your application relying simply on "Node.js", and tomorrow Node releases a major breaking update, your application will crash in production because Docker automatically grabbed the newest version. We need a way to lock down exact versions of software.

The Solution: Tags

Definition: A tag is a label applied to a Docker image to denote a specific version, variant, or release of that image.

If an image name is the repository (ubuntu), the tag is the specific version (22.04). They are separated by a colon :.

How Versioning Works Conceptual Flow

Imagine ordering parts for a critical machine. If you just send a purchase order for a "Bolt", the supplier looks at their inventory and sends whatever the most recent, standard bolt is. It might be 10mm, it might be 50mm. Your machine breaks. If your order says "Bolt:10mm-Hex-Steel", you get exactly what you need every single time.

The :latest Trap

When you run docker pull nginx, Docker implicitly translates that to docker pull nginx:latest.
The latest tag is just a default pointer to the most recently pushed image. In industry production environments, using :latest is considered a critical bad practice. It makes your deployments unpredictable.

Industry Standard: Pinning Versions

Always specify the exact version you tested your code against.

bash
docker pull node:18.17.0

The alpine Variant

When looking at tags, you will frequently see -alpine (e.g., node:18-alpine). Alpine Linux is a hyper-minimalist Linux distribution. While a standard Node image might be 1GB, the Alpine variant might only be 150MB. Industry standard practice dictates using -alpine tags for production whenever possible to speed up deployments and reduce security vulnerabilities (less code = fewer things to hack).

Tagging your own images

When you build your own images, you can apply tags to organize them.

bash
# Syntax: docker tag <source_image> <target_image>:<tag>
docker tag my-app:latest my-app:v1.0.3

Now, my-app:v1.0.3 is permanently locked to that specific state. Even if you update my-app:latest tomorrow, any server running v1.0.3 will safely run the older, stable code.


References & Further Reading

Challenge

You are a DevOps engineer responsible for managing Docker images for a new project. Your task is to write a series of commands to perform standard image management operations. Complete the script by writing the correct Docker command for each step described in the comments.

Expected result

Your solution should be a sequence of five Docker commands, each on a new line, corresponding to the five parts of the challenge.

shell
# Part 1
docker pull nginx:1.21.4-alpine

# Part 2
docker tag my-app:latest my-app:v1.5.0

# Part 3
docker image ls

# Part 4
docker image inspect nginx:1.21.4-alpine

# Part 5
docker image rm -f d9b1e61fd892
docker image prune -a
  • Part 1: Correctly combines the image name (nginx), a specific version tag (1.21.4), and the variant (-alpine).
  • Part 2: Uses docker tag with the source image (my-app:latest) and the new target image (my-app:v1.5.0).
  • Part 3: Uses the standard command docker image ls to list local images.
  • Part 4: Uses docker image inspect followed by the full image name and tag to get detailed information.
  • Part 5: The first command uses docker image rm with the -f flag for a forced removal of a specific image ID. The second command uses docker image prune -a to correctly remove all unused (not just dangling) images.
shell

Solve the challenge, then check it.

Ctrl+Enter

Course complete!

You've passed every challenge in this course.