CodeBlanks Logo Icon CodeBlanks

Summary - First container

Combine run, ps, stop and logs to manage a container.

Summary: the first container

In this group:

  • docker run image command: creates a container from an image; the container stops when the main process ends.
  • -d starts it in the background; docker exec runs a command in a running container.
  • docker ps (-a, -q, --filter, --format) lists containers.
  • stop shuts down gracefully, kill kills immediately, rm removes, start starts again.
  • docker logs shows stdout/stderr (-f, --tail, --since).
docker run -d --name web nginx
docker logs -f --tail 10 web
docker stop web && docker rm web

Practice A container's life cycle

Write four commands: start a container named web from nginx in the background; show the last 10 log lines; stop the container; remove the container.

Starter code

terminal
# buraya yaz

Sample solution

terminal
docker run -d --name web nginx
docker logs --tail 10 web
docker stop web
docker rm web

Expected output

7c2f4a9e1b3d
/docker-entrypoint.sh: Configuration complete; ready for start up
web
web

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which stops all running containers with one command?

  • docker stop $(docker ps -q) Correct answer
  • docker stop --all-running
  • docker rm -a
  • docker ps --stop

Explanation: docker ps -q gives the IDs; stop takes all of them.

2 Pass the IDs to the command.

Question
docker rm $(docker ps -a ____)
Answer
docker rm $(docker ps -a -q)

Explanation: -q prints only the IDs.

3 Match.

Term Meaning
docker run New container
docker exec Command in an existing container
docker logs Output records
docker rm Removal

All lessons in Beginner