Integrating Docker with Databases Running and Scaling MySQL PostgreSQL and MongoDB Containers
Learn how to containerize, configure, and scale relational and NoSQL databases with Docker for development and production
Docker has transformed how developers manage application environments—and databases are no exception. Whether you’re using MySQL, PostgreSQL, or MongoDB, Docker makes it easy to spin up isolated and consistent database environments for local development, testing, and even production at scale.
This guide walks through how to run and scale databases in Docker containers, with a focus on configuration, data persistence, Docker Compose, and best practices for each database engine.
Benefits of Running Databases in Docker
- Isolated environments for development and testing
- Easy-to-replicate configurations using Dockerfiles or Compose
- Portability across systems and teams
- Simplified setup for CI/CD and microservices
- Declarative infrastructure using YAML and version control
However, running databases in containers for production demands careful handling of persistence, networking, and performance.
Running MySQL in Docker
Simple MySQL Container
docker run -d \
--name mysql-db \
-e MYSQL_ROOT_PASSWORD=secret \
-e MYSQL_DATABASE=appdb \
-v mysql-data:/var/lib/mysql \
-p 3306:3306 \
mysql:8.0
Best Practices:
- Mount volumes for persistent data (
-v
) - Set proper user and database environment variables
- Avoid exposing MySQL to public networks
- Use
healthcheck
in Compose to ensure readiness
Running PostgreSQL in Docker
Basic PostgreSQL Command
docker run -d \
--name postgres-db \
-e POSTGRES_USER=admin \
-e POSTGRES_PASSWORD=secret \
-e POSTGRES_DB=appdb \
-v pg-data:/var/lib/postgresql/data \
-p 5432:5432 \
postgres:15
PostgreSQL Tips:
- Use Alpine or Slim images for lightweight containers
- Configure
max_connections
,shared_buffers
viapostgresql.conf
- Enable logging for query and error tracking
- Consider WAL volume separation for high write throughput
Running MongoDB in Docker
Basic MongoDB Container
docker run -d \
--name mongo-db \
-v mongo-data:/data/db \
-p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=secret \
mongo:6.0
MongoDB Recommendations:
- Use
wiredTiger
storage engine for better performance - Store data in mounted volumes for durability
- Enable authentication in non-local deployments
- Use replica sets if running MongoDB clusters
Using Docker Compose for Multi-Container Environments
Example docker-compose.yml
for MySQL + App
version: '3.8'
services:
mysql:
image: mysql:8.0
restart: always
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: appdb
volumes:
- mysql-data:/var/lib/mysql
ports:
- "3306:3306"
app:
image: myapp:latest
depends_on:
- mysql
ports:
- "8080:8080"
volumes:
mysql-data:
You can adapt this pattern to use PostgreSQL or MongoDB.
Scaling Database Containers
Scaling read replicas is common for databases in containerized environments.
For MySQL:
- Use MySQL Group Replication or Galera Cluster
- Scale read replicas behind load balancers
For PostgreSQL:
- Use tools like
pgpool
,Patroni
, orCrunchyData
for HA setups - Replication using streaming or logical replication
For MongoDB:
- Deploy MongoDB with replica sets for fault tolerance
- Use sharding for horizontal scaling in production
- Run each member in a separate container and expose ports internally
Persistent Data and Volumes
Data persistence is critical. Always mount database data directories to host volumes or Docker volumes:
docker volume create pg-data
docker run -v pg-data:/var/lib/postgresql/data postgres
Use named volumes in Compose or bind mounts for specific paths.
Networking and Security
- Use Docker networks (
--network
) to isolate database traffic - Disable external port exposure unless explicitly needed
- Encrypt traffic (PostgreSQL SSL, MongoDB TLS, MySQL SSL)
- Use
.env
files to avoid hardcoding passwords in Compose files
Monitoring and Backup Strategies
- Use tools like pgAdmin, MySQL Workbench, or Mongo Express in side containers
- Automate backups using
cron
,mysqldump
,pg_dump
, ormongodump
- Monitor health and metrics with Prometheus exporters for each DB engine
Conclusion
Docker makes it easier than ever to containerize databases, but you must take steps to ensure durability, performance, and scalability. Whether you’re developing locally or deploying production-grade environments, combining Docker with MySQL, PostgreSQL, and MongoDB can dramatically simplify your stack—when done right.
With Docker Compose, volume management, and clustering strategies, your database infrastructure becomes more portable, consistent, and cloud-ready.