Infrastructure Configuration
Configure infrastructure services like databases, caches, and queues.
Basic Infrastructure
provides:
mongodb:
type: database
image: mongo:7
port: 27017Full Configuration
provides:
mongodb:
type: database
image: mongo:7
port: 27017
description: Primary database
# Data persistence
data_dir: /data/db
# Additional ports
additional_ports:
admin: 27018
# Environment variables for container
env:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD}
MONGO_INITDB_DATABASE: myapp
# Volume mounts
volumes:
./init-mongo.js: /docker-entrypoint-initdb.d/init.js
# Health check
healthcheck: mongosh --eval "db.adminCommand('ping')"
# Replica set (for transactions)
replicaSet: rs0Common Services
MongoDB
provides:
mongodb:
type: database
image: mongo:7
port: 27017
data_dir: /data/db
env:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: secretRedis
provides:
redis:
type: cache
image: redis:7-alpine
port: 6379
data_dir: /data # For persistenceRabbitMQ
provides:
rabbitmq:
type: queue
image: rabbitmq:3-management
port: 5672
additional_ports:
management: 15672
env:
RABBITMQ_DEFAULT_USER: guest
RABBITMQ_DEFAULT_PASS: guestPostgreSQL
provides:
postgres:
type: database
image: postgres:16
port: 5432
data_dir: /var/lib/postgresql/data
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: secret
POSTGRES_DB: myappElasticsearch
provides:
elasticsearch:
type: search
image: elasticsearch:8.11.0
port: 9200
additional_ports:
transport: 9300
env:
discovery.type: single-node
ES_JAVA_OPTS: "-Xms512m -Xmx512m"
xpack.security.enabled: "false"MinIO (S3-compatible storage)
provides:
minio:
type: storage
image: minio/minio:latest
port: 9000
additional_ports:
console: 9001
env:
MINIO_ROOT_USER: minioadmin
MINIO_ROOT_PASSWORD: minioadminMailhog (Email testing)
provides:
mailhog:
type: email
image: mailhog/mailhog:latest
port: 1025
additional_ports:
web: 8025Infrastructure Types
| Type | Purpose | Examples |
|---|---|---|
database | Data storage | MongoDB, PostgreSQL, MySQL |
cache | Caching layer | Redis, Memcached |
queue | Message queues | RabbitMQ, Kafka |
storage | Object storage | MinIO, LocalStack S3 |
search | Search engines | Elasticsearch, Typesense |
email | Email services | Mailhog, MailCatcher |
custom | Other services | Any Docker image |
Profile-Specific Infrastructure
Control infrastructure per profile:
profiles:
quick:
apps: [frontend]
# No infrastructure - connect to staging
full:
apps: [frontend, backend]
connections:
backend:
mongodb: local # Use local MongoDB
redis: local # Use local Redis
rabbitmq: staging # Use staging RabbitMQConnection URLs
Apps receive connection URLs via environment variables:
apps:
backend:
connects_to:
mongodb:
mode: local
env:
MONGODB_URI: mongodb://root:secret@localhost:27017/myapp?authSource=admin
redis:
mode: local
env:
REDIS_URL: redis://localhost:6379Health Checks
Configure health checks for infrastructure:
provides:
mongodb:
type: database
image: mongo:7
port: 27017
healthcheck:
command: mongosh --eval "db.adminCommand('ping')"
interval: 10s
timeout: 5s
retries: 5Volumes and Persistence
provides:
mongodb:
type: database
image: mongo:7
port: 27017
data_dir: /data/db # Mounted for persistence
volumes:
# Mount initialization scripts
./scripts/init-db.js: /docker-entrypoint-initdb.d/init.js:ro
# Mount config files
./config/mongod.conf: /etc/mongod.conf:roBest Practices
- Use specific versions: Avoid
latesttags - Configure health checks: Ensure services are ready
- Set resource limits: Prevent runaway containers
- Use secrets for passwords: Don’t hardcode in config
- Document custom configs: Explain non-obvious settings
Last updated on