General Link to heading

sudo service docker start/restart/stop

# cleaning up
docker system prune # removes all stopped containers, unused networks, dangling images, and build cache
docker system prune -a # remove all unused images, not just dangling ones

# delete all images 
docker rmi $(docker images -q)

Build an image Link to heading

docker build -f <dockerfile> -t <name> .
docker build -f <dockerfile> -t <name>:<tag> .
  • -f:allows specifing a dockerfile outside the location
  • .: the positional argument is the build context, here . (= the current working directory)1

Run an image Link to heading

docker run --rm -it -d --privileged=true -v $(pwd)/../..:/directory_in_container --name <container_name> -p 8000:8000 <image_name>:<tag e.g. latest>
  • --rm: will remove existing container
  • -it: for interactive access e.g. bash
  • --detach/-d: run container in background and print container ID, no print out
  • --entrypoint: specificy entrypoint e.g. /bin/bash
  • -v: mount volume into container
  • -p: port forwarding
  • -v: hosting directory in container

Manipulate running container Link to heading

# use bash in a running container
docker exec -ti <container_name> bash

# get the ID of a running container
docker ps --filter ancestor=<image_name> --format "{{.ID}}"

# copy files out of a docker container 
docker cp <container id>:/file/path/within/container /host/path/target  # e.g. docker cp e9e8acdb0440:/project/geo-countries/data/countries.geojson ~/Downloads

# stopping running image
docker container stop <id>
docker container stop <tag>

Transfer an image Link to heading

docker save <image_name> # saved as .tar
docker save <image name>:2 | gzip > file_name.tar.gz # with gzip

docker load < file_name.tar.gz

References Link to heading