Docker Compose File Guide
Overview
The Docker Compose file is a YAML file defining services, networks, and volumes. This file is used to spin up multiple containers at once using docker-compose CLI tool. It's designed to be a part of your development process rather than being deployed on its own.
In this guide, we will explore the docker-compose.yml file in detail and provide steps for its installation and usage.
Understanding Docker Compose File
Let's take a look at an example of a docker compose file:
version: '3'
services:
db:
image: 'jc21/mariadb-aria:latest'
container_name: npm-db
restart: unless-stopped
env_file: .env
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MARIADB_AUTO_UPGRADE=1
volumes:
- ./data/mysql:/var/lib/mysql
networks:
- proxy
- proxydb
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u${MYSQL_USER}", "-p${MYSQL_PASSWORD}"]
interval: 20s
timeout: 5s
retries: 5
start_period: 30s
app:
image: 'jc21/nginx-proxy-manager:latest'
container_name: nginx-proxy-manager
restart: unless-stopped
env_file: .env
environment:
- TZ=${TZ}
- DB_MYSQL_HOST=db
- DB_MYSQL_PORT=3306
- DB_MYSQL_USER=${MYSQL_USER}
- DB_MYSQL_PASSWORD=${MYSQL_PASSWORD}
- DB_MYSQL_NAME=${MYSQL_DATABASE}
ports:
- '80:80'
- '81:81'
- '443:443'
volumes:
- ./data/npm:/data
- ./data/letsencrypt:/etc/letsencrypt
depends_on:
- db
networks:
- proxy
- proxydb
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:81/api/status"]
interval: 20s
timeout: 5s
retries: 5
start_period: 40s
networks:
proxy:
external: true
proxydb:
external: true
This Docker Compose file defines two services, db and app. Each service is defined with its own configuration such as the image to use, ports to expose, volumes to mount, etc. It also has a network section where it lists the networks that these services should connect to.
.env
MYSQL_ROOT_PASSWORD=rhDGjilZm6XEMJ
MYSQL_DATABASE=npm
MYSQL_USER=npm
MYSQL_PASSWORD=rhDGjilZm6XEMJ
# TZ for container timezone
TZ=America/New_York
Installation Guide
-
Install Docker: Follow instructions on the official Docker website for your specific operating system.
-
Install Docker Compose: On Linux, run
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-composefollowed bysudo chmod +x /usr/local/bin/docker-compose. On Windows or MacOS, follow the instructions on Docker Compose's Github page. -
Pull Docker Images: Before running docker-compose up, make sure to pull all necessary images with
docker-compose pullcommand.
Running the Services
-
Running the services: In your terminal, navigate to the directory containing your docker-compose file and run
docker-compose up -d. This will start all the services defined in the compose file. -
Checking the running status of services: You can check the status of running services with
docker-compose pscommand.
Remember to replace placeholders like ${MYSQL_ROOT_PASSWORD}, ${MYSQL_DATABASE}, and ${MYSQL_USER}, etc., in your environment variables files (.env) with actual values before starting the services. Also, ensure all paths mentioned in volumes section exist on your system or adjust them accordingly.
Conclusion
With this guide you should now have a solid understanding of Docker Compose file and how to use it for running multi-container applications. It's worth noting that Docker Compose is just one tool among many, each having its own strengths and weaknesses depending on the specific use case.