Docker Images and Containers
One of the most important Docker concepts:
Images and containers are NOT the same thing.
Docker Image
An image is a template.
Think of it like:
- blueprint
- snapshot
- application package
Images are read-only.
Example:
docker pull nginx
This downloads an image.
Docker Container
A container is a running instance of an image.
Example:
docker run nginx
This creates and starts a container.
Visual Explanation
Docker Image
↓
Creates
↓
Docker Container
One image can create many containers.
Real Example
Pull Ubuntu image:
docker pull ubuntu
Run container:
docker run -it ubuntu bash
Breakdown:
| Part | Meaning |
|---|---|
run |
create container |
-it |
interactive terminal |
ubuntu |
image name |
bash |
shell inside container |
Container Lifecycle
Containers move through states:
Created
↓
Running
↓
Stopped
↓
Removed
Useful commands:
docker ps
docker stop
docker start
docker rm
Important Docker Commands
List running containers:
docker ps
List all containers:
docker ps -a
List images:
docker images
Remove container:
docker rm CONTAINER_ID
Remove image:
docker rmi IMAGE_ID