Skip to main content

Command Palette

Search for a command to run...

Module 3: Docker Images

Updated
3 min readView as Markdown

In the previous modules, we explored Docker fundamentals and learned how to run containers with essential commands. In this module, we’ll focus on Docker Images—the building blocks of containers.

By the end of this article, you’ll understand:

  • What Docker Images are (and how layers work)

  • How to pull images from Docker Hub

  • How to build your own images using a Dockerfile

  • Best practices for optimizing images

  • How to push images to Docker Hub


🔹 1. What is a Docker Image?

A Docker image is a lightweight, read-only template used to create containers.

  • Think of an image as a blueprint (like a recipe).

  • A container is the running instance of that blueprint (like a cooked dish).

🧩 Layers in Docker Images

Docker uses a layered architecture:

  • Each instruction in a Dockerfile creates a new layer.

  • Layers are cached, making builds faster and more efficient.

  • If nothing changes in a layer, Docker reuses it.

👉 Example:

FROM ubuntu:20.04     # Base layer
RUN apt-get update    # Adds a new layer
RUN apt-get install -y python3  # Another layer
COPY . /app           # New layer with your code
CMD ["python3", "app.py"]  # Defines the default command

🔹 2. Pulling Images from Docker Hub

Docker Hub is the default registry for Docker images.

To pull an image:

docker pull nginx

This will download the latest nginx image.

To specify a version:

docker pull mysql:8.0

You can check downloaded images with:

docker images

🔹 3. Building Custom Images with Dockerfile

A Dockerfile defines how to build your custom image.

Example: Simple Python App

# Use base image
FROM python:3.9-slim

# Set working directory
WORKDIR /app

# Copy code
COPY app.py /app

# Install dependencies (if requirements.txt exists)
# COPY requirements.txt .
# RUN pip install -r requirements.txt

# Run the application
CMD ["python", "app.py"]

To build the image:

docker build -t my-python-app .

To run it:

docker run --name python-container my-python-app

🔹 4. Optimizing Images

Large images → slow builds, longer deploy times. Here are best practices:

✅ Use official lightweight base images (e.g., python:3.9-slim, alpine)
✅ Combine commands to reduce layers:

RUN apt-get update && apt-get install -y python3

✅ Use .dockerignore to skip unnecessary files (like .git, node_modules)
✅ Multi-stage builds for production-ready small images


🔹 5. Pushing Images to Docker Hub

To share your image with others:

  1. Login to Docker Hub

     docker login
    
  2. Tag the image

     docker tag my-python-app devopslaunchpad/my-python-app:v1
    
  3. Push the image

     docker push devopslaunchpad/my-python-app:v1
    

Now anyone can run:

docker pull devopslaunchpad/my-python-app:v1

🚀 Quick Recap

In this module, you learned:
✅ What Docker Images are & how layers work
✅ How to pull official images from Docker Hub
✅ How to build custom images with Dockerfile
✅ Best practices for optimizing images
✅ How to push your image to Docker Hub


Mastering Docker: The Complete Guide

Part 3 of 14

This series takes you on a journey from Docker basics to advanced real-world applications. You’ll learn everything from running your first container, building images, and managing networks, to multi-container setups, CI/CD pipelines.

Up next

Module 4: Docker Networking

In the previous module, we learned about Docker Images, how to build and optimize them, and push them to Docker Hub. Now, let’s focus on how containers communicate—a critical part of running real-world applications. By the end of this module, you’ll ...

More from this blog

D

DevOps Launchpad - Learn DevOps,Cloud, and Kubernetes

26 posts