Skip to content

Latest commit

 

History

History
276 lines (212 loc) · 6.01 KB

README.md

File metadata and controls

276 lines (212 loc) · 6.01 KB

Docker Cheatsheet

If you can't find what you are looking for try my other sources:

Index:

Docker

Manipulating Output

Filter and specify the name that you are interested in:

$ docker ps -f name=my-hostname-service
CONTAINER ID        IMAGE                        COMMAND  CREATED              STATUS              PORTS                     NAMES
edb30579c208        ruanbekker/hostname:latest   "/app"   About a minute ago   Up About a minute   0.0.0.0:42177->8080/tcp   my-hostname-service-1234

Only output the ID:

$ docker ps -f name=my-hostname-service --format '{{.ID}}'
edb30579c208

or:

$ docker ps -f name=my-hostname-service -q
edb30579c208

If you have more than one container with the same prefix, but you are only interested in the most recent one:

$ docker ps -f name=my-hostname-service -ql
edb30579c208

ID, string characters and Name:

$ docker ps  -f name=my-hostname-service --format '{{.ID}} -> {{.Names}}'
edb30579c208 -> my-hostname-service-1234

Chaining them to exec into the container:

$ docker exec -it $(docker ps -f name=my-hostname-service -ql) sh

More examples:

Template Variables

For Docker:

$ docker run -it --log-driver json-file --log-opt tag="{{.ImageName}}|{{.Name}}|{{.ImageFullID}}|{{.FullID}}|{{.ID}}" alpine echo hi
hi

Get the logfile:

$ docker inspect $(docker ps -l) | jq '.[].LogPath'
/var/lib/docker/containers/b8e6523c8741d33f778a4f899dc04dab912472cedfba5ab71119a8c9ab1555a8/b8e6523c8741d33f778a4f899dc04dab912472cedfba5ab71119a8c9ab1555a8-json.log

View the content:

$ cat /var/lib/docker/containers/b8e6523c8741d33f778a4f899dc04dab912472cedfba5ab71119a8c9ab1555a8/b8e6523c8741d33f778a4f899dc04dab912472cedfba5ab71119a8c9ab1555a8-json.log
{"log":"hi\r\n","stream":"stdout","attrs":{"tag":"alpine|festive_bell|sha256:e7d92cdc71feacf90708cb59182d0df1b911f8ae022d29e8e95d75ca6a99776a|b8e6523c8741d33f778a4f899dc04dab912472cedfba5ab71119a8c9ab1555a8|b8e6523c8741"},"time":"2020-07-07T12:35:12.3938298Z"}

For Swarm:

Get a container to report the host's hostname:

version: '3.7'
services:
  telegraf:
    ..
    hostname: "{{.Node.Hostname}}"

Permissions

Copy as User:

FROM python:2.7
RUN pip install Flask==0.11.1 
RUN useradd -ms /bin/bash admin
COPY --chown=admin:admin app /app
WORKDIR /app
USER admin
CMD ["python", "app.py"] 

Update

Service Update

Add a constraint to move to a worker node:

$ docker service update --constraint-add 'node.role==worker' my-service-name

Docker Compose

Docker Compose Build

In our Dockerfile we can specify:

ARG APP_VERSION=1
ENV APP_VERSION=$APP_VERSION

Then we can set or overwrite the value by using build arguments:

services:
  my-app:
    build:
      context: .
      dockerfile: test/Dockerfile
      args:
        APP_VERSION: 1.1

Docker Compose Healthchecks

To see how to use health checks for dependencies / conditions, look at the gist below:

services:
  php:
    build:
      context: .
      dockerfile: tests/Docker/Dockerfile-PHP
      args:
        version: cli
    depends_on:
      couchbase:
        condition: service_healthy

Credit to:

HTTP Healthcheck:

version: '2.1'
services:
    depends_on:
      couchbase:
        condition: service_healthy
  http-service:
    image: nginx
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:80/"]
      interval: 1s
      timeout: 3s
      retries: 60

MySQL Healthcheck:

  mysql:
    image: mysql
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
      - MYSQL_ROOT_PASSWORD=
      - MYSQL_DATABASE=test
    healthcheck:
      test: ["CMD", "mysql" ,"-h", "mysql", "-P", "3306", "-u", "root", "-e", "SELECT 1", "test"]
      interval: 1s
      timeout: 3s
      retries: 30

Postgres Healthcheck:

  postgresql:
    image: postgres
    environment:
      - POSTGRES_PASSWORD=
      - POSTGRES_DB=test
    healthcheck:
      test: ["CMD", "pg_isready"]
      interval: 1s
      timeout: 3s
      retries: 30

Redis Healthcheck:

  redis:
    image: redis
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 1s
      timeout: 3s
      retries: 30

Docker Compose Depends On

Depends-On Defaults, which will be wait for the database to start, before starting the web container:

version: "3.8"
services:
  web:
    build: .
    depends_on:
      - db
  db:
    image: postgres

Depends-On Types, where you can specify if it should wait for the service to start or to be healthy before starting the web container:

version: "3.8"
services:
  web:
    build: .
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started
  redis:
    image: redis
  db:
    image: postgres
    healthcheck:
      test: "exit 0"

Different types of depends on:

condition: service_started
condition: service_healthy
condition: service_completed_successfully