Docker Cheatsheet

Docker is a tool that allows you to get the equivalent of a disposable, single-time use computer

Tips

  • Do not use 'latest', instead use a specific tag.
  • Use unless-stopped instead of always for docker run --restart

Up and running

Entities

  • Image
  • Container
  • Registry

Container management

  • docker ps: lists the containers that are still running. Add the -a switch in order to see containers that have stopped

  • docker logs <id>: retrieves the logs of a container, even when it has stopped

    • —-since 10s get most recent 10 seconds of logs
  • docker inspect <id>: gets detailed information about a running or stopped container

  • docker stop <id>: stops a container that is still running

  • docker rm <id>: deletes a container

  • docker container prune -f: remove all stopped containers

  • docker run <image> <cmd> - run container

    • -d: run detached
    • -p 8085:80: map ports
    • -v /your/dir:/var/lib/mysql: map volumes(file system)
    • --rm: automatically removes the container after it exits.
    • -it: a combination of two options:
      • -i which allows you to send input to the container, and
      • -t which allocates a pseudo-TTY, allowing you to interact with the container as if you were sitting in front of it.
    • -e <variable> <value>: set env variable
    • --restart <option>: what to do when container stops
      • <option>
  • docker image ls: list local images

  • docker pull <image>: download image, even if its already presented

    • it can be useful if you want to have the latest version of an image tagges as “latest”

Create docker image

  • Every image is based on another base image
  • Image is described in Dockerfile, but it can be any other name

Dockerfile

  • FROM <image:tag>: specify base image
  • CMD [<cmd, …args>]: run command
  • COPY <origin> <dest>: copy file from origin directory to destination directory in the image
  • ENV <name>=<value>: set env variable
  • VOLUME <path>: path is directory in the container. When a container is created using the docker run command, the -v switch can be used to map this directory to an actual volume on the host system.
    • If the user doesn't map this volume to external store, the data will be stored inside the container
  • EXPOSE <port>: this instruction is only for documentation purpose. It doesn't actually expose the port. To expose a port, you need to use the -p flag when running docker run
  • RUN <command>: execute any command to create a new layer on top of the current image
  • WORKDIR <path>:

Build

docker build -t <name> <path>

  • <name> is optional
  • <path> is used as build context and where the Dockerfile is stored
  • --no-cache

Registry

When an image is published to a registry, its name must be: <repo_name>/<name>:<p>

  • tag is optional
  • repo_name can be a registry DNS or the name of registry in the Docker Hub

Publish an Image

  1. Build your image with appropriate prefix name or tag docker build docker tag
  2. Log into the Registry docker login
  3. Push the image docker push

Docker tag

docker tag <local image> <username>/<image>:<tag> Example: docker tag tldw psy667/tldw:0.5.9

Monitoring

docker stats: get stats for running containers

Reclaiming Disk Space

  • Stopped containers that were not removed by using the --rm switch on the docker run command or using the docker rm command once they are stopped.

  • Unused images: images that are not referenced by other images or containers.

  • Dangling images: images that have no name. This happens when you docker build an image with the same tag as before, the new one replaces it and the old one becomes dangling.

  • docker system df: Get stats about disk usage

  • docker container prune -f:

  • docker volume prune -f:

  • docker image prune -f: Remove all dangling images

  • docker image prune --all: Remove all unused images

  • docker system prune: Removes:

    • all stopped containers
    • all networks not used by at least one container
    • all dangling images
    • all dangling build cache