Running MySQL in Docker

in mysql •  7 years ago  (edited)

Running MySQL on Docker? Why? because it is really cool! in my case, I must run MySql 5.6 and I already have installed a newer version and I don't want to uninstall it. Despite of this, I would always recommend using containers. Nowadays, it is much easier to set up a container with MySQL than installing it on the bare metal.

Before going on, if you wanted to deeply understand how Docker works, I would recommend reading about cgroups because Docker is built on top of it.

The idea of running processes in containers drives us to think of ephemeral and stateless processes, because they are easy to scale automatically. But, do databases fit with this rule?. A strict answer would be no, because in most cases we can change its data, but docker allows us to run just the DMBS process in the container (which is stateless), define a volume in the local machine and mount it in the container. This way, we can persist on the host's disk the variable data generated in the container.

Having said that, I will describe the steps I followed to make this run. I will reuse an existing image based on Centos and MySQL 5.6 that matches my needs: centos/mysql-56-centos7 .

We start by pulling the image

 $ docker pull centos/mysql-56-centos7 

Then, we create and run the container using the centos/mysql-56-centos7 image.

$ docker run --name=mysql-db -d -e MYSQL_USER=martin -e MYSQL_PASSWORD=martin -e MYSQL_DATABASE=db -e MYSQL_ROOT_PASSWORD=martin -p 6666:3306 --volume=data:/var/lib/mysql/data centos/mysql-56-centos7   

We can check that the container is properly running:

$ docker ps 

CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                    NAMES 

f16d7268baf1        centos/mysql-56-centos7   "container-entrypo..."   5 minutes ago       Up 5 minutes        0.0.0.0:6666->3306/tcp   mysql-db 

We can see the volume we defined to persist the data generated in the container:

$ docker volume ls | grep data 

local               data 

And we can inspect it:

$ docker volume inspect data 

[ 

    { 

        "Driver": "local", 

        "Labels": null, 

        "Mountpoint": "/mnt/221a90fe-4c13-402f-bf3b-e66614f7d56b/docker/lib/volumes/data/_data", 

        "Name": "data", 

        "Options": {}, 

        "Scope": "local" 

    } 

] 

We can also inspect the container:

$ docker inspect mysql-db 

  "Networks": { 

                "bridge": { 

                    "IPAMConfig": null, 

                    "Links": null, 

                    "Aliases": null, 

                    "NetworkID": "db65a8f3b276533d83617e69f1541c7e52e571377f9676eeb0e40348f26069e8", 

                    "EndpointID": "a0dbfe4bf04b048c2faf9bf95a23295c7acad9e5d99ca10e6de634945b5b745e", 

                    "Gateway": "172.17.0.1", 

                    "IPAddress": "172.17.0.2", 

                    "IPPrefixLen": 16, 

                    "IPv6Gateway": "", 

                    "GlobalIPv6Address": "", 

                    "GlobalIPv6PrefixLen": 0, 

                    "MacAddress": "02:42:ac:11:00:02" 

                } 

            } 

Using this network configuration we can connect to it by(we assume we have the MySql client installed) and create a database and some tables. We must the user and and password we chose previously.

$ mysql --host=172.17.0.2 --port=3306 --user=root -p 

or

$ mysql --host=127.0.0.1 --port=6666 --user=root -p 
Server version: 5.6.37 MySQL Community Server (GPL) 

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. 

Oracle is a registered trademark of Oracle Corporation and/or its 

affiliates. Other names may be trademarks of their respective 

owners. 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 

 mysql> CREATE TABLE Persons ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, email VARCHAR(50) ); 

mysql> insert into Persons(firstname,lastname,email) values('Martín','Baez','[email protected]'); 

Query OK, 1 row affected (0.00 sec) 

mysql> quit; 

Now we are going to destroy our container,

 $ docker rm -f mysql-db 

We check that it is not there any more

$ docker ps 

CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                    NAMES 

And we will run a new container reusing the data generated by the previous container

$ docker run --name=mysql-db-2 -d  -e MYSQL_USER=martin -e MYSQL_PASSWORD=martin -e MYSQL_DATABASE=db -e MYSQL_ROOT_PASSWORD=martin -p 6666:3306 --volume=data-silvercore-container:/var/lib/mysql  centos/mysql-56-centos7   

Now we can connect to the new container and will see that we did not lose any data.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!