These are 5 useful Docker CLI commands which will help you in debugging and keeping your dev environment clean.
The docker ps
command only prints out currently running containers.
~/dev ᐅ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ee166ff0af6a gogs/gogs:0.11.66 "/app/gogs/docker/st…" 4 months ago Up 13 minutes 0.0.0.0:10022->22/tcp, 0.0.0.0:10080->3000/tcp class_gogs_4
c0b4f753e172 postgres:11.1 "docker-entrypoint.s…" 4 months ago Up 13 minutes (healthy) 0.0.0.0:15432->5432/tcp class_postgres_4
56bb63ff9213 127.0.0.1:5000/myuser/jenkins-class:latest "/sbin/tini -- /usr/…" 4 months ago Up 13 minutes 0.0.0.0:50000->50000/tcp, 0.0.0.0:10081->8080/tcp class_jenkins_4
By adding -all
flag, you can see all the containers including running and not running together.
~/dev ᐅ docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dcca1c1725c7 49d6021897ff "/bin/sh -c 'cd /sla…" 3 months ago Exited (20) 3 months ago stoic_khayyam
6eab34c14967 slate-example_app "bundle exec middlem…" 3 months ago Exited (10) 3 months ago slate-example_app_1
ee166ff0af6a gogs/gogs:0.11.66 "/app/gogs/docker/st…" 4 months ago Up 13 minutes 0.0.0.0:10022->22/tcp, 0.0.0.0:10080->3000/tcp class_gogs_4
c0b4f753e172 postgres:11.1 "docker-entrypoint.s…" 4 months ago Up 13 minutes (healthy) 0.0.0.0:15432->5432/tcp class_postgres_4
56bb63ff9213 127.0.0.1:5000/myuser/jenkins-class:latest "/sbin/tini -- /usr/…" 4 months ago Up 13 minutes 0.0.0.0:50000->50000/tcp, 0.0.0.0:10081->8080/tcp class_jenkins_4
If you are looking for a particular container, you can use grep to filter out results.
docker ps -all | grep gogs
Or you can use docker's filter flag:
docker ps -all --filter name=gogs
You can check the logs your container prints with docker logs <container-id|container-name>
command. If you include -f
or --follow
flag, the logs will keep printing the output.
docker logs -f <container-id|container-name>
~/dev ᐅ docker logs -f ee166ff0af6a
2020/02/08 23:25:09 [ INFO] Gogs 0.11.66.0916
2020/02/08 23:25:09 [ INFO] Cache Service Enabled
2020/02/08 23:25:09 [ INFO] Session Service Enabled
2020/02/08 23:25:09 [ INFO] SQLite3 Supported
2020/02/08 23:25:09 [ INFO] Run Mode: Development
2020/02/08 23:25:09 [ INFO] Listen: http://0.0.0.0:3000
MySQL Connection Error: (2002) Connection refused
If you don't remember the ID or the container name exactly, you can find it with docker ps command. However, you can also combine these commands together.
docker logs -f $(docker ps --filter name=gogs -q)
The -q
flag in the docker ps command will only print the IDs and the filter command will retrieve containers that has wordpress in their name.
You can take a peek inside the container running in your dev envrionment. This will take you the bash in your container. You can look around or run any commands you like here.
docker exec -it <container-id|container-name> /bin/sh
This command outputs metadata about your container such as container configuration, network settings, volumes and so on.
docker inspect <container-name>
[
{
"Id": "sha256:5a02f920193bc1d2658f673d0c77f93f25e7670078b930232f17856be34d7699",
"RepoTags": [
"postgres:11.1"
],
"RepoDigests": [
"postgres@sha256:f310592cf3964f038dbaefac2dc2088982e5ab06312a590bcacc97749ee5db69"
],
"Parent": "",
"Comment": "",
"Created": "2019-02-06T08:16:44.722701909Z",
"Container": "2d677acb5076b018c635c60e8a9c0883808f984d199dd4fee398fdc3de3a4021",
"ContainerConfig": {
"Hostname": "2d677acb5076",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"5432/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/postgresql/11/bin",
"GOSU_VERSION=1.11",
...
When you work with Docker for a while, you might notice that the disk space used by docker increasing.
Run this command to check how docker uses your disk space and how much is reclaimable.
docker system df
~/dev ᐅ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 26 13 6.711GB 4.388GB (65%)
Containers 15 5 104MB 42.59MB (40%)
Local Volumes 5 2 549.4MB 287.7MB (52%)
Build Cache 0 0 0B 0B
The reclaimable space consists of unused containers, images and volumes. Docker prune helps you reclaim space by removing unused resources. By default, it doesn't include volumes.
docker system prune --volume
~/dev ᐅ docker system prune --volumes
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all volumes not used by at least one container
- all dangling images
- all dangling build cache
Are you sure you want to continue? [y/N]
These are the commands that I use almost every day. They help me debug issues correctly and get a more sense of how an application is doing. Happy coding!
Command | Details |
---|---|
docker ps -a |
List all containers including not running |
docker logs -f <container-id or container-name> |
Output and follow container logs |
docker exec -it <container-id or container-name> /bin/sh |
Execute bash in the container |
docker inspect |
Retrieve metadata |
docker system prune |
Clean unused resources |