Try Documentalist, my app that offers fast, offline access to 190+ programmer API docs.

docker commands
docker run --rm -it --name=name -p <host>:<container> -v <host>:<container> <image> <cmd>
  • —rm : delete when finished running
  • -it : interactive and allocate tty (needed when running interactive commands like bash)
  • -d : run in background
  • -p : expose port inside container to host e.g. -p 3333:80 exposes port 80 inside the container as port 3333 in the host
  • -v : mount a host directory inside the container e.g. -v $(pwd)/..:/opt/script:ro
    • ro stands for read-only; can also be rw for read-write
  • --privileged=true : run as root
  • —rm and -it are often used to run ad-hoc commands
  • -d is used for running long-lived containers (e.g. a database). those can be stopped with docker stop <container>
docker start ${containerId} : start a container that exited/is stopped
docker stop ${containerId} : stops a container
docker exec -it <container> /bin/bash
  • -t : allocate tty (needed for interactive programs like bash)
  • -i : interactive mode
docker images : list images
  • -a : include intermediary containers
docker build : build an image from Dockerfile description
  • -f : name of docker file (Dockerfile by default)
  • -t : tag with ${repository}:${tag}, ${tag} is optional
docker ps : list containers
  • -a : list exited containers
docker rm ${containerId} : remove a container
docker rmi ${image} : remove an image
docker inspect ${containerId} : list info about a container as json
docker concepts
  • is built from description in Dockerfile, e.g.: docker build -t ${imageTag} -f Dockerfile .
  • if specified by name and not present locally, will be downloaded from docker store e.g. docker run ubuntu:18:04 will download ubuntu image from
  • list local images: docker images
  • delete with docker rmi ${imageTag}
  • delete untagged (likely stale) images: docker rmi $(docker images | grep none | awk '{print $3}')
interesting images
  • an image that is running
multi-stage builds for smaller images
Basically a single docker file can have stages, first stage builds the thing, second stage copies only needed files to a fresh image. Available since 17.05.0 (currently requires beta channel on Mac)
# build stage
FROM golang:alpine AS build-env
ADD . /src
RUN cd /src && go build -o goapp

# final stage
FROM alpine
COPY --from=build-env /src/goapp /app/
Example multi-stage docker for Go program that uses cgo:
smallest image for Go (scratch) is an example of using 2 stage to build in golang image and run in scratch image
articles : an example of using docker-compose to run multiple things : Running Untrusted Code in a Secure Docker Container from Scratch
delete untagged images
docker rmi $(docker images | grep none | awk '{print $3}')
remove stopped containers
docker rm $(docker ps -a | awk '{print $1}')
allowing 2 containers to communicate i.e. networking
Scenario: container-2 needs to talk to a port exposed by container-1
Version 1:
Version 2:
  • container-1: expose the port with --expose ${port}
  • container-2: docker run --link="container-1" . This adds /etc/hosts entry for container-1 with the right ip address so that container-2 can use container1:${port} as ip address
Version 3:
  • container-1: docker run --expose ${port} --net-alias="container-1". This exposes container ip under name container-1 in the network so container-2 can use it. Only valid in

Feedback about page:

Optional: your email if you want me to get back to you:

Need fast, offline access to 190+ programmer API docs? Try my app Documentalist for Windows