DEV Community

David Sánchez
David Sánchez

Posted on • Updated on

Install MySQL using Docker

I've been a Docker 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. 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.

You can also read how to install Docker on macOS, Linux (using Ubuntu or Linux Mint) and Windows.

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.

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) 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.

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

docker volume create mysql-volume
Enter fullscreen mode Exit fullscreen mode

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:

docker run --name mysql-db -e MYSQL_ROOT_PASSWORD=mysql --mount source=mysql,target=/var/lib/mysql -p 3306:3306 -d mysql
Enter fullscreen mode Exit fullscreen mode

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

  • docker run Creates a Docker Container based on a Docker Image
  • --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.

docker exec -ti mysql-db mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

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) ❤️

Top comments (0)