Docker and Kubernetes course menu
Managing Docker Containers: ps, logs, exec, stop and rm
Manage containers day to day: list them with docker ps, read logs, run commands with docker exec, copy files, understand the container lifecycle, and set restart policies.
On this page
A container named web is running for this lesson.
List containers#
docker ps shows running containers, and -a includes stopped ones:
docker run --name once alpine:3.24 echo "Done" > /dev/null
docker ps --format '{{.Names}} {{.State}}'
echo ---
docker ps -a --format '{{.Names}} {{.State}}'Unable to find image 'alpine:3.24' locally
3.24: Pulling from library/alpine
Digest: sha256:be2656061b536c1883c4034790e21d0a09c5aaaf51968b89368a3c0371ea2c9a
Status: Downloaded newer image for alpine:3.24
web running
---
once exited
web runningStopped containers remain until you remove them, and they take up a small amount of disk space.
The lifecycle#
created --> running --> exited --> removed
^ |
| v
paused / restarteddocker stop sends SIGTERM, and then SIGKILL if the process has not ended. See signals.
Logs#
Containers should write logs to standard output, and Docker collects them:
docker logs --tail 1 web 2>&1 | cut -c1-60172.17.0.1 - - [05/Nov/2026:07:05:38 +0000] "GET / HTTP/1.1"Run commands in a container#
docker exec runs a command in a running container:
docker exec web nginx -v
docker exec web sh -c 'echo "Hello from exec" > /usr/share/nginx/html/hi.html'
curl -s http://localhost:8080/hi.htmlnginx version: nginx/1.30.6
Hello from execFor an interactive shell, use this:
docker exec -it web shChanges do not last#
The file above exists only in this container. A new container from the same image will not have it, because each container gets its own thin writable layer:
docker rm -f web > /dev/null
docker run -d --name web -p 8080:80 nginx:1.30-alpine > /dev/null
sleep 2
curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8080/hi.html404That is a good thing: containers are disposable, and you can replace them at any time. Keep important data in volumes.
Copy files#
docker cp copies between your computer and a container:
echo "<h1>Copied</h1>" > page.html
docker cp page.html web:/usr/share/nginx/html/page.html
curl -s http://localhost:8080/page.html
docker cp web:/etc/nginx/nginx.conf ./nginx.conf
wc -l < nginx.conf<h1>Copied</h1>
32See resource use#
docker stats --no-stream --format '{{.Name}} {{.PIDs}} processes'
docker top web -o pid,comm | head -n 2 | awk '{print $2}'web 3 processes
COMMAND
nginxRestart policies#
Tell Docker to restart a container if it crashes, or when the server reboots:
docker run -d --name always --restart unless-stopped alpine:3.24 sleep 300 > /dev/null
docker inspect --format '{{.HostConfig.RestartPolicy.Name}}' always
docker rm -f always > /dev/nullunless-stoppedClean up#
docker rm -f web > /dev/null
docker container prune -f | tail -n 1Total reclaimed space: 0BCommon mistakes#
Practice#
Practice exercise
Run alpine:3.24 in the background with the command sleep 60, named worker. Print its status word using docker inspect, stop it with docker kill, and print the status again.
Show hint
docker inspect --format '{{.State.Status}}' worker.
Show solution
docker run -d --name worker alpine:3.24 sleep 60 > /dev/null
docker inspect --format '{{.State.Status}}' worker
docker kill worker > /dev/null
docker inspect --format '{{.State.Status}}' worker
docker rm worker > /dev/nullrunning
exitedKey takeaway#
Next: Writing a Dockerfile.
Finished this lesson?
Related lessons
Recommended Docker and Kubernetes resources
- Docker Deep Dive by Nigel Poulton
A practical guide to how Docker works, for readers who have finished the basics.
Some links are affiliate links. If you buy through them, Laviyx Learn may earn a commission at no extra cost to you. As an Amazon Associate, we earn from qualifying purchases.