Demystifying Images & Containers
To master Docker, you must understand its two fundamental building blocks: the static blueprints that define your application, and the dynamic, running environments they spawn.
Docker Images
A Docker image is like a blueprint. It contains absolutely everything needed to run an application without relying on the host system's configuration.
Read-Only & Layered
Images are completely read-only and built in stacked layers. This layered approach makes them fast to pull, incredibly efficient to store, and highly cacheable.
What's Inside?
An image packs OS libraries (like Debian/Alpine), your specific runtime (Java, Python, PHP), your application code, and your environment configurations.
Docker Containers
Container Lifecycle
- start Spawns the container, attaching a read-write layer over the image.
- stop Gracefully halts the running processes inside the container.
- restart Stops and immediately starts the container back up.
- delete Destroys the container and its temporary read-write data layer.
A container is a running instance of an image. When you start an image, Docker creates a container and adds a writable layer on top, bringing the application to life.
Essential CLI Operations
Understanding the difference between create, start, and run is crucial. run is simply a combination of create (making the container) followed immediately by start (executing it).
terminal
# Image Commands
$ docker image ls → List all images
$ docker pull ubuntu → Download an image from Hub
$ docker history ubuntu → View build layers
$ docker rmi ubuntu → Remove an image
# Container Run Commands
$ docker create ubuntu → Creates, but does NOT start
$ docker start <id> → Starts a created container
$ docker run -d nginx → Create + Start in background
# Container Management
$ docker ps → List running containers
$ docker ps -a → List all (running + stopped)
$ docker logs <id> → View container logs
$ docker exec -it <id> /bin/bash → Open interactive shell
$ docker stop <id> → Stop a container
$ docker rm <id> → Remove a containerThe Key Differences
| Attribute | Docker Image | Docker Container |
|---|---|---|
| State | Static, read-only | Running, read-write layer added |
| Purpose | Blueprint / Template | Active Execution Environment |
| Real-Life Analogy | Cake Recipe / App Installer File | Baked Cake / Installed App running |
| Relationship | You need 1 Image... | ...to spin up N Containers |