DCubes
Docker module

Lesson 4

Container lifecycle

Name a container, read logs, stop it, start it, and remove it without pruning the world.

beginner25 minRun on your machine

What you will be able to do

  • Start a detached container and find it with docker ps
  • Read logs and stop the container cleanly
  • Remove a stopped container by name

Why this matters for DE and AI

A pipeline container that “is running” might be restarting every three seconds. docker ps and docker logs are the first two questions: is the process up, and what did it print? Dashboards come later.

Concepts

Command What it does
docker run -d --name NAME IMAGE CMD Start in the background (detached)
docker ps List running containers
docker ps -a Running and stopped
docker logs NAME stdout/stderr of the main process
docker logs -f NAME Follow (like tail -f)
docker stop NAME Ask the process to exit, then kill if needed
docker start NAME Start a stopped container again (same writable layer)
docker rm NAME Delete a stopped container

-d without --name still works; you will hate the random name.

Status words

docker ps shows STATUS like Up 10 seconds or Exited (0) 3 seconds ago. Exit code 0 usually means success. Non-zero means the process failed — read logs before you delete it.

stop vs rm vs --rm

  • stop — container still exists; you can start or logs.
  • rm — gone. Image stays.
  • run --rmrm happens automatically on exit. Do not use --rm if you still need logs after a crash.

Practice on your machine

cd ~/dcubes/docker-lab

Start a container that stays up by sleeping. Give it a name.

docker run -d --name dcubes-sleep alpine:3.20 sleep 600

What you should see: a long hex ID. That is the container ID. Detached runs do not print sleep output.

docker ps

You should see dcubes-sleep, image alpine:3.20, command sleep 600, status Up ….

Sleep prints nothing, so make a container that does:

docker run -d --name dcubes-ticks alpine:3.20 sh -c 'for i in 1 2 3 4 5; do echo tick $i; sleep 1; done'

Wait a few seconds, then:

docker logs dcubes-ticks

What you should see: lines tick 1tick 5. If you were fast, fewer lines; run docker logs again. When the loop ends, the container exits.

docker ps -a --filter name=dcubes-

dcubes-sleep should still be Up. dcubes-ticks should be Exited (0).

Stop the sleeper and remove both:

docker stop dcubes-sleep
docker rm dcubes-sleep dcubes-ticks
docker ps -a --filter name=dcubes-

What you should see: dcubes-sleep printed by stop, then both names printed by rm, then an empty list (header only).

If rm says the container is running, you skipped stop. If run says the name is in use, docker rm the old one (stop first if needed).

Common mistakes

  • Reading docker images when you meant ps. Images do not have a Status column of Up/Exited.
  • docker rm on a running container. Stop first, or docker rm -f (force). Prefer stop.
  • Losing crash logs because of --rm. For a job you are debugging, omit --rm until you have copied docker logs.

Next

Look insidedocker exec and inspect.