Container lifecycle
stateDiagram-v2 [*] --> Running: docker run Running --> Stopped: docker stop Stopped --> Running: docker start Stopped --> [*]: docker rm
The command you use most is docker run; it pulls the image (if missing), creates and starts the container:
docker run -d --name web -p 8080:80 nginx:1.27
-d(--detach) runs in the background and prints the container ID.--name websets a name, so later commands can usewebinstead of the ID.-p 8080:80publishes a port in host:container order:http://localhost:8080reaches port 80 inside the container.- All flags go before the image name; anything after the image name is the command run inside the container.
| Command | Meaning |
|---|---|
docker ps / docker ps -a |
Running containers / all, including stopped |
docker logs web |
Show the container's stdout/stderr |
docker exec -it web sh |
Open a shell in a running container |
docker stop web / docker start web |
Stop / start the container again |
docker rm web |
Remove a stopped container (-f also removes a running one) |
docker rmi nginx:1.27 |
Remove an image (= docker image rm) |
Management commands are the newer, equivalent spelling: docker container ls = docker ps, docker image ls = docker images, docker container run = docker run.
Common traps
- Reversing it as
-p 80:8080sends you to the wrong port. - You cannot remove an image while a container (even a stopped one) uses it:
docker rmthe container first. - Copying commands from slides with
–(en dash) instead of-: Docker does not see a flag. - To push to Docker Hub:
docker tagas<user>/<repo>:<tag>,docker login, thendocker push.
More docker run flags you will use
| Flag | Meaning |
|---|---|
-e KEY=value, --env-file .env |
Set environment variables |
-v data:/var/lib/app |
Mount a volume |
--rm |
Remove the container when it exits |
-it |
Interactive terminal, for shells |
--restart unless-stopped |
Restart after crashes and daemon restarts |
--memory 512m --cpus 1 |
Limit memory and CPU |
-u 1000:1000 |
Run as a specific user |
Looking inside
docker logs -f --tail 100 webfollows the last 100 lines.docker inspect webprints full JSON;docker inspect -f '{{.State.Status}}' webextracts one field.docker statsshows live CPU, memory, and network per container;docker top weblists its processes.docker cp web:/etc/nginx/nginx.conf ./nginx.confcopies files out of (or into) a container.docker port webshows published ports.
Exit codes tell a story
| Code | Usual meaning |
|---|---|
| 0 | The main process finished normally |
| 1 | Application error |
| 125 | docker run itself failed (bad flag, name conflict) |
| 127 | Command not found inside the image |
| 137 | Killed by SIGKILL, often the out-of-memory killer or docker kill |
| 143 | Stopped by SIGTERM, a normal docker stop |
docker ps -a shows the exit code in the STATUS column, and docker inspect -f '{{.State.OOMKilled}}' web confirms a memory kill.
Cleaning up
Stopped containers, unused images, and build cache pile up quickly. docker system df shows how much space each uses. docker container prune, docker image prune -a, and docker builder prune remove them selectively; docker system prune removes stopped containers, unused networks, dangling images, and build cache in one go (add --volumes only when you are sure no data is needed).