FresherTechnicalDocker
Which Docker commands list containers, read logs and open a shell inside one?
Try answering first
Model answer
Three commands cover most day-to-day inspection.
docker ps # running containers
docker ps -a # all containers, including stopped ones
docker logs -f --tail 100 web # follow the last 100 log lines
docker exec -it web sh # open a shell inside a running container
docker pslists running containers with their id, image, status and ports. Add-ato include stopped ones, which is how you find a container that crashed.docker logsshows what the main process wrote to stdout and stderr.-ffollows new lines, and--taillimits the output.docker exec -itruns a new command in a running container.-ikeeps stdin open and-tallocates a terminal. Useshif the image has nobash.
Related: docker inspect web prints the full configuration, and docker stats shows live CPU and memory use.
exec works only on a running container. For a stopped one, read the logs, or start a new container from the same image with a shell as its entrypoint.
ps to find it, logs to see what happened, exec to look inside.
Key points
- docker ps lists containers, and -a includes stopped ones
- docker logs shows stdout and stderr, with -f to follow
- docker exec -it opens a shell in a running container
- exec needs a running container
Likely follow-ups
- How do you get a shell in a container that keeps crashing?
Common mistakes
- Looking only at docker ps and missing crashed containers
- Trying bash in an image that only has sh