Skip to content
Laviyx Learn

Try

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.

Beginner6 min readUpdated
On this page

A container named web is running for this lesson.

List containers#

docker ps shows running containers, and -a includes stopped ones:

Terminal
docker run --name once alpine:3.24 echo "Done" > /dev/null
docker ps --format '{{.Names}} {{.State}}'
echo ---
docker ps -a --format '{{.Names}} {{.State}}'
Output
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 running

Stopped containers remain until you remove them, and they take up a small amount of disk space.

The lifecycle#

Text
created --> running --> exited --> removed
              ^  |
              |  v
             paused / restarted
CommandEffect
docker createCreate, but do not start
docker startStart a stopped container
docker stopAsk it to stop, then force it after 10 seconds
docker killStop it immediately
docker restartStop and start
docker rmRemove a stopped container

docker 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:

Terminal
docker logs --tail 1 web 2>&1 | cut -c1-60
Output
172.17.0.1 - - [05/Nov/2026:07:05:38 +0000] "GET / HTTP/1.1"
OptionMeaning
-fFollow the logs live
--tail 50The last 50 lines
--since 10mThe last 10 minutes
-tShow timestamps

Run commands in a container#

docker exec runs a command in a running container:

Terminal
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.html
Output
nginx version: nginx/1.30.6
Hello from exec

For an interactive shell, use this:

Terminal
docker exec -it web sh

Changes 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:

Terminal
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.html
Output
404

That 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:

Terminal
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
Output
<h1>Copied</h1>
32

See resource use#

Terminal
docker stats --no-stream --format '{{.Name}} {{.PIDs}} processes'
docker top web -o pid,comm | head -n 2 | awk '{print $2}'
Output
web 3 processes
COMMAND
nginx

Restart policies#

Tell Docker to restart a container if it crashes, or when the server reboots:

Terminal
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/null
Output
unless-stopped
PolicyRestarts
noNever (the default)
on-failureOnly after an error exit
alwaysAlways, even after a daemon restart
unless-stoppedLike always, unless you stopped it

Clean up#

Terminal
docker rm -f web > /dev/null
docker container prune -f | tail -n 1
Output
Total reclaimed space: 0B

Common 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
Terminal
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/null
Output
running
exited

Key takeaway#

Next: Writing a Dockerfile.

Finished this lesson?

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.