How to Create a MySql Instance with Docker Compose

Chris Chuck
5 min readSep 9, 2018

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.

Linux Installation

First, let’s download the Docker repository. Run the following:

sudo apt-get update

Followed by:

sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common

Next run:

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) <docker@docker.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:

docker-compose --version

And you should get something similar to:

docker-compose version 1.22.0, build 1719ceb

Setting Up

After installing Docker run it and create file calleddocker-compose.yml. This file is essentially and instructions sheet for Docker.

In it paste the following:

version: '3.3'services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'db'
# So you don't have to use root, but you can if you like
MYSQL_USER: 'user'
# You can use whatever password you like
MYSQL_PASSWORD: 'password'
# Password for root access
MYSQL_ROOT_PASSWORD: 'password'
ports:
# <Port exposed> : <MySQL Port running inside container>
- '3306:3306'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- my-db:/var/lib/mysql
# Names our volume
volumes:
my-db:

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 localhost:3306.

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:

version: '3.3'services:
db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: password

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.

Conclusion

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!

My aim is to keep all articles free for others to learn, though I know many readers are quite generous and want to give a token of appreciation. If you are feeling generous, I ask that you simply donate towards a conservation charity.

A couple of favorites (but not limited to):

Force Blue — Special operations veterans working to preserve reefs around the world

Save the Frogs — dedicated to outreach, education, and research of amphibian conservation

Xerces Society — nonprofit dedicated to the conservation of insects and invertebrates

--

--