This Docker Compose configuration provides a complete database solution with either MySQL or MariaDB as the database server and phpMyAdmin as the web-based management interface. This setup is ideal for development environments, testing, and learning purposes.
- Docker installed on your system
- Docker Compose (usually included with Docker Desktop)
services: db: image: mysql:8.0 platform: linux/amd64 volumes: - mysql_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD:-rootpassword} MYSQL_DATABASE: ${DB_NAME:-mydatabase} MYSQL_USER: ${DB_USER:-myuser} MYSQL_PASSWORD: ${DB_PASSWORD:-userpassword} ports: - "3306:3306" phpmyadmin: depends_on: - db image: phpmyadmin:latest platform: linux/amd64 restart: always ports: - "8090:80" environment: PMA_HOST: db MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD:-rootpassword} UPLOAD_LIMIT: 128M volumes: mysql_data: driver: local
services: db: image: mariadb:10.6 volumes: - mariadb_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD:-rootpassword} MYSQL_DATABASE: ${DB_NAME:-mydatabase} MYSQL_USER: ${DB_USER:-myuser} MYSQL_PASSWORD: ${DB_PASSWORD:-userpassword} ports: - "3306:3306" phpmyadmin: depends_on: - db image: phpmyadmin:latest restart: always ports: - "8090:80" environment: PMA_HOST: db MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD:-rootpassword} UPLOAD_LIMIT: 128M volumes: mariadb_data: driver: local
Available Images:
- MySQL:
mysql:8.0
- MariaDB:
mariadb:10.6
- Volume: Persistent storage at
/var/lib/mysql
using a named volume - Restart Policy: Always restart if container stops
- Port Mapping: Host port 3306 to container port 3306
- Environment Variables:
MYSQL_ROOT_PASSWORD
: Root password (default: 'rootpassword')MYSQL_DATABASE
: Initial database to create (default: 'mydatabase')MYSQL_USER
: Initial user to create (default: 'myuser')MYSQL_PASSWORD
: Password for the initial user (default: 'userpassword')
Connect to the database server directly:
mysql -h 127.0.0.1 -P 3306 -u root -p
Image: phpmyadmin:latest
- Dependency: Requires the
db
service to be running - Restart Policy: Always restart if container stops
- Port Mapping: Host port 8090 to container port 80
- Environment Variables:
PMA_HOST
: Database hostname (set to 'db' - the database service name)MYSQL_ROOT_PASSWORD
: Root password (must match the db service)UPLOAD_LIMIT
: Maximum file upload size (set to 128MB)
Access phpMyAdmin through your web browser at:
http://localhost:8090
Login credentials:
- Username:
root
- Password: The value set for
DB_ROOT_PASSWORD
(default: 'rootpassword')
Customize the configuration using environment variables either by:
- Setting them in your shell before running Docker Compose
- Creating a
.env
file in the same directory
Available variables with their defaults:
DB_ROOT_PASSWORD=rootpassword DB_NAME=mydatabase DB_USER=myuser DB_PASSWORD=userpassword
- MySQL version:
mysql_data
- Persistent volume for MySQL data storage - MariaDB version:
mariadb_data
- Persistent volume for MariaDB data storage - Both use the local driver for data persistence
- Choose either MySQL or MariaDB version and save as
docker-compose.yml
- (Optional) Create a
.env
file to customize environment variables - Run the following command:
docker-compose up -d
- Start services:
docker-compose up -d
- Stop services:
docker-compose down
- View logs:
docker-compose logs
- Monitor containers:
docker-compose ps
- Stop services but keep volumes:
docker-compose down --volumes
(to remove volumes, omit the flag)
- Always change default passwords for production environments
- Avoid exposing database port (3306) publicly without proper security measures
- Consider using Docker networks for internal communication only
- Use strong, complex passwords for database users
- Regularly update Docker images to receive security patches
Aspect | MySQL | MariaDB |
---|---|---|
License | Oracle-owned (GPLv2) | Open-source (GPLv2) |
Performance | Good for enterprise workloads | Often faster for read operations |
Features | Oracle-specific enhancements | Community-driven enhancements |
Compatibility | Standard MySQL | MySQL compatible with extras |
Storage Engines | Standard set | Additional engines like Aria, ColumnStore |
- MySQL Docker Hub: https://hub.docker.com/_/mysql
- MariaDB Docker Hub: https://hub.docker.com/_/mariadb
- phpMyAdmin Docker Hub: https://hub.docker.com/r/phpmyadmin/phpmyadmin
- Docker Documentation: https://docs.docker.com/
- Docker Compose Documentation: https://docs.docker.com/compose/
- Connection refused errors: Verify both containers are running with
docker-compose ps
- Port conflicts: Ensure ports 3306 and 8090 are not already in use
- Permission errors: Check volume permissions for database data directory
- Platform issues: For ARM devices, you may need to adjust platform settings
Check service logs for detailed error information:
docker-compose logs db # Database logs docker-compose logs phpmyadmin # phpMyAdmin logs docker-compose logs -f # Follow all logs in real-time
- Data persists between container restarts due to named volumes
- To completely reset, remove volumes with
docker-compose down -v
- Volume data is stored in Docker's managed volume directory
For additional support:
- Check official Docker documentation for both MySQL and MariaDB
- Review phpMyAdmin documentation for web interface issues
- Consult Docker community forums for platform-specific issues