Setup a Development MySQL Database with Docker

We start from the official MySQL Docker image available: mysql.

FROM mysql:5.7.18

set the ENVironment variable to provide a root password:

ENV MYSQL_ROOT_PASSWORD=s3cr3t

and ADD our development setup script init-development.sql:

ADD init-development.sql /docker-entrypoint-initdb.d

Within this SQL file we create all data needed for development purposes.

CREATE USER 'devel'@'%' IDENTIFIED BY 's3cure';

GRANT ALL ON data.* TO 'devel'@'%';

# TODO insert into data table...

Let's create the Docker image:

$ docker build . -t development-mysql

The run the MySQL with

$ docker run -it --rm \
        --publish 3306:3306 \
        --hostname mysql \
        --name mysql \
        development-mysql

To test the installation you can either connect into the running container:

$ docker exec -it mysql mysql --user devel --password
....
mysql> SHOW GRANTS;
+-----------------------------------+
| Grants for devel@%                |
+-----------------------------------+
| GRANT USAGE ON *.* TO 'devel'@'%' |
+-----------------------------------+
1 row in set (0.00 sec)

or run a fresh container to test remote access to the host machine with the IP 192.168.1.42:

$ docker run --rm -it development-mysql mysql --host 192.168.1.42 --user devel --password

Happy SQL development!

For more information on the used SQL commands see: