As a MuleSoft developer, setting up MySQL quickly and efficiently is often a critical step for testing integrations and APIs. Using Docker to host MySQL provides an isolated and portable container environment, simplifying the setup process while ensuring your database isn’t affected by host machine configurations.
This makes Docker an ideal choice for testing, allowing you to replicate environments and share setups with your team seamlessly. In this post, we’ll explore how to install and customize a MySQL Docker container to streamline your development and testing mule integration flows.
This command creates a managed storage volume that Docker can use to retain data. We will make use of this volume when we’ll run the container.
Where:
Once the container starts, MySQL will be running and accessible on port 3306.
This will list all active containers. We should see an entry for our
Enter the root password you set earlier when prompted. Then type the
Prerequisites
Before starting, make sure we have the following:- Docker Installed: Ensure Docker is installed and running on our system. You can download Docker from the official website.
Pull the MySQL Docker Image
Docker uses images to create containers. To get started, pull the latest MySQL image from Docker Hub by running:docker pull mysql:latest
This command downloads the latest version of MySQL. If you need a specific version, replace
For persistence, we will create a Docker volume called
latest
with the desired version tag (e.g., mysql:8.0
).Persist Data (Optional)
By default, Docker containers are ephemeral, meaning data stored in the container is lost when the container is removed. This behavior is advantageous for testing as it allows you to start with a clean state every time you recreate the container, ensuring consistent and isolated test environments. However, for scenarios where data persistence is needed—such as testing database migrations or backups—you can use a Docker volume to retain data even after the container is removed.For persistence, we will create a Docker volume called
mysql-data
to store MySQL data:docker volume create mysql-data
Run the MySQL Container
After pulling the image, create and start a container with the following command:docker run -d --name mule-mysql \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=Mule1234 \
-e MYSQL_DATABASE=mule-db \
-e MYSQL_USER=mule \
-e MYSQL_PASSWORD=Mule1234 \
-v mysql-data:/var/lib/mysql \
mysql:latest
-d
: Runs the container in detached mode (in the background).--name mule-mysql
: Assigns a custom name to the container (you can change this).-p 3306:3306
: Maps port 3306 of your host machine to port 3306 in the container.-e MYSQL_ROOT_PASSWORD=rootpassword
: Sets the root password for the MySQL database.-e MYSQL_DATABASE=mule-db
: Creates a default database named mule-db (change this to your own)-e MYSQL_USER
and-e MYSQL_PASSWORD
: Creates an additional user with specific credentials.-v mysql-data:/var/lib/mysql
: Attaches the volume we created previously to the containermysql:latest
: refers to the image we’ve just downloaded in the first step
Verify the Container is Running
To ensure your MySQL container is up and running, execute:docker ps
This will list all active containers. We should see an entry for our
mule-mysql
.Connect to MySQL
We can connect to MySQL in two ways:1. Docker CLI:
We’ll run this command to open a shell session within the mysql container and connect with the MySQL clientdocker exec -it mysql-container mysql -uroot -p
Enter the root password you set earlier when prompted. Then type the
SHOW DATABASES
command to check that our default db has been created successfully2. External Client:
Use a MySQL client like MySQL Workbench or any command-line client. Connect to:- Host:
localhost
- Port:
3306
- Username:
root
- Password: The root password you set earlier.
Manage the MySQL Container
Stop the Container:
docker stop mule-mysql
Start the Container:
docker start mule-mysql
Remove the Container:
To completely remove the container and its data:docker rm -v mule-mysql