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)

# delete named volumes (see compose.yml below)
docker volume rm caddy_data

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
  • --no-cache

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

Docker Compose Link to heading

Docker Compose is a tool for defining and running multi-container applications based on the YAML based compose file.

Example compose.yml

services:
  caddy:
    image: caddy:2.10
    restart: unless-stopped  # container will start unless deliberately stopped by the user
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
    volumes:
      - ./conf:/etc/caddy  # maps a local directory ./conf on the host machine to /etc/caddy inside the container
      - caddy_data:/data  # creates a named volume `caddy_data` mapped to `data` in the container. Named volumes are managed by Docker and persist even when containers are removed

References Link to heading