This Dockerfile (check github) is used to provide MySQL databases in a frictionless but flexible way. The requirement was to provide different MySQL based relational databases for computer science students for educational purposes (database/webtechnology lectures). Nevertheless, the approach can be used for similar purposes in complete different domains.
Whenever you have to
this container might be of interest to you.
Warning: You should not use this container for production purposes.
First, you have to install Docker.
If you are using Linux, you are fine. Docker installation on Linux is less complicated than for other operating systems. Docker is a operating system virtualization tool chain for Linux. Therefore installation gets only complicated if you are leaving the Linux ecosystem.
You will find Docker installation instructions for a lot of Linux distributions here.
But no worries. If you are using Windows (why ever) or Mac OS X (like me) simply follow the boot2docker installation instructions for
Start Boot2Docker according to your operating system. You can skip this step, if you are working on a Linux system.
First you have to build a image. This image provides a self-contained MySQL server. You can clone this repository or tell docker to do the repository handling behind the scenes for you (which is my preferred way in case of github provided Dockerfiles):
Now you have an image named mysqldb on your system, capable to provide MySQL databases. The simplest way to do start a database is like that:
It will create a running container on your system providing a MySQL server.
If you want to run your database on a different port than 3306 just do the following:
to run it on port 4407 (for instance). You can figure out that the container is running
and docker returns an output like that.
To check whether the database is working, you can connect to it. Figure out what address your docker host has. If you are working with Boot2Docker you con do this
and you will get an answer like that:
Now you have all to connect to your MySQL database. To check that everything is fine just start MySQLWorkbench and enter the following parameters when creating a new database connection:
To stop a container simply figure out its CONTAINER ID (via
docker ps as shown above)
and stop it like that:
The above mentioned standard database is a read only default database. Not very helpful (except for me and my lectures). But the container is capable to do more by providing a set of parameters.
passwordto define your own user
rightto define whether you want to provide just read or full access rights
urlto provide an arbitrary sql file (UTF8 encoded) to deliver your own database
So let’s figure out some details.
You can provide your own database via a sql file when you start your container.
Just use the
url parameter to point to a valid sql file.
This file can be hosted anywhere (accessible from your docker host).
Attention! SQL file is assumed to be encoded as UTF8 and has to valid as well as non interactively processable by mysql.
You can create your own user with own password by using the
By default the created MySQL user will get read access to all databases hosted by this container.
You can change the access rights of your database.
READ(which is mapped to GRANT SELECT on all databases) [DEFAULT]
WRITE(GRANT ALL PRIVILEGES WITH OPTIONS on all databases, so this is power user is able to do everything)
Hint! Access rights have to be written completely in uppercase.
Write is not recognized as
Be aware! If you are granting write access to the user, the user be able to do everything with the database including
The read access right is the default one. So
is synonym to
Read access is perfect for providing read-only datasets. E.g. databases for students they should use to answer questions. By providing the database read-only it is assured that no student can destroy the database accidentally.
If you want to create a user with complete write access to your database you can run something like that
which will provide full access to the database for user Me.
Write access is perfect to provide databases, which can be administered by the user. E.g. for students who have to set up a data model from scratch but should not have to deal with nitty critty MySQL server installation and configuration. Nevertheless they have to be aware that the user can do arbitrary harm to the database. So normally a user with write access should create users with a more restrictive set of rights for the database. But this is up to the user.
If you want to provide your own databases, you must assure that your database definition
file provided via the
url parameter is valid and processable by MySQL.
If it works in MySQL Workbench it should work with this container.
Nevertheless, if your are running into troubles you should start your container
-i parameter of docker. This will forward the output of the container
to your console, which is likely to be helpful for debugging purposes.