Docker Cheatsheet

11.29.2020

List docker images

docker images

Remove an image

docker rmi [IMAGE]

List volumes

docker volume ls

Remove a volume

docker volume rm [VOLUME]

Open interactive shell into docker image

docker run -it [IMAGE] bash

Stop all containers

docker stop $(docker ps -aq)

Remove all containers

docker rm $(docker ps -aq)

List all networks

docker network ls

Prune all networks

docker network prune -f

List dangling images

docker images --filter dangling=true

Remove all dangling images

docker rmi -f $(docker images --filter dangling=true -qa)

List all dangling volumes

docker volume ls --filter dangling=true

Remove all dangling volumes

docker volume rm $(docker volume ls --filter dangling=true -q)

Remove all images

docker rmi -f $(docker images -qa)

Prune unused volumes

docker system prune --volumes

List running containers

docker ps

Kill docker container

docker kill [CONTAINER ID]

Build, tag, and push image to container registry

export COMMIT_ID=`git rev-parse HEAD`

docker build -f ./server/Dockerfile -t gcr.io/my-gcp-project/my-service:$COMMIT_ID -t gcr.io/my-gcp-project/my-service:latest ./server

docker push gcr.io/my-gcp-project/my-service:$COMMIT_ID

docker push gcr.io/my-gcp-project/my-service:latest

Copy a file into container

docker cp SRC_PATH CONTAINER:DEST_PATH

Docker Compose

Build without cache

docker-compose build --no-cache

Run a command

docker-compose run [SERVICE] [COMMAND]

Open interactive shell

docker-compose exec [SERVICE] bash

Tail logs

docker-compose logs -f [SERVICE]

Start docker-compose and rebuild containers

docker-compose up --build

Stop docker-compose

docker-compose down

Rebuild a single service

docker-compose build --no-cache [SERVICE]

Run a single service from docker-compose file

docker-compose run [SERVICE]

Run a docker-compose and abort on container exit

docker-compose -f ./docker-compose-test.yml up --abort-on-container-exit

Example docker-compose file

version: "3"
services:
  client:
    image: my-client
    container_name: client
    build:
      context: ./client
      dockerfile: Dockerfile
    restart: always
    ports:
      - "3000:3000"
    env_file: ./client/.env
    stdin_open: true
    volumes:
      - ./client/src:/usr/src/app
    command: sh "docker-entrypoint.sh"
  db:
    image: postgres:11.2
    container_name: db
    build:
      context: ./db
      dockerfile: Dockerfile
    restart: always
    ports:
      - "5432:5432"
    env_file:
      - ./server/.env
    volumes:
      - dbdata:/var/lib/postgresql/data
  server:
    image: my-server
    container_name: server
    build:
      context: ./server
      dockerfile: Dockerfile
    restart: always
    ports:
      - "5000:5000"
    env_file:
      - ./server/.env
    depends_on:
      - db
    volumes:
      - ./server/src:/usr/src/app
    command:
      ["./wait-for-it.sh", "db:5432", "--", "sh", "./docker-entrypoint.sh"]
volumes:
  dbdata:

docker-entrypoint.sh example

#!/usr/bin/env bash

npm run dev

wait-for-it script

https://github.com/vishnubob/wait-for-it

Multi-stage build Dockerfile example

FROM node:12 AS build
WORKDIR /usr/src/app
COPY ./package.json ./
RUN npm install
COPY . .

FROM node:12
COPY --from=build /usr/src/app /usr/src/app
WORKDIR /usr/src/app
CMD ["npm", "start"]