Docker and MySQL: A Technical Guide for Developers

SimpleBackups founder

Laurent Lemaire

Co-founder, SimpleBackups

January 31, 2025

tl;dr

Docker revolutionizes database management by providing lightweight, reproducible environments for MySQL. This guide walks you through Docker installation, MySQL container setup, version management, and robust backup strategies.

Understanding Docker: The Container Revolution

What is Docker?

Docker is a powerful platform that enables developers to create, deploy, and run applications using containers. Unlike traditional virtual machines, containers:

  • Virtualize at the operating system level
  • Are lightweight and resource-efficient
  • Ensure consistent environments across development and production
  • Simplify dependency management
  • Enable rapid scalability and deployment

Docker Installation Guide

For Linux (Ubuntu/Debian):

# Update package index
sudo apt-get update

# Install dependencies
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# Set up stable repository
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

# Install Docker CE
sudo apt-get update
sudo apt-get install docker-ce

For macOS:

  1. Download Docker Desktop from official Docker website
  2. Install the .dmg file
  3. Launch Docker Desktop
  4. Complete initial setup wizard

For Windows:

  1. Download Docker Desktop for Windows
  2. Enable Hyper-V and Windows Containers features
  3. Install and restart your system

MySQL Container Management

Creating Your First MySQL Container

# Pull MySQL official image
docker pull mysql:latest

# Run MySQL container
docker run --name my-mysql-container
  -e MYSQL_ROOT_PASSWORD=your_secure_password
  -p 3306:3306
  -d mysql:latest

Managing Multiple MySQL Versions

Different projects often require different MySQL versions. Docker simplifies this complexity:

# MySQL 5.7
docker run --name mysql-5.7
  -e MYSQL_ROOT_PASSWORD=password57
  -p 3307:3306
  -d mysql:5.7

# MySQL 8.0
docker run --name mysql-8.0
  -e MYSQL_ROOT_PASSWORD=password80
  -p 3308:3306
  -d mysql:8.0

Switching Between MySQL Containers

  1. List running containers:
docker ps
  1. Stop current container:
docker stop <container_name>
  1. Start desired container:
docker start <target_container_name>

Database Backup Strategies

Manual Backup Command

# Backup entire database
docker exec CONTAINER_NAME mysqldump -u root -p database_name > backup.sql

# Backup specific tables
docker exec CONTAINER_NAME mysqldump -u root -p database_name table1 table2 > specific_backup.sql

Automated Backups with SimpleBackups

If you want to automate your backups securely and store your backups remotely with confidence, leveraging anomaly detection, granular access controls, and comprehensive security features that meet the most stringent production requirements, SimpleBackups was purpose-built for exactly this scenario.

Crafted with production environments in mind, the platform delivers a smooth, intuitive user interface that enables rapid setup and provides the ultimate peace of mind for technical teams who cannot compromise on data protection and operational reliability.

Conclusion

Docker transforms MySQL management from complex infrastructure to flexible, reproducible environments. By mastering containerization, developers can focus on building great applications rather than managing intricate database setups.

Key Takeaways

  • Docker provides lightweight, consistent MySQL environments
  • Multiple MySQL versions can coexist seamlessly
  • Backup strategies are crucial for data protection
  • Containerization simplifies database management

Pro Tip: Treat your containers like cattle, not pets. Be prepared to destroy and recreate them without hesitation.



Back to blog

Stop worrying about your backups.
Focus on building amazing things!

Free 7-day trial. No credit card required.

Have a question? Need help getting started?
Get in touch via chat or at [email protected]

Customer support with experts
Security & compliance
Service that you'll love using