11. Debugging running containers
In this lab, you’ll learn some more advanced features used when debugging, stopping and starting containers
Docker info
To receive general info about your docker environment use
docker system info
Containers: 42
Running: 0
Paused: 0
Stopped: 42
Images: 75
Server Version: 18.06.1-ce
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 468a545b9edcd5932818eb9de8e72413e616e86e
runc version: 69663f0bd4b60df09991c08812a60108003fa340
init version: fec3683
Security Options:
apparmor
seccomp
Profile: default
Kernel Version: 4.15.0-36-generic
Operating System: Ubuntu 18.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 15.31GiB
Name: system-name
ID: QSH5:AZKW:AZKW:FZLG:AZKW:2WRM:AZKW:OOFU:UUTI:AZKW:YBG5:QI4F
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Username: philipona
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Listing containers
Note
docker ps and docker container ls are equivalent. This also applies to other top-level commands.
For brevity, we are going to use docker ps etc. even though the docker container commands are more consistent.
See docker container --help for a full list of sub-commands.
To see all running containers:
docker ps
To see all containers, also exited containers:
docker ps --all
To see only the last container that was started:
docker ps -l
To see only the ID of containers:
docker ps -q
To see only the ID of the last started container:
docker ps -ql
To see the size of the containers:
docker ps -s
To see the general docker storage usage:
docker system df
This is helpful for scripting or doing a lot of experimentation where you start and delete a container quite a lot of times. As an example docker rm -f $(docker ps -ql), which will delete the last started container.
Stopping containers
Take a look at lab 04 .
Restarting and attaching to containers
We have started containers in the foreground and in the background. Now we will see how to:
- Put a container in the background.
- Attach to a background container to bring it to the foreground.
- Restart a stopped container
Background and foreground
From Docker’s point of view, all containers are the same. All containers run the same way, whether there is a client attached to them or not.
It is always possible to detach from a container, and to re-attach to a container.
Detaching from a container
If you have started an interactive container (with option -it), you can detach from it.
- The detach key sequence is
CTRL-p CTRL-q. - Or you can detach by killing the Docker client.
Don’t hit CTRL-c, as this would deliver SIGINT to the container
What does -it stand for?
-tmeans “terminal” as in “allocate a terminal.”-imeans “interactive” as in “connect stdin to the terminal.”
Attaching to a container
You can attach to a container:
docker attach <container>
- The container must be running.
- There can be multiple clients attached to the same container.
- Warning: if the container was started without
-it:- You won’t be able to detach with
CTRL-p CTRL-q. - If you hit
CTRL-c, the signal will be proxied to the container.
- You won’t be able to detach with
Remember: you can always detach by killing the Docker client (e.g. close the bash window).
Executing a command in a container
You can execute a command in a container:
docker exec -it <container> <command>
E.g. if you want to execute a shell, then run the following command:
docker exec -it <container> /bin/bash
Checking container output
Use docker attach if you intend to send input to the container.
If you just want to see the output of a container, use docker logs.
Restarting a container
When a container has exited, it is in stopped state. It can then be restarted with the start command: docker start <container>
The container will be restarted using the same options you launched it with. You can re-attach to it if you want to interact with it.
Listing images
We already stumbled upon the command to list Docker images. See lab 02 .
Viewing logs of containers
docker logs <container>
This will show the whole log of that container, sometimes it’s enough to display just a few lines:
docker logs --tail 3 <container>
With the -follow option you can tell the docker logs command to follow the log file in real time:
docker logs --tail 3 --follow <container>
Housekeeping
There are various housekeeping commands.
Dev environment
Note
Do not use these commands in production!Stop all running containers and then delete them:
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
Delete all images:
docker rmi $(docker images -q)
Pruning
Remove unused data:
docker system prune
Remove all stopped containers:
docker container prune
Remove unused images:
docker image prune