All notes
Compos

Intro

Compose yml

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).

Command line


########## 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".