Running Containers
What is a container?
Section titled “What is a container?”An image is a blueprint. A container is a live, running instance of that blueprint — an isolated process with its own filesystem, network, and process space, all sharing the host kernel.
You can run many containers from the same image, stop and restart them, inspect their logs, and remove them when you are done. Understanding this lifecycle is the foundation of working with Docker day to day.
The container lifecycle
Section titled “The container lifecycle”Every container moves through four states:
| State | What it means |
|---|---|
| Created | docker create allocated the container but it has not started yet. |
| Running | The container’s main process is executing. |
| Stopped | The main process exited (or you called docker stop). The container still exists on disk. |
| Removed | docker rm deleted the container and freed its writable layer. |
docker run → Created + Runningdocker stop → Stoppeddocker start → Running againdocker rm → Removed (gone)What this module covers
Section titled “What this module covers”| Lesson | Topic |
|---|---|
| Lifecycle | docker run, docker ps, docker stop/start/restart, docker rm, --rm |
| Exec & Logs | docker exec -it, docker logs, docker top, attaching |
| Ports & Env | Publishing ports -p, environment variables -e, naming --name |
| Restart & Inspect | Restart policies, docker inspect, docker stats, resource limits |
Quick demo — run, list, and stop
Section titled “Quick demo — run, list, and stop”The three commands you will use most in this module:
# Pull and run nginx in the backgrounddocker run -d --name demo nginx:alpine
# List running containersdocker ps
# Stop itdocker stop demo
# Remove itdocker rm demoCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa1b2c3d4e5f6 nginx:alpine "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 80/tcp demo# Pull and run nginx detached
docker run -d --name demo nginx:alpine
# Confirm it is running
docker ps
# Stop the container
docker stop demo
# Remove it
docker rm demo
# Confirm it is gone
docker ps -a