G
GuideDevOps
Lesson 3 of 13

Images & Containers

Part of the Docker tutorial series.

In Docker, everything revolves around Images and Containers. Understanding the difference between them is the first step to mastering containerization.

1. Docker Images (The Blueprint)

A Docker Image is a read-only, multi-layered file that includes everything needed to run an application: code, runtime, libraries, environment variables, and config files.

  • Immutable: Images never change once built.
  • Layered: Built using a series of layers to optimize storage and build speed.

List Local Images

Action:

docker images

Result:

REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
nginx         latest    605c77e624dd   2 weeks ago    141MB
ubuntu        22.04     27941809078c   3 weeks ago    77.8MB
python        3.10      a03666504309   4 weeks ago    915MB

2. Docker Containers (The Instance)

A Docker Container is a live, running instance of an image. If an Image is a class in programming, a Container is an object.

  • Ephemeral: Containers are designed to be temporary and replaceable.
  • Isolated: They run in their own namespace, separated from the host and other containers.

Run a Container

Action:

# Run a container from the nginx image
docker run -d --name my-web-server nginx

Result:

45eb2345a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z

(The output is the long unique ID of the running container)

List Running Containers

Action:

docker ps

Result:

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
45eb2345a1b2   nginx     "/docker-entrypoint.…"   10 seconds ago   Up 9 seconds    80/tcp    my-web-server

3. The Lifecycle: Image to Container

  1. Pull: Download an image from a registry (like Docker Hub).
  2. Run: Create a container from that image.
  3. Stop: Halt the running container processes.
  4. Remove: Delete the container (the image remains).

Stopping and Removing

Action:

docker stop my-web-server
docker rm my-web-server

Result:

my-web-server
my-web-server

Summary

  • Image: A static template (The Blueprint).
  • Container: A running process (The House).
  • One Image can spawn hundreds of identical Containers.