─── ❖ ── ✦ ── ❖ ───

General commands

  • List total size used by Docker components (Source)
    docker system df
  • List detailed size used by Docker components (Source)
    docker system df -v

Containers

  • List all containers
    docker ps -a
  • List only running containers
    docker ps
  • List running containers with custom formatting
    docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"
  • Enter the shell of the containerΒ $CONTAINERNAME (must be running)
    docker exec -it $CONTAINERNAME sh
  • Stop all running containers, run something, then start them up again
    CONTRUN2=$(docker ps -q)
     
    docker stop $CONTRUN2
     
    ...
     
    docker start $CONTRUN2

Volumes

  • List all volumes
    docker volume ls
  • Delete the volume $VOLUMENAME
    docker volume rm $VOLUMENAME
  • List contents of the volume $VOLUMENAME (works with alpine and ubuntu)
    docker run --rm -v $VOLUMENAME:/data/ alpine ls -la /data
  • Delete contents of the volume $VOLUMENAME
    (works with alpine and ubuntu)
    docker run --rm -v $VOLUMENAME:/data/ alpine /bin/sh -c "rm -rf /data/*"
  • List all containers using the volume $VOLUMENAME (Source)
    docker ps -a --filter volume=$VOLUMENAME
  • List all volumes and by which container those are used (Source)
    for v in $(docker volume ls --format "{{.Name}}")
    do
     containers="$(docker ps -a --filter volume=$v --format '{{.Names}}' | tr '\n' ',')"
     echo "volume $v is used by $containers"
    done
  • List all volumes and by which container those are used (Alternative formatting,Β Source)
    for volume in $(docker volume ls  --format '{{.Name}}')
    do
     echo $volume
     docker ps -a --filter volume="$volume"  --format '{{.Names}}' | sed 's/^/  /'
    done

Images

  • List all local Docker images

    docker image ls
  • List all local Docker images (including their RepoDigest)

    docker image ls --digests
  • List image (and RepoDigest) of the local image $IMAGENAME

    docker image inspect --format '{{index .RepoDigests 0}}' $IMAGENAME
  • Download image with specific RepoDigest (NOT ImageID, must not be truncated!)

    docker pull jellyfin/jellyfin@sha256:21e49baac0a05efd4822269e3d8ba2f693e741006a2f81aa397cf5f8445e48a9
  • List dangling images (Source)

    docker images -f "dangling=true"
  • List only ImageID of dangling images (Source)

    docker images -qf "dangling=true"
  • Delete all dangling images (Source)

    docker image prune
  • List all images currently used by containers (running and stopped) (Source)

    docker ps -a --format="{{.Image}}"
  • Get ImageID from $IMAGENAME (Source)

    docker images --format "{{.ID}}" --filter=reference=$IMAGENAME
  • List all local images by name, ImageID and RepoDigest (Source)

    docker images --format="{{.Repository}} {{.ID}} {{.Digest}}"

Combined commands

  • List image (and RepoDigest) of running containers
    docker ps --format '{{.Image}}' | xargs docker image inspect --format '{{if .RepoDigests}}{{index .RepoDigests 0}}{{end}}'
  • List ImageID and tag of running containers:
    docker inspect $(docker ps -q) | grep Image
  • Print from which tag the running containers have been pulled
    docker inspect $(docker ps | awk '{print $2}' | grep -v ID) | jq .[].RepoTags

Other commands

  • RestoreΒ docker runΒ command of the container $CONTAINERNAME (withΒ runlike)
    docker run --rm -v /var/run/docker.sock:/var/run/docker.sock:ro assaflavie/runlike $CONTAINERNAME
  • RestoreΒ docker runΒ command by inspecting the container $CONTAINERNAME (withΒ docker inspect,Β Source)
    docker inspect --format "$(curl -s https://gist.githubusercontent.com/efrecon/8ce9c75d518b6eb863f667442d7bc679/raw/run.tpl)" $CONTAINERNAME
  • …

Useful tools


  • …
    ...

─── ❖ ── ✦ ── ❖ ───