Docker Compose - BKJackson/BKJackson_Wiki GitHub Wiki

Docker Compose aggregates individual docker containers and can set dependencies.

How Docker Compose Works

  1. use yaml files to configure application services (docker-compose.yaml)
  2. can start all the services with a single command (docker-compose up)
  3. can stop all the service with a single command (docker-compose down)
  4. able to scale up the specific services when required
  5. works in all environments: production, staging, development, testing, as well as CI workflows
    5a. Use environment variables from shell or .env file
    5b. Merge multiple Compose files into one, e.g., base config in one file and a deployment-specific config in other files.

Docker compose database setup Source

Note: Add data to .gitignore file

version: '3'

services: 
  db:
    image: mysql:latest
    volumes:
      - "./data:/var/lib/mysql"
    restart: always
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: password  

Enter the container with bash:
docker-compose exec --user root db /bin/bash
Enter the MySQL shell:
docker-compose exec --user root db mysql -u root -p
When prompted enter password as the password.
Create a database:
CREATE DATABASE mydatabase;

View docker-compose.yml config for active container network

docker-compose config  

Grant sudo access to jovyan

environment:
  GRANT_SUDO: "yes"
user: root  

Override original docker-compose.yml with a second one

docker-compose -f docker-compose.yml -f docker-compose.second.yml up  

Alternatively, you can create an override file called docker-compose.override.yml and just type

docker-compose up  

Source

To run a series of commands in a docker container with docker-compose

docker-compose run <name in yml> sh -c '<command 1> && <command 2> && <command 3>'  

Docker compose for creating and destroying isolated testing environments

$ docker-compose up -d
$ ./run_tests
$ docker-compose down  

Docker command to deploy in production with your docker-compose file

docker stack deploy --compose-file docker-compose.yml --with-registry-auth my-stack-name  

Dealing with linked containers dependency in docker-compose

Docker compose environment setup (.env)

Using Docker Compose for Development Environment Dependencies

YAML / YML tips

PyYAML docs - A YAML parser and emitter for Python. Docs include tutorials and tips.

Docker compose for databases

Local Docker DB - A collection of Docker Compose files I've used to quickly spin local databases of various sorts., Alex MacArthur github

Docker compose for dev to prod environments

Docker Compose from dev to prod - Good tips
Configure Docker project for different environments using Docker Compose 3 - Sep 6, 2018
Use Compose in production - Official Docker docs
Efficient development with Docker and docker-compose - July 31, 2017

Tutorials

Docker Compose in 12 Minutes - Simple example, includes flask and flask_restful api examples.
“What, Why, How” Docker Compose? - Apr 29, 2019 "Docker compose is a simple yet powerful tool that is used to run multiple containers as a single service."
Getting Started with Docker Compose (Official Docs)
Define named volume with host mount in the docker compose file - example of how to specify .env variables in docker-compose.yml files
Using Docker Compose to Run Your Applications - Oct 16,2018

⚠️ **GitHub.com Fallback** ⚠️