Hello. In this post, I will teach you how to deploy MySQL using Docker compose. It will be brief but explained step by step.
Briefly, I will tell you that Docker compose is more thought of production environments where compatibility should be as high as possible. This is precisely the great advantage of Docker images that we will be able to use in any supported system thanks to the technology of containers. Then, these steps can be done from any Linux distribution that has installed Docker and Docker Compose.
Remember that it is always possible to install MySQL on a server using your precompiled packages, we can easily have the latest version available.
For this tutorial, apart from MySQL, we will also deploy Adminer that we talked about earlier. Together you will be able to manage a MySQL server without problems.
So let’s get to work.
Install Docker and Docker Compose on Linux
The first step is to have Docker installed in the system, obviously.
- If you are using Ubuntu: How to install Docker on Ubuntu 18.04?
- For CentOS 8: How to install Docker on CentOS 8?
- For CentOS 7: How to install Docker CE on CentOS 7?
- If you use Windows: How to install Docker in Windows 10?
- Or, if you are using Debian 10: How to install Docker on Debian 10?
And using your distribution’s package manager, you can install Docker Compose. For example:
:~$ sudo apt docker-compose
For Debian, Ubuntu and derivatives.
:~$ sudo dnf install docker-compose
Or, for RHEL, CentOS and derivatives.
Deploy MySQL using Docker Compose
Well, it’s time to work. First create a folder called mysql and access it.
:~$ mkdir mysql
:~$ cd mkdir
Inside it, we will create the docker compose file called docker-compose.yml
.
:~$ nano docker-compose.yml
Once you have created the file, you need to add the following content:
version: '3.1'
services:
db:
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: angelo123
MYSQL_DATABASE: example
MYSQL_USER: angelo
MYSQL_PASSWORD: angelo123
volumes:
/home/angelo/mysqldata:/var/lib/mysql
adminer:
image: adminer
restart: always
ports:
- 8080:8080
I now proceed to explain briefly.
First, we deploy a service called db that contains the information to install MySQL. First, we use the image of the latest version of MySQL. Inside the environment section we define the necessary variables to start the service. In this section, you define things like the password of the root user, the regular user and the user name as well as the name of the initial database.
An important aspect is the volume that allows us to have access to the database in the host system. This folder must be created before the execution of the file.
In the end, we deploy Adminer using port 8080, obviously you can change this.
So, create the volume folder:
:~$ mkdir /home/angelo/mysqldata
Then, run Docker Compose.
:~$ sudo docker-compose up -d
So, now you can open your favorite web browser and go to http://your-server:8080
to access Adminer. Remember that port has to be open in the firewall.
Now, type in your MySQL credentials and log in. These are the ones we define in the Docker compose file.
And that is it.
Conclusion
Docker is a technology with enormous potential. It helps a lot in the deployment of applications without many dependency problems. Today in this tutorial we have deployed MySQL and Adminer without many problems.
For more information about the use of the MySQL image, please consult the official documentation.