How to Create a MySql Instance with Docker Compose
Why Docker Compose?
With Docker Compose, we get all the benefits of Docker plus more. Docker works by creating a virtual environment(or container) for your code to run. What Docker Compose adds is orchestration and organization of multiple containers. While this tutorial will only spin up a single container for our MySQL instance, Docker Compose can also be used to run all of your various services at once when your project begins to grow out.
Let’s Get Docker
Mac & Windows Installation
Just follow the installation guide for Docker for Mac or Windows here.
Unfortunately there’s no Docker for Linux ☹️
First, let’s download the Docker repository. Run the following:
sudo apt-get update
sudo apt-get install \
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
To verify you have the repository run the following:
sudo apt-key fingerprint 0EBFCD88
And you should get something like this:
pub 4096R/0EBFCD88 2017-02-22
Key fingerprint = 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid Docker Release (CE deb) <email@example.com>
sub 4096R/F273FCD8 2017-02-22
Now to install Docker you just need to do the following:
sudo apt-get update && sudo apt-get install docker-ce
Now to get Docker Compose run:
sudo curl -L "https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && sudo chmod +x /usr/local/bin/docker-compose
To test if your installation was setup correctly, run:
And you should get something similar to:
docker-compose version 1.22.0, build 1719ceb
After installing Docker run it and create file called
docker-compose.yml. This file is essentially and instructions sheet for Docker.
In it paste the following:
# So you don't have to use root, but you can if you like
# You can use whatever password you like
# Password for root access
# <Port exposed> : < MySQL Port running inside container>
# Opens port 3306 on the container
# Where our data will be persisted
# Names our volume
Great, now we can start our container. In your command line or terminal,
cd into the directory where you made your
docker-compose.yml and from there, run
docker-compose up(this might take a while on the first run because Docker needs to pull the containers). We should now have a MySQL instance running on
Your output should look similar to this:
And that’s it! If you want to change databases from MySQL to something else, all you have to do is change the
docker-compose.yml and restart Docker Compose.
Here’s a simple Postgres DB example:
As you can see, using Docker Compose provides us with a ton a flexibility and configurations. We can switch databases in about 30 seconds if we wanted to!
Now let’s play with our new database!
Connecting with MySQL Workbench
Downloading MySQL Workbench
Start with downloading MySQL Workbench here.
Connecting to your MySQL Instance
Next, Make a new connection like so:
Click “OK” and click on your connection.
Now we’re free to play with our database as we please.
Shutting Down & Cleaning Up
We don’t want our image to be running 24/7, but shutting it down can be a little tricky.
In your terminal, press
ctrl + c, this may or may not kill the container gracefully 🤷. Either way, run
docker-compose down afterwards and your container should be shut down. Now you just have to run
docker-compose up again and you’ll be right where you left off.
If you want a fresh start for everything, run
docker system prune -a and
docker volume prune. The first command removes any unused containers and the second removes any unused volumes. I recommend doing this fairly often since Docker likes to stash everything away causing the gigabytes to add up.
With Docker Compose, we can run a containerized database in a safe, isolated environment while still having the ability to change the type of database in an instant. On top of this, in a large project, we can fully leverage Docker Compose and orchestrate all of our different services at once. As you saw, we just needed to copy/paste a YAML file and run
docker-compose up in order to have a fully operational development database.
Thank you for reading and I hope you’ll give Docker a try in your next project!