1. Create .env.example
- Have .env.example
- Have .env locally
- Secrets not hardcoded
- .env is ignored
2.Clean docker-compose structure
- postgres
- redis
- migrate job
- api
- workers
- correct depends_on
- healthchecks
- restart policies (if added)
- entrypoint override for workers
- proper port mapping
3. Add Restart policies
1. Create .env.example at project root to push to git. Remove secrets
DATABASE_URL="postgres://postgres:postgres@localhost:5432/postgres_go?sslmode=disable"
JWT_SECRET=runState_Go
APP_ENV=dev
ADMIN_EMAIL=
ADMIN_PASSWORD=
MAILGUN_DOMAIN=
MAILGUN_API_KEY=
MAILGUN_SENDER=
MONITORING_STREAM=betteruptime:websites-ticks
# ap-south-1 region id
MONITORING_REGION_NAME=ap-south-1
MONITORING_GROUP=monitoring-group
MONITORING_CONSUMER=monitoring-consumer
STATUS_CHANGE_STREAM=betteruptime:website-status-change
STATUS_CHANGE_GROUP=status-change-group
STATUS_CHANGE_CONSUMER=status-change-consumer
NOTIFICATION_STREAM=betteruptime:website-status-notification
NOTIFICATION_GROUP=notification-group
NOTIFICATION_CONSUMER=notification-consumer
2. Update docker-compose.yml to fetch secrets from .env file
services:
postgres:
image: postgres:16
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=postgres_go
ports:
- "5432:5432"
volumes:
- "pgdata:/var/lib/postgresql/data"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres -d postgres_go"]
interval: 5s
timeout: 3s
retries: 30
start_period: 20s
redis:
image: redis:7
ports:
- "6379:6379"
volumes:
- redisdata:/data
command: ["redis-server", "--appendonly", "yes"]
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 3s
retries: 30
migrate:
image: migrate/migrate:v4.17.1
depends_on:
postgres:
condition: service_healthy
volumes:
- ./apps/api-go/migrations:/migrations
command: [
"-path", "/migrations",
"-database", "postgres://postgres:postgres@postgres:5432/postgres_go?sslmode=disable",
"up"
]
restart: on-failure
api:
build:
context: .
dockerfile: apps/api-go/Dockerfile
image: runstate-api-go:local
command: ["/server"]
ports:
- "3001:3001"
env_file:
- .env
environment:
- DATABASE_URL=postgres://postgres:postgres@postgres:5432/postgres_go?sslmode=disable
- REDIS_ADDR=redis:6379
depends_on:
migrate:
condition: service_completed_successfully
redis:
condition: service_healthy
monitoring-pusher:
image: runstate-api-go:local
entrypoint: ["/monitoring-pusher"]
env_file:
- .env
environment:
- DATABASE_URL=postgres://postgres:postgres@postgres:5432/postgres_go?sslmode=disable
- REDIS_ADDR=redis:6379
depends_on:
migrate:
condition: service_completed_successfully
redis:
condition: service_healthy
restart: unless-stopped
worker-monitoring:
image: runstate-api-go:local
entrypoint: ["/worker-monitoring"]
env_file:
- .env
environment:
- DATABASE_URL=postgres://postgres:postgres@postgres:5432/postgres_go?sslmode=disable
- REDIS_ADDR=redis:6379
depends_on:
migrate:
condition: service_completed_successfully
redis:
condition: service_healthy
restart: unless-stopped
worker-status-change:
image: runstate-api-go:local
entrypoint: ["/worker-status-change"]
env_file:
- .env
environment:
- DATABASE_URL=postgres://postgres:postgres@postgres:5432/postgres_go?sslmode=disable
- REDIS_ADDR=redis:6379
depends_on:
migrate:
condition: service_completed_successfully
redis:
condition: service_healthy
restart: unless-stopped
worker-notification:
image: runstate-api-go:local
entrypoint: ["/worker-notification"]
env_file:
- .env
environment:
- DATABASE_URL=postgres://postgres:postgres@postgres:5432/postgres_go?sslmode=disable
- REDIS_ADDR=redis:6379
depends_on:
migrate:
condition: service_completed_successfully
redis:
condition: service_healthy
restart: unless-stopped
volumes:
pgdata:
redisdata:
docker compose down -v
docker compose up --build