Skip to content

Instantly share code, notes, and snippets.

@iagoalonsomrf
Last active October 21, 2022 14:31
Show Gist options
  • Save iagoalonsomrf/c57ede4ba0867d83b5fcbcca5ba54ba6 to your computer and use it in GitHub Desktop.
Save iagoalonsomrf/c57ede4ba0867d83b5fcbcca5ba54ba6 to your computer and use it in GitHub Desktop.
Best practices for writing docker-compose files
version: "3" # specify compose spec version
services:
db: # define a service name (docker-compose restart db)
image: postgres:13 # specify an image tag
container_name: db # define a container name (docker logs -f db)
restart: unless-stopped # specify an appropiate restart policy
volumes:
- psql_data:/var/lib/postgresql # use named volumes
healthcheck:
test: "[ -S /var/run/postgresql/.s.PGSQL.5432 ]"
interval: 20s
timeout: 5s
retries: 3
start_period: 10s
networks:
- internal
backend:
build: . # build image instead of pulling
container_name: backend
restart: unless-stopped
networks:
- internal
depends_on:
- db
frontend:
build: # long syntax for building an image
context: /opt/frontend
dockerfile: Dockerfile
container_name: frontend
restart: unless_stopped
command : npm start
networks:
- internal
depends_on:
- backend
proxy:
image: nginx:stable
container_name: proxy
restart: always
ports:
- 80:80
- 443:443
volumes:
- nginx_config:/etc/nginx/conf.d
networks:
- internal
depends_on:
- frontend
networks:
internal:
driver: bridge
volumes:
postgres_data: # long syntax for named volumes
driver: local
driver_opts:
type: "none"
o: "bind"
device: "/data/postgresql"
nginx_config:
driver: local
driver_opts:
type: "none"
o: "bind"
device: "/opt/nginx/conf.d"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment