Module 3: Docker Images
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
DockerfileBest 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
Dockerfilecreates 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:
Login to Docker Hub
docker loginTag the image
docker tag my-python-app devopslaunchpad/my-python-app:v1Push 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
