Table of Contents
What is docker compose and what are its benefits
Docker compose is a command-line interface for defining and running multi-container Docker applications. If you have multiple containers that you want to run together, but with different configurations, then docker compose will be a good option for you. Docker compose only runs on Linux and Mac.
How to install it?
Let’s install Docker compose in your directory, which also installs the docker tool. Before starting to install docker compose, check if you have enabled the package repository for docker-ce.
sudo pacman -S docker-ce sudo systemctl enable docker && sudo systemctl start docker 1 2 sudo pacman – S docker – ce sudo systemctl enable docker && sudo systemctl start docker
The following command will install the latest version of Docker Compose, which is 1.6 as of writing.

How to create a docker-compose file
All compose files are written in YAML format. They are composed of settings, services and networks. They are used to pull images and start containers. A file’s name will be docker-compose.yaml.
Below is an example of a docker-compose file:
version: ‘3.1’
services:
web:
build: .
command: python app.py
ports: 8080 : 9000 9001 : 9010 9002 : 9020 -9030 -9091 http_proxy=localhost https_port=9443 volumes_from=db container_name=web [email protected] [email protected]: /etc/default/postgresql [email protected]: db
version: ‘3.1’
services: web : build : . command : python app.py ports : – 8080 : 9000 – 9001 : 9010 – 9002 : 9020 – 9030 – 9091 http_proxy = localhost https_port = 9443 volumes_from = db container_name = web db @ dbhost . domain . tld db @ dbhost . domain . tld : / etc / default / postgresql web @ webhost . org : db
services section [show headlines only, not full copy]: “services “

Examples of docker-compose files for different applications
A docker-compose file allows the user to describe services and links between them, so that when it’s started, all the service will be up and running. This is useful for quickly creating and running a service locally, or in production. Here are some examples of docker-compose files for different applications.
If you decide to use docker-compose files, please don’t. For different services, you need to create docker-compose.yml file and ENV variables will be set.
1. Database
Useful when you want to test your application without the need of the real database and want to bring the database up and down painlessly.
version: “2”
services:
db: image: mysql:5.7 volumes: – ./data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: rootvol MYSQL_DATABASE: test MYSQL_USERNAME : docker MYSQL_PASSWORD : docker restart : always ports: – “3306” networks : – mynet volumes : networks : mynet : driver : bridge

Tips for troubleshooting docker compose
The docker-compose down command will stop services, but leave any images created by those stopped services intact. The docker-compose rm command removes all images and containers, as well as the networks that were created in the container. The docker-compose rm –all will remove all images and containers, as well as all networks and volumes.
To keep all of your containers running, but change one container, use the –build option to docker-compose up . By default, –build will start any containers that you previously created using docker-compose up , even if those containers are no longer defined in the docker-compose.yml file. To prevent a container from being started again when you rebuild the stack, you can add a line to the Dockerfile that specifies an ENV with a key and value of STOP_ON_REBUILD .
