DAY 24 : 🐳 Docker Images: The Building Blocks of Containers

Ritesh SinghRitesh Singh
8 min read

Docker Logo

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

  1. Install Docker:

    • Verify installation:

        docker --version
        docker info
      
    • Outcome: Confirms Docker is installed and running (e.g., Docker version 24.0.5, no errors in docker info).

  2. Pull and Run an Image:

    • Pull the Nginx image:

        docker pull nginx
      
    • Run an Nginx container:

        docker run -d -p 8080:80 nginx
      
      • Why: Starts a container in detached mode (-d) with port 8080 (host) mapped to 80 (container).

      • Outcome: Open http://localhost:8080 in a browser to see the Nginx welcome page.

    • Terminal: Host terminal.

  3. Explore Commands:

    • List images:

        docker images
      
      • Outcome: Shows nginx:latest with its IMAGE ID and SIZE.
    • List running containers:

        docker ps
      
      • Outcome: Shows the Nginx container with a CONTAINER ID.
    • Stop the container:

        docker stop <container_id>
      
      • Replace <container_id> with the ID from docker ps.
    • Remove the container:

        docker rm <container_id>
      
    • Terminal: Host terminal.

📚 Resources


🚀 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., alpine or ubuntu: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 pull to download or docker push to share.

  • 🏃 Run as Containers: docker run creates 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.

  1. Clean Up Existing Containers:

     docker stop web1 web2 web3
     docker rm web1 web2 web3
    
    • Why: Clears containers from Day 24 for a fresh start.

    • Outcome: No containers listed (docker ps -a).

    • Terminal: Host terminal.

  2. List Images:

     docker images
    
    • Why: Shows all images, including nginx.

    • Outcome: Displays nginx:latest with its IMAGE ID and SIZE (~140MB).

    • Troubleshooting: If missing, run docker pull nginx:latest.

  3. Inspect the Nginx Image:

     docker image inspect nginx:latest
    
    • Why: Reveals metadata (layers, ports, commands).

    • Outcome: JSON output with ExposedPorts (80/tcp) and Cmd (["nginx", "-g", "daemon off;"]).

    • Terminal: Host terminal.

  4. View Image Layers:

     docker history nginx:latest
    
    • Why: 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).

  1. Run Containers:

     docker run -d --name web1 nginx
     docker run -d --name web2 nginx
    
    • Why: Creates web1 and web2 on the default bridge network.

    • Outcome: Containers running (docker ps).

    • Terminal: Host terminal.

  2. Check Network IPs:

     docker network inspect bridge
    
    • Why: Confirms IPs (e.g., 172.17.0.2, 172.17.0.3).

    • Outcome: JSON shows web1 and web2 under Containers.

    • Terminal: Host terminal.

  3. Test Connectivity:

    • Access web1 Shell:

        docker exec -it web1 /bin/bash
      
    • Install Tools:

        apt-get update && apt-get install -y iputils-ping curl
      
    • Ping web2:

        ping 172.17.0.3
      
      • Outcome: Successful replies (e.g., 64 bytes from 172.17.0.3).
    • Ping by Name (expect failure):

        ping web2
      
      • Outcome: Fails with “Name or service not known”.
    • Exit Shell:

        exit
      
    • Terminal: Host terminal, then web1 shell, then back to host.

  4. Run web3 with Port Mapping:

     docker run -d --name web3 -p 8080:80 nginx
    
    • Why: Exposes Nginx on port 8080.

    • Outcome: Open http://localhost:8080 to 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.

  1. Create Project Directory:

     mkdir my-image && cd my-image
    
    • Terminal: Host terminal.
  2. 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).

  3. 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.

  4. Build the Image:

     docker build -t my-custom-nginx:latest .
    
    • Outcome: Image built, visible in docker images.

    • Terminal: Host terminal.

  5. Run the Container:

     docker run -d --name custom-web -p 8081:80 my-custom-nginx
    
    • Outcome: Open http://localhost:8081 to see “Hello from my custom Docker image!”.

    • Terminal: Host terminal.

  6. Inspect the Image:

     docker image inspect my-custom-nginx:latest
    
    • Outcome: Shows layers and metadata, including index.html.

    • Terminal: Host terminal.

🧪 Task 4: Push and Pull from Docker Hub

Goal: Share your custom image.

  1. Create a Docker Hub Repository:

    • Sign up at hub.docker.com and create a repository (e.g., ritesh355/my-custom-nginx).
  2. Tag the Image:

     docker tag my-custom-nginx:latest ritesh355/my-custom-nginx:latest
    
    • Terminal: Host terminal.
  3. Log in to Docker Hub:

     docker login
    
    • Outcome: “Login Succeeded” after entering credentials.

    • Terminal: Host terminal.

  4. Push the Image:

     docker push ritesh355/my-custom-nginx:latest
    
    • Outcome: Image uploaded to Docker Hub.

    • Terminal: Host terminal.

  5. 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:latest
    
    • Outcome: Open http://localhost:8082 to 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:latest if nginx isn’t found.

  • Build Fails: Check Dockerfile syntax and file paths (e.g., index.html).

  • Port Conflicts: Verify ports 8080–8082 are free (netstat -tuln on Linux).

  • Push Fails: Ensure Docker Hub login and correct repository name (ritesh355/my-custom-nginx).

  • New Terminal: Use a second terminal for docker ps or docker network inspect while in a container shell.


📚 Additional Resources


🌟 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! 🚢

0
Subscribe to my newsletter

Read articles from Ritesh Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ritesh Singh
Ritesh Singh

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