Image Fundamentals

Loading concept...

Docker Images: The LEGO Blocks of Containers 🧱


The Story: Building with Magical Recipe Cards

Imagine you want to bake the perfect chocolate cake. Instead of memorizing every step, you have a magical recipe card. This card doesn’t just tell you how to bakeβ€”it actually contains all the ingredients inside it!

That’s exactly what a Docker Image is. It’s a recipe card that contains everything your app needs to runβ€”the code, tools, settings, and all the little pieces. Just hand it to Docker, and boom! Your app runs perfectly, every single time.


πŸ§… Image Layers: The Onion Structure

Think of a Docker image like a stack of pancakes. Each pancake is a layer.

How Layers Work

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Your App Code         β”‚  ← Top layer (you add this)
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   Python Libraries      β”‚  ← Middle layer
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   Python Runtime        β”‚  ← Another layer
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   Ubuntu Base           β”‚  ← Bottom layer (foundation)
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Why layers are magical:

  • Speed: Docker reuses unchanged layers. If you only change your code, Docker only rebuilds that ONE layer.
  • Sharing: Multiple images can share the same bottom layers, saving space.

Real Example

FROM python:3.11    # Layer 1
COPY app.py /app/   # Layer 2
RUN pip install flask  # Layer 3

Each line = one layer. Simple!


🏷️ Image Tagging and Versioning

Tags are like labels on folders. They help you know which version of your image you’re using.

The Format

repository:tag

Examples:

nginx:latest
python:3.11
myapp:v2.0

What If You Don’t Add a Tag?

Docker assumes you mean :latest. But beware! latest doesn’t always mean β€œnewest”—it just means β€œdefault.”

graph TD A[myapp:v1.0] --> B[First Release] C[myapp:v2.0] --> D[Bug Fixes] E[myapp:latest] --> F[Points to v2.0]

Best Practice

Always use specific version tags in production:

# Good
docker pull python:3.11.4

# Risky
docker pull python:latest

πŸ” Image Digest vs Tag: The Fingerprint

Here’s a secret: tags can lie. Someone can push a completely different image with the same tag!

But the digest? That’s the image’s fingerprint. It never changes.

What’s a Digest?

A long, unique ID like this:

sha256:abc123def456...

Tag vs Digest

Feature Tag Digest
Can change? βœ… Yes ❌ Never
Human-readable? βœ… Yes ❌ Hard to remember
Best for security? ❌ No βœ… Yes

Using Digests

# Pull by digest (guaranteed exact image)
docker pull nginx@sha256:abc123...

Think of it like this:

  • Tag = β€œThat red car” (could be any red car)
  • Digest = License plate number (one specific car)

⬇️ Pulling Images: Getting Your Recipe Cards

Pulling means downloading an image from a registry (like Docker Hub).

Basic Pull

docker pull nginx

This grabs nginx:latest from Docker Hub.

Pull Specific Version

docker pull python:3.11-slim

Pull from Other Registries

# From GitHub Container Registry
docker pull ghcr.io/owner/image:tag

# From AWS ECR
docker pull 123456.dkr.ecr.us-east-1.amazonaws.com/myapp:v1

What Happens When You Pull?

graph TD A[Your Computer] -->|docker pull| B[Docker Hub] B -->|Downloads layers| A A -->|Stores locally| C[Local Image Cache]

⬆️ Pushing Images: Sharing Your Creations

Pushing means uploading your image to a registry.

Step-by-Step

1. Login first:

docker login

2. Tag your image properly:

docker tag myapp:v1 username/myapp:v1

3. Push it up:

docker push username/myapp:v1

Why Tag Before Push?

Docker Hub expects the format:

username/imagename:tag

If your image is just myapp:v1, Docker doesn’t know where to push it!


πŸ“‹ Listing Images: See What You Have

Your computer stores images locally. Let’s see them!

Basic List

docker images

Output:

REPOSITORY    TAG       IMAGE ID       SIZE
nginx         latest    abc123def     142MB
python        3.11      def456ghi     912MB
myapp         v1.0      ghi789jkl     85MB

Filter by Name

docker images nginx

Show All (Including Intermediate)

docker images -a

Just Show Image IDs

docker images -q

πŸ—‘οΈ Removing Images: Cleaning Up

Images take up space. Clean up what you don’t need!

Remove One Image

docker rmi nginx:latest

Remove by Image ID

docker rmi abc123def

Force Remove (Even If Used)

docker rmi -f nginx:latest

Remove ALL Unused Images

docker image prune

Nuclear Option: Remove Everything

docker image prune -a

⚠️ Warning: This removes ALL images not used by containers!


πŸ’Ύ Saving and Loading Images: Offline Magic

What if you need to move an image to a computer without internet? Save it as a file!

Saving an Image

docker save -o myapp.tar myapp:v1

This creates a myapp.tar file containing the entire image.

Loading an Image

On the other computer:

docker load -i myapp.tar

Done! The image is now available locally.

Save Multiple Images

docker save -o backup.tar nginx:latest python:3.11
graph LR A[Computer A] -->|docker save| B[myapp.tar] B -->|USB/Network| C[Transfer] C -->|docker load| D[Computer B]

When to Use Save/Load?

  • Air-gapped environments (no internet)
  • Backup images before updates
  • Share images without a registry

🎯 Quick Command Reference

Task Command
Pull image docker pull image:tag
Push image docker push user/image:tag
List images docker images
Remove image docker rmi image:tag
Save to file docker save -o file.tar image:tag
Load from file docker load -i file.tar

🌟 You Did It!

You now understand Docker Imagesβ€”the building blocks of containers. They’re like magical recipe cards that:

  • Stack in layers (reusable, efficient)
  • Have tags for versions (human-friendly)
  • Have digests for security (machine-precise)
  • Can be pulled, pushed, saved, and loaded

Next time you docker pull, you’ll know exactly what’s happening behind the scenes! πŸš€

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.