How to Install MySQL on Docker with Docker Compose
Setting up MySQL on Docker using Docker Compose is an efficient way to manage databases. Docker simplifies container management, and Docker Compose further streamlines multi-container applications. This guide will take you step-by-step through the process of installing MySQL on Docker with Docker Compose.
What is Docker and Docker Compose?
Before diving into the steps, it’s essential to understand Docker and Docker Compose.
- Docker is a platform that allows you to run applications in containers. Containers are lightweight and include everything your application needs to run.
- Docker Compose is a tool for defining and running multi-container Docker applications. It lets you define services, networks, and volumes in a single YAML file.
By using Docker and Docker Compose, you can quickly set up a MySQL container in a few steps.
Prerequisites
Before we begin, you will need:
- Docker installed on your system.
- Docker Compose installed.
- Basic knowledge of the command line interface (CLI).
Step 1: Set up Docker and Docker Compose
First, ensure Docker and Docker Compose are installed.
- Check Docker Installation: Open a terminal and type the following command:
docker --version
This should return the installed version of Docker.
- Check Docker Compose Installation: Use this command to check Docker Compose:
docker-compose --version
If both commands return their respective versions, you are ready to proceed.
Step 2: Create a Project Directory
To organize your files, create a dedicated directory for the MySQL project.
- Run this command to create a new directory:
mkdir mysql-docker-setup
- Navigate to the directory:
cd mysql-docker-setup
This directory will contain your docker-compose.yml
file, which defines the MySQL container.
Step 3: Create a Docker Compose File
Now, create a docker-compose.yml
file. This file describes the services (in this case, MySQL) that Docker will manage.
- Inside your project directory, create the file:
touch docker-compose.yml
- Open the file in a text editor and add the following content:
version: '3.8'
services:
db:
image: mysql:latest
container_name: mysql_container
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: mydatabase
MYSQL_USER: user
MYSQL_PASSWORD: password
ports:
- "3306:3306"
volumes:
- mysql-data:/var/lib/mysql
volumes:
mysql-data:
Explanation of the Docker Compose File
- version: Specifies the version of Docker Compose. Here, we are using version 3.8.
- services: Defines the containers we want to run. We only have one service,
db
, which will run MySQL. - image: Specifies the MySQL Docker image we want to use. The
latest
tag pulls the most recent version of MySQL. - container_name: Gives the container a custom name (
mysql_container
). - environment: Defines environment variables for MySQL.
MYSQL_ROOT_PASSWORD
: The password for the root user.MYSQL_DATABASE
: The name of a default database created when the container starts.MYSQL_USER
andMYSQL_PASSWORD
: Optional credentials for another user.
- ports: Maps the MySQL port (
3306
) from the container to the host. - volumes: Creates a persistent volume (
mysql-data
) to store MySQL data. This ensures your data persists even when the container stops.
Step 4: Run Docker Compose
Now that your docker-compose.yml
file is ready, you can use Docker Compose to start MySQL.
- Run the following command in the project directory:
docker-compose up -d
up
starts the services defined indocker-compose.yml
.-d
runs the containers in detached mode, allowing them to run in the background.
Docker Compose will download the MySQL image and start the container.
Step 5: Verify the MySQL Container is Running
To confirm the MySQL container is running, use the following command:
docker ps
This will list all running containers. You should see mysql_container
in the list, along with its status as “Up.”
Step 6: Connect to the MySQL Database
You can now connect to the MySQL database from your host system or another container.
Option 1: Use the MySQL CLI Inside the Container
To access the MySQL command line interface inside the container, run:
docker exec -it mysql_container mysql -u root -p
This command runs MySQL inside the container with the root user. You’ll be prompted to enter the root password you defined in the docker-compose.yml
file.
Option 2: Use a MySQL Client
You can also connect to MySQL using a graphical client like MySQL Workbench. Use localhost
as the hostname, 3306
as the port, and the username/password from your docker-compose.yml
file.
Step 7: Manage the MySQL Container
With Docker Compose, managing your MySQL container is easy. Here are a few commands to help you manage it:
- Stop the MySQL container:
docker-compose down
This will stop and remove the container, but it will keep the data stored in the mysql-data
volume.
- Restart the MySQL container:
docker-compose up -d
- View MySQL container logs:
docker-compose logs
This shows the logs of the MySQL container, useful for debugging or checking startup status.
Step 8: Remove Data (Optional)
If you want to remove the persistent data stored in the volume, use the following command:
docker volume rm mysql-docker-setup_mysql-data
This will permanently delete the volume and all its data.
Step 9: Customizing the Docker Compose File
You can easily customize the docker-compose.yml
file to suit your project. For example:
- Add more users: You can define additional environment variables for multiple users.
- Change the MySQL version: Replace
mysql:latest
with a specific version, such asmysql:8.0.25
. - Add backup volumes: Define more volumes for backups or configurations.
Conclusion
Installing MySQL on Docker using Docker Compose is straightforward and efficient. It allows you to spin up a MySQL instance quickly without manual configuration. The process involves creating a docker-compose.yml
file, defining the MySQL service, and running it using Docker Compose commands. With just a few steps, you can have a fully functioning MySQL database that is easy to manage and customize.
Now, you can experiment with additional configurations and scale your setup as needed.
Thank you for reading the article! If you found the information useful, you can donate using the buttons below:
Donate ☕️ with PayPalDonate 💳 with Revolut