Does MySQL Work With Docker?

Fully CompatibleLast verified: 2026-02-26

MySQL and Docker work seamlessly together; Docker provides the ideal containerization platform for MySQL databases in development, testing, and production environments.

Quick Facts

Compatibility
full
Setup Difficulty
Easy
Official Integration
Yes ✓
Confidence
high
Minimum Versions
MySQL: 5.7
Docker: 1.12

How MySQL Works With Docker

MySQL integrates perfectly with Docker through the official MySQL container images maintained on Docker Hub. You pull the image, configure environment variables for the root password and database initialization, mount volumes for data persistence, and expose port 3306. The developer experience is excellent—spin up a fully configured MySQL instance in seconds without installing anything locally. Docker handles networking seamlessly, allowing other containers to communicate with MySQL via container names as hostnames. For production, you gain immutable infrastructure, easy scaling with orchestration tools like Kubernetes, and simplified CI/CD pipelines. The main architecture consideration is volume management: use named volumes or bind mounts to persist data across container restarts, otherwise your database disappears. Most teams use Docker Compose to orchestrate MySQL alongside application containers, defining the entire stack in a single YAML file.

Best Use Cases

Local development environments where developers need isolated MySQL instances without system-wide installation
CI/CD pipelines running automated tests against fresh MySQL databases for each test run
Microservices architectures where MySQL is deployed as a containerized service alongside other services
Multi-environment setups ensuring identical MySQL versions and configurations across development, staging, and production

Quick Setup

bash
docker pull mysql:8.0
bash
# Start MySQL container with persistent volume
docker run -d \
  --name mysql-app \
  -e MYSQL_ROOT_PASSWORD=rootpass \
  -e MYSQL_DATABASE=myapp \
  -e MYSQL_USER=appuser \
  -e MYSQL_PASSWORD=apppass \
  -v mysql_data:/var/lib/mysql \
  -p 3306:3306 \
  mysql:8.0

# Connect from host
mysql -h 127.0.0.1 -u appuser -p myapp

# Or from another container on same network
docker exec -it mysql-app mysql -u appuser -p myapp

Known Issues & Gotchas

critical

Data loss when container is deleted without proper volume configuration

Fix: Always use named volumes or bind mounts with `-v mysql_data:/var/lib/mysql` or specify volumes in docker-compose.yml. Never rely on container storage layers for persistent data.

warning

Performance degradation on Mac and Windows due to filesystem binding overhead

Fix: Use Docker Desktop's performance settings, consider using named volumes instead of bind mounts, or use WSL 2 backend on Windows for better I/O performance.

warning

Initialization scripts in the container don't execute if the volume already contains data

Fix: Mount initialization SQL files to `/docker-entrypoint-initdb.d/` only on first run, or manually initialize an empty volume before mounting.

info

Container networking requires understanding Docker's DNS—localhost won't work for inter-container communication

Fix: Use container names as hostnames (e.g., `mysql:3306` instead of `localhost:3306`) or ensure containers are on the same Docker network.

Alternatives

  • PostgreSQL with Docker—similar setup but with different SQL dialect and advanced features like JSON support
  • MongoDB with Docker—NoSQL alternative for document-based data without rigid schemas
  • MariaDB with Docker—MySQL-compatible fork that's fully open-source and containerizes identically to MySQL

Resources

Related Compatibility Guides

Explore more compatibility guides