π Sample YAML File for Docker-Compose
version: '3.8'
services:
π web:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./web-content:/usr/share/nginx/html
networks:
- frontend
depends_on:
- api
π§ api:
image: node:14
working_dir: /app
environment:
NODE_ENV: production
volumes:
- ./api:/app
networks:
- frontend
- backend
ports:
- "3000:3000"
ποΈ db:
image: postgres:13
environment:
POSTGRES_DB: mydb
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
volumes:
- postgres-data:/var/lib/postgresql/data
networks:
- backend
π monitoring:
image: prom/prometheus
ports:
- "9090:9090"
volumes:
- ./prometheus-config:/etc/prometheus
networks:
- backend
networks:
π frontend:
driver: bridge
π backend:
driver: bridge
volumes:
π¦ postgres-data:
π¦ prometheus-config:π Explanation
Services
-
π web:
- Image: Uses the lightweight
nginx:alpineimage. - Ports: Maps port 80 on the host to port 80 on the container.
- Volumes: Mounts the
./web-contentdirectory on the host to/usr/share/nginx/htmlin the container. - Networks: Connected to the
frontendnetwork. - Depends_on: Depends on the
apiservice.
- Image: Uses the lightweight
-
π§ api:
- Image: Uses the
node:14image. - Working Directory: Sets
/appas the working directory. - Environment: Sets
NODE_ENVtoproduction. - Volumes: Mounts the
./apidirectory on the host to/appin the container. - Networks: Connected to both
frontendandbackendnetworks. - Ports: Maps port 3000 on the host to port 3000 on the container.
- Image: Uses the
-
ποΈ db:
- Image: Uses the
postgres:13image. - Environment: Sets up the database with
POSTGRES_DB,POSTGRES_USER, andPOSTGRES_PASSWORD. - Volumes: Mounts a named volume
postgres-datato/var/lib/postgresql/data. - Networks: Connected to the
backendnetwork.
- Image: Uses the
-
π monitoring:
- Image: Uses the
prom/prometheusimage. - Ports: Maps port 9090 on the host to port 9090 on the container.
- Volumes: Mounts the
./prometheus-configdirectory on the host to/etc/prometheusin the container. - Networks: Connected to the
backendnetwork.
- Image: Uses the
Networks
- π frontend: A bridge network for the
webandapiservices. - π backend: A bridge network for the
api,db, andmonitoringservices.
Volumes
- π¦ postgres-data: A named volume for persisting PostgreSQL data.
- π¦ prometheus-config: A named volume for storing Prometheus configuration files.