All notes
Compos

docker-compose.yml

The default path for a Compose file is ./docker-compose.yml. You can use either a .yml or .yaml extension for this file.

Intro

Example 1:


version: '3'
services:
  db:
    image: postgres
  web:
    build: .
    # Or:
    # build
    #   context: .
    #   dockerfile: Dockerfile-alternate
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

Example 2. docs.docker.com/docker-compose.


version: "3" // This is the compose file version.
services:
  web:
    # replace username/repo:tag with your name and image details
    image: username/repository:tag
    deploy:
      replicas: 5 # Run five instances of that image as a service called 'web'.
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "80:80"
    networks:
      - webnet # Share port 80 via a load-balanced network called 'webnet'
networks:
  webnet: # Define the webnet network with the default settings (which is a load-balanced overlay network).

services

Top-levels: services, networks and volumes.

A service definition contains configuration is much like passing command-line parameters to docker run. Likewise, network and volume definitions are analogous to docker network create and docker volume create.

docs.docker.com: service configuration reference.

depends_on

Express dependency between services, which has two effects:

ports



ports:
 - "3000"
 - "3000-3005"
 - "8000:8000"
 - "9090-9091:8080-8081"
 - "49100:22"
 - "127.0.0.1:8001:8001"
 - "127.0.0.1:5000-5010:5000-5010"
 - "6060:6060/udp"

volumes


services:
  appName:
    volumes:
      # Just specify a path and let the Engine create a volume
      - /var/lib/mysql
    
      # Specify an absolute path mapping
      - /opt/data:/var/lib/mysql
    
      # Path on the host, relative to the Compose file
      - ./cache:/tmp/cache
    
      # User-relative path
      - ~/configs:/etc/configs/:ro
    
      # Named volume
      - datavolume:/var/lib/mysql
volumes:

tty

so: interactive shell using docker compose.

The canonical way to get an interactive shell with docker-compose is to use: docker-compose run --rm myapp.

You can set stdin_open: true, tty: true, however that won't actually give you a proper shell with "up", because logs are being streamed from all the containers.

wcfNote: this is not recommended! The first corresponds to "-i" in docker run and the second to "-t".


stdin_open: true
tty: true

Disable a service

so: disable a service in docker-compose.yml.

You could simply redefine the entrypoint or command in order to replace said command with something which does nothing (/bin/true). That would make the container exit immediately, doing nothing.

volumes

It allows you to create named volumes.


version: "3"

services:
  db:
    image: db
    volumes:
      - data-volume:/var/lib/db
  backup:
    image: backup-service
    volumes:
      - data-volume:/var/lib/backup/data

volumes:
  data-volume:

Variable substitution



// POSTGRES_VERSION=9.3 and you supply this configuration:
db:
  image: "postgres:${POSTGRES_VERSION}"
// When you run docker-compose up, compose resolves the image to "postgres:9.3".

${VARIABLE:-default} will evaluate to default if VARIABLE is unset or empty in the environment.
${VARIABLE-default} will evaluate to default only if VARIABLE is unset in the environment.

You can set default values for environment variables using a ".env" file. The .env file feature only works when you use the docker-compose up command and does not work with docker stack deploy.

Commandline


########## Non-swarn
# Run version 3 files with non swarm configurations
docker-compose up
# In background.
docker-compose up -d

# Run one-off commands for your services
# To see what environment variables are available to the web service:
docker-compose run web env

# Stop the containers.
docker-compose stop

# Remove the containers.
docker-compose down [--volumes]

########## Swarm
docker swarm init

# Only supported on Compose files version 3.x and up
docker stack deploy

# Give a name 'getstartedlab'
docker stack deploy -c docker-compose.yml getstartedlab
# See a list of the five containers you just launched:
docker stack ps getstartedlab

docker stack ls
docker stack services appname
docker stack ps appname   # List the running containers associated with an app
docker stack rm appname                             # Tear down an application

# Take down
docker stack rm getstartedlab

# See node
docker node ls
# Take down the swarm with
docker swarm leave --force

Database

MySQL

SO: docker compose persistent data MySQL.


version: '2'
services:
  mysql:
    container_name: flask_mysql
    restart: always
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: 'test_pass' # TODO: Change this
      MYSQL_USER: 'test'
      MYSQL_PASS: 'pass'
    volumes:
      - my-datavolume:/var/lib/mysql
volumes:
  my-datavolume:
// Docker will create the volume for you in the /var/lib/docker/volumes folder. This volume persist as long as you are not typing "docker-compose down -v".