DAY 24 : ๐ณ Docker Images: The Building Blocks of Containers
Docker image for beginner

Hi, Iโm Ritesh ๐ Iโm on a mission to become a DevOps Engineer โ and Iโm learning in public every single day.With a full-time commitment of 8โ10 hours daily, Iโm building skills in: โ Linuxโ Git & GitHubโ Docker & Kubernetesโ AWS EC2, S3โ Jenkins, GitHub Actionsโ Terraform, Prometheus, Grafana I post daily blogs on Hashnode, push projects to GitHub, and stay active on LinkedIn and Twitter/X. Letโs connect, collaborate, and grow together ๐
#100DaysOfDevOps #LearningInPublic #DevOps

Master Docker images with hands-on practice and networking integration!
๐ Getting Started
This repository kicks off your journey into Docker Images, a cornerstone of containerization, as part of Day 24: Docker Networking in the Phase 2: Containers & Automation learning plan. Below are essential commands, practice tasks, and resources to get you started with Docker images, followed by a deeper dive into their structure and advanced practice.
๐ Key Commands
๐ผ
docker pull <image>: Downloads an image from Docker Hub (e.g.,nginx:latest).๐
docker images: Lists all images on your Docker host.๐
docker run <image>: Creates and starts a container from an image.๐
docker ps: Lists running containers.๐
docker ps -a: Lists all containers (running or stopped).๐งน
docker image prune -a: Removes all unused images (not referenced by any containers).
๐งช Practice: Getting Started
Install Docker:
Verify installation:
docker --version docker infoOutcome: Confirms Docker is installed and running (e.g.,
Docker version 24.0.5, no errors indocker info).
Pull and Run an Image:
Pull the Nginx image:
docker pull nginxRun an Nginx container:
docker run -d -p 8080:80 nginxWhy: Starts a container in detached mode (
-d) with port 8080 (host) mapped to 80 (container).Outcome: Open
http://localhost:8080in a browser to see the Nginx welcome page.
Terminal: Host terminal.
Explore Commands:
List images:
docker images- Outcome: Shows
nginx:latestwith itsIMAGE IDandSIZE.
- Outcome: Shows
List running containers:
docker ps- Outcome: Shows the Nginx container with a
CONTAINER ID.
- Outcome: Shows the Nginx container with a
Stop the container:
docker stop <container_id>- Replace
<container_id>with the ID fromdocker ps.
- Replace
Remove the container:
docker rm <container_id>Terminal: Host terminal.
๐ Resources
๐ณ Docker Get Started: Beginner-friendly guide to Docker basics.
๐ผ Docker Image Reference: Commands for managing images.
๐ Dockerfile Reference: Syntax for building images.
๐ Docker Hub: Public registry for sharing images.
๐ Building Docker Images: Step-by-step image creation.
๐ฅ Docker Images Explained: Visual guide to image layers.
๐ Overview
This repository dives deep into Docker Images, building on Day 24: Docker Networking in your 40-day Containers & Automation journey. Docker images are the blueprints for containers, and this lab explores their structure, creation, and usage, with practical tasks to solidify your skills. You'll work with the nginx image, build a custom image, and share it on Docker Hub, integrating networking concepts from Day 24.
Objective: Master Docker image fundamentals, build and run custom images, and prepare for Day 25 (Dockerfiles).
๐ What Are Docker Images?
Docker images are lightweight, portable, and immutable templates that package everything needed to run an application:
๐จ Application Code: Your app (e.g., a Python Flask app).
๐ Dependencies: Libraries and runtimes (e.g.,
pip install flask).๐พ OS Base: A minimal OS layer (e.g.,
alpineorubuntu:20.04).โ๏ธ Configuration: Environment variables, config files, or default commands.
Key Characteristics
๐ Immutable: Images are read-only; changes create new images.
๐ฅ Layered: Built from cached layers for efficiency (e.g., base OS, app code, dependencies).
๐ Portable: Run anywhere Docker is installed, via registries like Docker Hub.
๐ท Tagged: Named with tags (e.g.,
nginx:latest) for versioning.
How They Work
๐ Built with Dockerfiles: Define steps to create an image (e.g.,
FROM,COPY,RUN).๐ Stored Locally or in Registries: Use
docker pullto download ordocker pushto share.๐ Run as Containers:
docker runcreates a container with a writable layer on top of the image.๐ Networking Connection: Images define services (e.g., Nginx on port 80), which interact with Dockerโs networks (e.g., Day 24โs port mapping).
๐ Prerequisites
Ensure you have:
๐ณ Docker installed and running (
docker --version,docker info).๐ A web browser for testing web servers.
โ๏ธ A text editor (e.g., VS Code) for creating files.
๐ก A Docker Hub account for Task 4 (e.g.,
ritesh355).๐ฅ One terminal (Linux/Mac/WSL2 or Windows PowerShell). Open a second terminal for troubleshooting if needed.
๐งช Advanced Practice Tasks
These hands-on tasks build on Day 24โs Nginx containers and networking, helping you master Docker images beyond the basics.
๐งช Task 1: Explore Docker Images
Goal: Inspect the nginx image used in Day 24.
Clean Up Existing Containers:
docker stop web1 web2 web3 docker rm web1 web2 web3Why: Clears containers from Day 24 for a fresh start.
Outcome: No containers listed (
docker ps -a).Terminal: Host terminal.
List Images:
docker imagesWhy: Shows all images, including
nginx.Outcome: Displays
nginx:latestwith itsIMAGE IDandSIZE(~140MB).Troubleshooting: If missing, run
docker pull nginx:latest.
Inspect the Nginx Image:
docker image inspect nginx:latestWhy: Reveals metadata (layers, ports, commands).
Outcome: JSON output with
ExposedPorts(80/tcp) andCmd(["nginx", "-g", "daemon off;"]).Terminal: Host terminal.
View Image Layers:
docker history nginx:latestWhy: Shows the imageโs layers (e.g., base OS, Nginx installation).
Outcome: Lists layers with commands and sizes.
Terminal: Host terminal.
๐งช Task 2: Run and Test Containers
Goal: Run containers from nginx and test connectivity (like Day 24).
Run Containers:
docker run -d --name web1 nginx docker run -d --name web2 nginxWhy: Creates
web1andweb2on the default bridge network.Outcome: Containers running (
docker ps).Terminal: Host terminal.
Check Network IPs:
docker network inspect bridgeWhy: Confirms IPs (e.g.,
172.17.0.2,172.17.0.3).Outcome: JSON shows
web1andweb2underContainers.Terminal: Host terminal.
Test Connectivity:
Access
web1Shell:docker exec -it web1 /bin/bashInstall Tools:
apt-get update && apt-get install -y iputils-ping curlPing
web2:ping 172.17.0.3- Outcome: Successful replies (e.g.,
64 bytes from 172.17.0.3).
- Outcome: Successful replies (e.g.,
Ping by Name (expect failure):
ping web2- Outcome: Fails with โName or service not knownโ.
Exit Shell:
exitTerminal: Host terminal, then
web1shell, then back to host.
Run
web3with Port Mapping:docker run -d --name web3 -p 8080:80 nginxWhy: Exposes Nginx on port 8080.
Outcome: Open
http://localhost:8080to see the Nginx welcome page.Terminal: Host terminal.
๐งช Task 3: Build a Custom Image
Goal: Create a custom Nginx image with a static HTML page.
Create Project Directory:
mkdir my-image && cd my-image- Terminal: Host terminal.
Create
index.html:<!DOCTYPE html> <html> <head> <title>My Custom Image</title> </head> <body> <h1>Hello from my custom Docker image!</h1> </body> </html>Why: A custom page for Nginx to serve.
Terminal: Host terminal (use a text editor).
Create
Dockerfile:FROM nginx:alpine COPY index.html /usr/share/nginx/html/index.html EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]Why: Defines a custom image based on
nginx:alpine.Terminal: Host terminal.
Build the Image:
docker build -t my-custom-nginx:latest .Outcome: Image built, visible in
docker images.Terminal: Host terminal.
Run the Container:
docker run -d --name custom-web -p 8081:80 my-custom-nginxOutcome: Open
http://localhost:8081to see โHello from my custom Docker image!โ.Terminal: Host terminal.
Inspect the Image:
docker image inspect my-custom-nginx:latestOutcome: Shows layers and metadata, including
index.html.Terminal: Host terminal.
๐งช Task 4: Push and Pull from Docker Hub
Goal: Share your custom image.
Create a Docker Hub Repository:
- Sign up at hub.docker.com and create a repository (e.g.,
ritesh355/my-custom-nginx).
- Sign up at hub.docker.com and create a repository (e.g.,
Tag the Image:
docker tag my-custom-nginx:latest ritesh355/my-custom-nginx:latest- Terminal: Host terminal.
Log in to Docker Hub:
docker loginOutcome: โLogin Succeededโ after entering credentials.
Terminal: Host terminal.
Push the Image:
docker push ritesh355/my-custom-nginx:latestOutcome: Image uploaded to Docker Hub.
Terminal: Host terminal.
Test Pulling:
docker rmi ritesh355/my-custom-nginx:latest my-custom-nginx:latest docker pull ritesh355/my-custom-nginx:latest docker run -d --name test-web -p 8082:80 ritesh355/my-custom-nginx:latestOutcome: Open
http://localhost:8082to see the custom page.Terminal: Host terminal.
๐งช Task 5: Clean Up
docker stop web1 web2 web3 custom-web test-web
docker rm web1 web2 web3 custom-web test-web
docker image prune -a -f
Why: Removes containers and unused images to free resources.
Terminal: Host terminal.
๐ Key Takeaways
๐ผ Images as Blueprints: Package apps, dependencies, and OS for portability.
๐ฅ Layered Efficiency: Cached layers optimize builds and storage.
๐ Networking Integration: Images define services (e.g., Nginx on port 80) that interact with Dockerโs networks (Day 24).
๐ Custom Images: Building images (Task 3) prepares you for Day 25โs Dockerfiles.
๐ Next Steps: Ready for Flask + Nginx + MongoDB project (Day 31โ33).
๐ Troubleshooting
Image Missing: Run
docker pull nginx:latestifnginxisnโt found.Build Fails: Check Dockerfile syntax and file paths (e.g.,
index.html).Port Conflicts: Verify ports 8080โ8082 are free (
netstat -tulnon Linux).Push Fails: Ensure Docker Hub login and correct repository name (
ritesh355/my-custom-nginx).New Terminal: Use a second terminal for
docker psordocker network inspectwhile in a container shell.
๐ Additional Resources
๐ณ Docker Networking Overview: Complements Day 24โs networking tasks.
๐ฅ Docker Networking Basics: Visualizes container communication.
๐ Best Practices for Docker Images: Tips for efficient images.
๐ Understanding Docker Layers: Deep dive into caching.
๐งน Docker Cleanup Guide: Housekeeping tips.
๐ Next Steps
๐ Review: Repeat Tasks 3โ4 to master image creation.
๐ Prepare: Study Dockerfiles for Day 25.
๐ก Extend: Build a Python-based image (e.g., Flask) for your project
๐ Connect with Me
Built as part of a 100-day Containers & Automation journey. Letโs containerize the world! ๐ข



