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 imagesResult:
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 915MB2. 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 nginxResult:
45eb2345a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z(The output is the long unique ID of the running container)
List Running Containers
Action:
docker psResult:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
45eb2345a1b2 nginx "/docker-entrypoint.…" 10 seconds ago Up 9 seconds 80/tcp my-web-server3. The Lifecycle: Image to Container
- Pull: Download an image from a registry (like Docker Hub).
- Run: Create a container from that image.
- Stop: Halt the running container processes.
- Remove: Delete the container (the image remains).
Stopping and Removing
Action:
docker stop my-web-server
docker rm my-web-serverResult:
my-web-server
my-web-serverSummary
- Image: A static template (The Blueprint).
- Container: A running process (The House).
- One Image can spawn hundreds of identical Containers.