Docker Images
Recipes for the Lunchbox
Think of Docker images as recipes. A recipe tells you what ingredients to use and how to prepare them. Once you follow the recipe, you get a dish. In Docker, the recipe is the image, and the dish is the container. Every time you want to serve the same dish, you don’t rewrite the recipe - you just cook it again.
Docker images are the blueprints that define what goes inside a container: operating system, libraries, dependencies, and application code. Understanding images is crucial because they are the starting point for every container you’ll ever run.
Images Foundations
1. What is a Docker Image?
- A Docker image is a read‑only template used to create containers.
- It contains everything needed to run an application: code, runtime, libraries, environment variables, and configuration files.
- Images are immutable - once built, they don’t change.
2. Image Layers
- Images are built in layers, each representing a set of changes.
- Example:
- Base layer: Operating system (e.g., Alpine Linux).
- Next layer: Installed packages (e.g., Python).
- Next layer: Application code.
- Layers are cached and reused, making builds faster and more efficient.
3. Union File System
- Docker uses a Union File System to stack layers together.
- Each layer is read‑only, but when combined, they form a complete filesystem for the container.
- Containers add a thin writable layer on top of the image.
4. Image Lifecycle
- Pull: Download an image from a registry (e.g., Docker Hub).
- Build: Create a new image using a Dockerfile.
- Run: Start a container from the image.
- Tag: Assign a version or name to the image.
- Push: Upload the image to a registry for sharing.
5. Registries
- Docker Hub: Default public registry with thousands of prebuilt images.
- Private Registries: Enterprises use them for secure, internal images.
- Other Registries: AWS ECR, Azure Container Registry, Google Artifact Registry.
Things to Remember
- Images are blueprints; containers are instances of those blueprints.
- Layers make images efficient and reusable.
- Registries are the distribution hubs for images.
Hands‑On Lab
Step 1: Pull an Image
docker pull nginx
- Downloads the
nginximage from Docker Hub.
Step 2: List Images
docker images
- Displays all images stored locally.
Step 3: Inspect an Image
docker inspect nginx
- Shows detailed metadata about the image, including layers.
Step 4: Run a Container from the Image
docker run -d -p 8080:80 nginx
- Starts an Nginx web server container.
Practice Exercise
- Pull three different images (
ubuntu,alpine,redis). - Use
docker imagesto compare their sizes. - Run containers from each image and observe differences in startup time and behavior.
- Inspect the
alpineimage to see why it’s so lightweight compared toubuntu.
Visual Learning Model
Registry (Docker Hub)
↓
Docker Image (layers)
↓
Container (image + writable layer)
The Hackers Notebook
Docker images are the foundation of containers. They are built in layers, stored in registries, and used to create containers. By mastering images, learners gain control over how applications are packaged, shared, and deployed.
