Install MySQL using Docker
Tags: mysql, docker, tutorial, tooling
Published: 2021-03-27 06:00:32

I've been a [Docker](https://www.docker.com/) user for some years and I never get tired of the power, flexibility and organization that it brings to my personal machine. In this article I will show you how do I install MySQL in my computer using Docker in order to be able to use it while avoiding all the headaches that might come by installing the database locally.

## What is Docker?

Let's start with a short explanation about what is Docker. It is a technology that has become the standard when building and distributing applications. Docker's objective is to standardize the way to distribute software by packing the code and dependencies in artifacts called **[containers](https://www.docker.com/resources/what-container)**. A container will have a set of processes that executes in your machine, but are logically isolated from the rest of your system.

Containers bring a lot of benefits such as:

- **Flexibility:** You can pack any type of application into a container.
- **Lightweight:** All containers reuse the kernel between them.
- **Portable:** They're designed to run the same way in any computer.
- **Low coupling:** They are contained and are not affected by different configuration or software that may be installed in the host OS.
- **Scalable:** You can easily copy containers multiple times.
- **Safe:** A container may only access certain parts of the system that it has been granted access.

To know more about Docker you can read the official [Docker Overview article](https://docs.docker.com/get-started/overview/).

You can also read how to install Docker on [macOS](https://docs.docker.com/docker-for-mac/install/), Linux (using [Ubuntu](https://docs.docker.com/engine/install/ubuntu/) or [Linux Mint](https://dev.to/d4vsanchez/install-docker-community-edition-in-linux-mint-2gl4)) and [Windows](https://docs.docker.com/docker-for-windows/install/).

## What is MySQL?

Now, let's talk a bit about MySQL. It's a DBMS (Database Management System) that allows us to manage relational databases. It was created by Sun Microsystems and then absorbed by Oracle when they purchased Sun on 2010. It's a pretty popular DBMS in the community due to being Open Source and free to use for commercial purposes.

You use DBMS, such as MySQL, in order to store and retrieve data for multiple purposes such as applications, websites, and services.

To know more about MySQL you can read the official [What is MySQL? article](https://dev.mysql.com/doc/refman/8.0/en/what-is-mysql.html).

## Why Do I Use Docker for This?

You might say that I'd be better if I just run a `[brew](https://brew.sh/)` install ([I moved to macOS a while ago](https://twitter.com/d4vsanchez/status/1360663470727958533)) and that's all, and while that may be true, I still find myself being a bit uncomfortable installing services that I may use sporadically. For this reason, I'm using Docker to keep my main OS free of services and configurations that may come to haunt me in the future.

Another benefit I find in using Docker is that I can just upgrade or downgrade the version of the container with a simple command, just by modifying its tag.

## Create a Docker Volume

We're installing a database in order to persist information, aren't we? So we need to find a way that our Docker containers are able to persist information in our host machine. 

**Note:** Docker containers doesn't persist information by default. They execute the processes and when you stop them, all the information that was written to the hard drive is gone.

We will make use of Docker Volumes to persist our data through multiple executions of the containers. Docker Volumes are managed by Docker and bring us multiple advantages such as:

- Not dependent on the directory structure and the OS host machine.
- Easy to back up or migrate.
- Safely shared among multiple containers.
- Easy to manage.
- You can read more about this topic in the [Docker Volumes documentation](https://docs.docker.com/storage/volumes/).

In order to create a new Docker Volume it is pretty simple, we just have to execute the following command in our terminal:

```bash
docker volume create mysql-volume
```

And that's it! Docker has created our Volume that we'll be using to persist our data.

## Create a Docker MySQL Container

Now that we have our Docker Volume created, we can create the container that will contain the mysql binary, services and configuration.

In order to do this we'll execute the following command:

```bash
docker run --name mysql-db -e MYSQL_ROOT_PASSWORD=mysql --mount source=mysql,target=/var/lib/mysql -p 3306:3306 -d mysql
```

I'm going to explain what every part of this command does:

- `docker run` Creates a Docker Container based on a [Docker Image](https://docs.docker.com/get-started/overview/#docker-objects)
- `--name mysql-db` This is the name of the container we're creating right now. Docker containers must have unique naming.
- `-e MYSQL_ROOT_PASSWORD=mysql` Set an environment variable containing the password of the **root** user. Please don't judge me for using this password, is just for educational purposes, but you **should** change the password to something else more secure.
- `--mount source=mysql-volume,target=/var/lib/mysql` This is where we told our container to use our newly created `mysql-volume` Docker Volume to store information that's going to be written on `/var/lib/mysql` which is the directory where MySQL persists the information.
- `-p 3306:3306` We tell Docker to expose the `3306` port of the container into the `3306` port of our host OS in order to be able to access the MySQL service.
- `-d` It just tells Docker to run the container in **detached** mode or in background.
- `mysql` The Docker Image that we're going to use as "blueprint" to create our container.

That's it! After executing that line we may have MySQL running inside a Docker Container that's exposing the 3306 port into our system so we can do queries to it.

## Test Installation

We can check that our container is working correctly by connecting to the MySQL client.

```bash
docker exec -ti mysql-db mysql -u root -p
```

And then entering the password we added as value of the `MYSQL_ROOT_PASSWORD` environment variable.

If you receive an output in console showing the Copyright notice and a `mysql>` prompt, then it means that the container is running correctly and you can start using it now!

That's all to create a Docker Container for MySQL, now you can use it for whatever you want. If you've got any question about this process, I'll be more than happy to help you so please leave it in the comments and expect an answer from me as soon as I have time to answer it.

Hope you've liked the post and leave your reaction in the sidebar on the left (or in the bottom if you're in a small screen device) ❤️