Skip to content

tsukiy0's blog

Docker Cheatsheet

March 02, 2018

Images

  • Build

    docker build -t img_x .
    • By using . it will look for Dockerfile in the pwd, override this by specifying your own Dockerfile
  • Remove

    docker image rm img_x
  • List

    docker image ls

Define Dockerfile

  • Extend alpine image

    FROM alpine
    RUN apk add --update --no-cache -q \
    bash \
    curl
  • Extend debian image

    FROM debian
    RUN apt-get update && apt-get install -y \
    bash \
    curl
  • Multi-stage builds

    1. Use tooling to build an artifact

    2. Copy that artifact into a minimal runtime

      FROM golang:1.7.3
      WORKDIR /go/src/github.com/alexellis/href-counter/
      RUN go get -d -v golang.org/x/net/html
      COPY app.go .
      RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffimg_x cgo -o app .
      FROM alpine:latest
      RUN apk --no-cache add ca-certificates
      WORKDIR /root/
      COPY --from=0 /go/src/github.com/alexellis/href-counter/app .
      CMD ["./app"]

Containers

  • Run

    docker run -d --name cntr_x tag img_x
  • Execute

    docker run img_x echo "hello world!"
    # interactive session for debugging
    docker run -it img_x /bin/bash
  • List

    docker ps
  • Logs

    docker logs -f cntr_x
  • Remove

    docker rm -f cntr_x

docker-compose

A useful tool for defining how to build and orchestrate multiple containers.

  • Build

    # all
    docker-compose build
    # single service
    docker-compose build svc_x
  • Run

    # all
    docker-compose up -d
    # single service
    docker-compose run svc_x
  • Stop

    docker-compose down
    # remove volumes (e.g. wipe database)
    docker-compose down --volumes
  • Execute

    docker-compose run svc_x echo "hello world!"
    # interactive session for debugging
    docker-compose run -it svc_x /bin/bash
  • Logs

    # all
    docker-compose logs -f
    # single service
    docker-compose logs -f svc_x
  • Run commands against compose file with different name

    docker-compose run -f ../../pew.yml

Define docker-compose.yml

  • Build from Dockerfile

    version: '3'
    services:
    svc_x:
    build:
    context: .
    dockerfile: svc_x.Dockerfile
  • Dependent containers

    version: '3'
    services:
    pg:
    image: postgres:12-alpine
    environment:
    POSTGRES_PASSWORD: postgres
    networks:
    - integration
    pg_migration:
    image: flyway/flyway
    command: -url=jdbc:postgresql://pg:5432/postgres -user=postgres -password=postgres -connectRetries=60 migrate
    volumes:
    - ./flyway/sql:/flyway/sql
    networks:
    - integration
    depends_on:
    - pg

tsukiy0