Summary - First container
- Docker & Kubernetes course
- Beginner
- Lesson 18/82
- 5 min
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.-dstarts it in the background;docker execruns a command in a running container.docker ps(-a,-q,--filter,--format) lists containers.stopshuts down gracefully,killkills immediately,rmremoves,startstarts again.docker logsshows stdout/stderr (-f,--tail,--since).
docker run -d --name web nginx
docker logs -f --tail 10 web
docker stop web && docker rm webPractice 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
# buraya yazSample solution
docker run -d --name web nginx
docker logs --tail 10 web
docker stop web
docker rm webExpected 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.
docker rm $(docker ps -a ____)
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 |