Skip to main content
Docker Basics Tutorial
CHAPTER 03 Beginner

Docker Images and Containers

Updated: May 15, 2026
20 min read

# CHAPTER 3

Docker Images and Containers

1. Introduction

In Chapter 1, we learned that a Docker Image is the blueprint, and a Docker Container is the live, running application. But where do these blueprints come from? Do you have to build them from scratch? No! The true power of Docker lies in its massive, open-source community. In this chapter, we will explore Docker Hub, the global repository of pre-built images, and learn the fundamental lifecycle of pulling images and spinning them up into live containers.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Navigate Docker Hub to find official software images.
  • Understand Image Tags and versioning.
  • Execute the docker pull command.
  • Execute the docker run command with basic flags.
  • Understand the lifecycle of a container (Starting, Stopping, Removing).

3. Beginner-Friendly Explanation

Imagine an App Store for servers.
  • Docker Hub is the App Store. It is a massive cloud website where companies like Google, Microsoft, and Ubuntu upload perfect, working blueprints of their software.
  • Pulling: When you want a database, you do not write a database from scratch. You "Pull" (download) the official MySQL blueprint from Docker Hub to your laptop.
  • Running: You tell Docker to take that blueprint and "Run" it. Docker instantly materializes a live, working MySQL database on your computer.

4. Docker Hub and Official Images

If you go to hub.docker.com and search for "Ubuntu" or "Node" or "Nginx", you will see thousands of results. Always look for the "Docker Official Image" badge. This means the image was built and verified directly by the company that created the software (e.g., The official Node.js team maintains the Node image). Never download a random, unverified image for a critical application, as it could contain malware.

5. Image Tags (Versioning)

When you download an image, you must specify exactly which version you want using a Tag. Tags are appended to the image name with a colon :.
  • docker pull node:18 (Downloads Node.js version 18).
  • docker pull node:20.5.0 (Downloads the highly specific version 20.5.0).
  • docker pull node:latest (Downloads whatever the newest version currently is).

*Warning:* Avoid using the :latest tag in production. If your application relies on latest, and Node releases a breaking update tomorrow, your app will pull the new update and crash! Always lock your images to a specific version number.

6. The docker run Lifecycle

When you type docker run, Docker performs several actions invisibly:
  1. 1. It checks your local laptop to see if you already have the image downloaded.
  1. 2. If it is missing, it reaches out to Docker Hub and automatically pulls it.
  1. 3. It creates a brand new container from the image.
  1. 4. It allocates a read-write file system and a virtual network interface.
  1. 5. It starts the software inside the container.

7. Mini Project: Run an Nginx Web Server

Let's pull an official web server image and run it.

Step-by-Step Tutorial:

  1. 1. Open your terminal. Let's explicitly download the image first:

bash
1
docker pull nginx:alpine

*(Note: "alpine" is a special tag that means "A super tiny, minimal version of Linux". Alpine images are usually less than 10MB!)*

  1. 2. Verify the image is on your computer:

bash
1
docker images

*(You will see a list showing nginx, the tag alpine, and its file size).*

  1. 3. Run the container:

bash
1
docker run --name my-web-server -p 8080:80 -d nginx:alpine
  • --name my-web-server: Gives the container a friendly name so we don't have to memorize a random ID string.
  • -p 8080:80: Port Mapping! It connects Port 8080 on your laptop to Port 80 inside the container.
  • -d: Detached mode. It runs the server in the background so you can keep using your terminal.
  1. 4. Open your browser and go to http://localhost:8080. You will see the Nginx welcome page!
  1. 5. Stop the container:
bash
1
docker stop my-web-server

8. Real-World Scenarios

A senior developer asks you to test an application against PostgreSQL version 12, and then test it again against PostgreSQL version 15. Without Docker, you would have to spend hours uninstalling and reinstalling conflicting database software on your laptop. With Docker, you simply run docker run postgres:12, test it, stop it, and then run docker run postgres:15. You can even run them simultaneously on different ports!

9. Best Practices

  • Clean Up After Yourself: Every time you type docker run, a brand new container is born. If you stop a container, it doesn't disappear; it just goes to sleep, taking up hard drive space. To automatically delete a container the exact second it stops, add the --rm flag to your run command: docker run --rm nginx:alpine.

10. Security Tips

  • Image Scanning: Before pushing an image to a production server, always scan it for known vulnerabilities. Docker provides a built-in command: docker scout cves <image-name>, which will check the image against a global database of known hacker exploits.

11. Common Mistakes

  • Port Conflicts: If you run an Nginx container on port 8080:80, and then try to run a second Nginx container on port 8080:80, Docker will throw a massive error. Two applications cannot share the exact same host port. You must change the left side of the map (e.g., -p 8081:80 for the second container).

12. Exercises

  1. 1. What does the : symbol represent when defining a Docker Image?
  1. 2. Explain the difference between running a container in "Foreground" mode vs. "Detached" (-d) mode.

13. FAQs

Q: Can I edit files inside a running container? A: Yes, but you shouldn't! While you can log into a container and edit an HTML file, the second the container stops and is deleted, your edits are gone forever. Containers are immutable. We will learn how to handle permanent data in Chapter 6.

14. Interview Questions

  • Q: Explain the purpose of Docker Hub. How do "Official Images" differ from community-uploaded images?
  • Q: Describe the step-by-step internal process that occurs when a developer executes the docker run command for an image that does not currently exist on their local machine.

15. Summary

In Chapter 3, we tapped into the global ecosystem of Docker Hub. We learned how to identify official, secure software images and the critical importance of locking our applications to specific Image Tags to prevent breaking changes. We manually pulled a lightweight Alpine Nginx image and executed the docker run command, utilizing Port Mapping to connect the isolated container to our local web browser, bringing our first web server to life.

16. Next Chapter Recommendation

We know how to start a container, but how do we see its logs, check its health, or destroy it? Proceed to Chapter 4: Docker CLI Commands Essentials.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·