This image extends the offical MySQL image, but gives you the following benefits:
- You can supply your own database / data at run time,
- Or you can create an image containing your own database / data.
- Your mysql data directory is in a separate image.*
- It opens up mysql on 0.0.0.0.*
You came here for this:
docker run -e MYSQL_ROOT_PASSWORD=root -e MYSQL_USER=dba -e MYSQL_PASSWORD=password -e MYSQL_DATABASE=testdb -v $(pwd)/myschema.sql:/tmp/import_database.sql drakedroid/mysql-with-data
The following environment variables are from the official image:
The volume parameter -v $(pwd)/myschema.sql:/tmp/import_database.sql will run the sql script myschema.sql on mysql startup. For best results, use the database defined in the MYSQL_DATABASE variable.
Much more is possible if you use the included shell scripts:
The full project with scripts can be downloaded from GitHub.
Creating your own image containing a specific schema
Let's say that you have an database schema that is static and will be reused a lot. You can create an image for it in the following way:
- Replace the import_database.sql with your own schema. Note that the name will have to stay the same.
- Create a new image called mysql_with_data, containing your schema:
docker build -t mysql-with-data .
- ./startMysqlWithVolume myimagename
Behind the scenes, the following is happening.
- A new volume image, sharing /var/lib/myimagename is created, with the name dataimage_myimagename.
- A new container is spinned up and down from this new image, and is called myimagename_data.
- A different container for mysql_with_data is started, using the volume from the myimagename_data container.
This means that after you stop and delete your mysql container, you can still attach a new container to the data by running:
again. You will explicitly have to remove the myimagename_data container to delete the data.
Dynamically attaching a schema to a new instance
This section explains a helper script for the functionality in the Quick start section.
If you have a schema you want to spin up, without building an image, you can do that too.
- Copy your schema to this directory. We'll pretend that you have copied newstuffs.sql here.
- ./startDynamic my_vol_name newstuffs.sql
This will do everything that happened in the previous section, just with the schema you specified, without having to compile it. Edit the startDynamic to suit your needs.
Environment variables added to the MySQL image
The following are new variables you can use:
- DEV_ACCESS=true, explicitly opens up mysql to listen on 0.0.0.0
- IMPORT_DB=true, imports the database included in the image at /tmp/import_database.sql, or the file that is linked to that location from the host machine. For examples how this works, see the two shell scrip$
- MYSQL_DIR=/some/new/dir, all the mysql database files will be created in this directory.