Docker

Docs | QuickRef | Cheatsheet

Use docker help, man dockerfile of man docker-<command> (e.g. man docker-run).

Concepts:

  • Major components:
    • A server/daemon which
      • manages docker objects, such as images, containers, network and data volumes
      • has a REST API
      • and a CLI (which uses the REST API)
    • A client which is the primary way to interact with Docker.
    • A registry (e.g. Docker Hub) that stores images
  • App hierarchy:
    • Stack
    • Service: a container from a docker-compose file
    • Container:
      • When stopped: a runnable instance of an image
      • When started: a running image (i.e. image + state)
      • may be connected to a network and/or storage
      • can be stored as an image
    • Image:
      • A read-only template with instructions for creating a Docker container
      • an executable package
      • may be based on another image
  • Files:
    • A Dockerfile defines an image
    • A docker-compose.yml defines one or more containers (a.k.a. services) that work together
  • Tips:
    • You start a container (after it has been stopped) and run an image (which creates and starts a container).
    • ​Docker services can address each other through their container names as host name.

Basic docker commands

command (docker …) effect
build -t friendlyhello . Create image using this directory's Dockerfile
run -p 4000:80 friendlyhello Run "friendlyname" mapping port 4000 to 80
run -d -p 4000:80 friendlyhello Same thing, but in detached mode
container ls List all running containers
container ls -a List all containers, even those not running
container stop <hash> Gracefully stop the specified container
container kill <hash> Force shutdown of the specified container
container rm <hash> Remove specified container from this machine
container rm $(docker container ls -a -q) Remove all containers
image ls -a List all images on this machine
image rm <image id> Remove specified image from this machine
image rm $(docker image ls -a -q) Remove all images from this machine
login Log in this CLI session using your Docker credentials
tag <image> username/repository:tag Tag <image> for upload to registry
push username/repository:tag Upload tagged image to registry
run username/repository:tag Run image from a registry

(src)

More advanced docker commands

command (docker …) effect
volume rm $(docker volume ls) Remove all named volumes
exec -i -t container_name /bin/bash Open a terminal
system prune WARNING: use with caution

docker-compose

command (docker-compose ...) effect
exec <container_name> bash run command bash
-f <file> up start from a custom file
down --volumes also remove volumes attached to the container
Tags: technote