Skip to content
ed-mare edited this page Mar 4, 2020 · 15 revisions

Helpful Links

Tools

Dockerignore

CLI

https://docs.docker.com/engine/reference/commandline/cli/

Basic Commands

docker --help

# Show all images.
docker images -a

# View contents of an image. 
docker run -it --rm <image>:<tag> /bin/sh

# Show containers
docker ps # running
docker ps -a   # all containers
docker ps -a  -q  # all containers, (q = quite) only show numeric IDs

# Docker interactive shell in a running container
docker exec -it <container> /bin/bash

# Inspect a container that's not running.
docker run --rm -it --entrypoint=/bin/bash <name of image>

# Stats
docker stats --no-stream

# i.e.
CONTAINER           CPU %               MEM USAGE / LIMIT       MEM %               NET I/O               BLOCK I/O             PIDS
d807fae98e92        0.00%               159.7 MiB / 1.955 GiB   7.98%               40.18 kB / 174.4 kB   86.84 MB / 688.1 kB   0
e889858938d5        0.11%               8.812 MiB / 1.955 GiB   0.44%               11.77 kB / 648 B      2.712 MB / 0 B        0
8d78d4f84b82        0.00%               41.22 MiB / 1.955 GiB   2.06%               17.67 kB / 13.55 kB   27.69 MB / 1.028 MB   0

# Show top processes in container
docker top <container>

# i.e.
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                4735                4728                0                   Feb02               ?                   00:00:00            Passenger watchdog
root                4739                4735                0                   Feb02               ?                   00:00:00            Passenger core
nobody              4745                4735                0                   Feb02               ?                   00:00:00            Passenger ust-router
www-data            4756                4728                0                   Feb02               ?                   00:00:00            nginx: worker process
9999                5164                4670                0                   Feb02               ?                   00:00:00            Passenger RubyApp: /home/app/myapp/public

# Docker logs
docker logs <container name>
docker logs -f --tail <number of lines to show> <container name>

# Docker networks
docker network ls # list all on host
docker network inspect <network> # details on a specific network

# Diff a running container - new/modified files since started
docker diff <container>

# Docker volumes
docker volume inspect <name>  # inspect a volume

# i.e.
[
    {
        "Name": "testrailsdocker_postgres-db-volume",
        "Driver": "local",
        "Mountpoint": "/var/lib/docker/volumes/testrailsdocker_postgres-db-volume/_data",
        "Labels": null,
        "Scope": "local"
    }
]
# Stop all running containers
docker stop $(docker ps -q)

Debug

# Copy any file from container onto your local machine
docker cp <container_id>:/path/to/useful/file /local-path

# Good for running or stopped container - do 'ps -a' to see the stopped container
docker logs <container name>

# Docker interactive shell for running container
docker exec -it <container> /bin/bash
docker exec -it --privileged <container> /bin/bash # to install strace and run strike -p

# Failed docker initialization
docker events  # Run the failed container, get the ID and inspect the logs

Creation

# VOLUMES

# create volume named datacontainer based off ubuntu image in the hosts /tmp directory
docker create -v /tmp --name datacontainer ubuntu
# Create ubuntu image and mount container in image,  anything written to /tmp directory in ubuntu container
# will get saved to the /tmp volume of our datacontainer
container.docker run --volumes-from datacontainer ubuntu

Cleanup Containers

# flags -q = --quiet (return only id) -f = --filter

# man docker ps
-f, --filter=[]
          Filter output based on these conditions:
          - exited=<int> an exit code of <int>
          - label=<key> or label=<key>=<value>
          - status=(created|restarting|running|paused|exited|dead)
          - name=<string> a containers name
          - id=<ID> a containers ID
          - before=(<container-name>|<container-id>)
          - since=(<container-name>|<container-id>)
          - ancestor=(<image-name>[:tag]|<image-id>| ⟨image@digest⟩) - containers created from an image or a descendant.
          - volume=(<volume-name>|<mount-point-destination>)
          - network=(<network-name>|<network-id>) - containers connected to the provided network

# Delete all stopped containers
docker rm $(docker ps -a -q)

# Delete stopped containers and filter by name (even partial name)
docker rm $(docker ps -a -q -f "name=<name here>")

i.e,
docker rm $(docker ps -a -q -f "name=reactexamples_web_run")
# it can be wildcarded...
docker rm $(docker ps -a -q -f "name=reactexamples_web_run* ")
# many partial names with or piped
docker rm $(docker ps -a -q -f "name=small|sleepy|cranky|suspicious|stoic|admiring")

# Filtering with reference would give:
docker images --filter=reference='busy*:*libc'

# REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
# busybox             uclibc              e02e811dd08f        5 weeks ago         1.09 MB
# busybox             glibc               21c16b6787c6        5 weeks ago         4.19 MB

# Delete exited containers
docker rm $(docker ps -q -f "status=exited")

Cleanup images

# Delete dangling (untagged <none>) images
docker rmi $(docker images -q -f "dangling=true")
docker image prune # does same as above
docker image prune -f # no prompt

# Docker prune with -a flag removes unused and dangling images.
# https://docs.docker.com/config/pruning/
docker image prune -a 

# Remove images created more than 24h ago.
docker image prune -a --filter "until=24h"

# filter by name 
docker images -q "foobar*"

# CAREFUL remove all images
docker rmi $(docker images -q)

Cleanup Volumes

# Delete all dangling volumes
docker volume rm $(docker volume ls -qf dangling=true)

#### Truncate Logs

# Truncate all docker logs
sudo -i
truncate -s 0 /var/lib/docker/containers/*/*-json.log

Troubleshooting

  • If an image built moments ago and starts to fail to build (i.e., can't install package, can't install gem), delete image and all layers and start from fresh.

Clone this wiki locally