Simple docker-compose example
Create file docker-compose.yml or compose.yml
YAML
services:
database:
container_name: fitness_mysql
image: 'mysql:8.0'
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: dbname
MYSQL_USERNAME: dbuser
MYSQL_PASSWORD: dbpass
volumes:
- ./mysql-data:/var/lib/mysql
- ./conf:/etc/mysql/conf.d # for adding custom.ini
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-proot"]
retries: 3
timeout: 5s
restart: unless-stopped
backend:
container_name: backend
image: <docker repo>:<tag>
ports:
- "3001:3001"
restart: unless-stopped
depends_on:
database:
condition: service_healthy
frontend:
container_name: frontend
image: <docker repo>:<tag>
ports:
- "3000:3000"
restart: unless-stopped
nginx:
image: nginx:1.27.3
container_name: nginx
ports:
- "80:80"
restart: unless-stopped
depends_on:
database:
condition: service_healthy
volumes:
- ./nginx-config:/etc/nginx/conf.d
networks:
default:
external: true
name: main-net
- make sure to create network before running docker-compose up -d
(main-net is an example)
In this case you do not have to define network for each service and if want to add another docker stack simply add network part: