How can I create a docker image with a initialized database - mysql

I want to use the official mysql docker image to create an new image with an initialized database and imported dump file. The default command is mysqld, so I wonder how I can achieve this.
Is there a way to only initialize the database and import a dump, without starting the mysql daemon?

The MySQL docker images automatically parse .sh .sql and .sql.gz from the /docker-entrypoint-initdb.d directory on first start up. Files will be executed in alphabetical order.
Dump your existing database to a file, then edit your Dockerfile to add:
ADD mysqldump.sql /docker-entrypoint-initdb.d/mysqldump.sql
On first start, mysqldump.sql will be imported.

An option would be to access the bash shell of the docker container (please see "Container shell access and viewing MySQL logs" section in https://hub.docker.com/_/mysql/ ), import your dump file (you can use docker cp to copy the dump file from your host system to the docker container) and once you are done, you can use docker commit command to create a new docker image from your current running container. And you can use the newly created image afterwards.
Hope this helps.

I just found a solution for mysql:5.7. I execute the slightly modified entrypoint.sh during the build process:
COPY ./docker-entrypoint.sh /
RUN /docker-entrypoint.sh mysqld
In the docker-entrypoint.sh I deleted the last line:
exec "$#
It looks ugly, but it works so far.

Related

Load MySQL Employees Sample Database into a already running docker container on Windows

I have a MySQL Docker container running in my local Windows machine. I want to load Employees database into that docker container.
Employees Database Reference: https://dev.mysql.com/doc/employee/en/
I tried using MySQL Workbench and "Run SQL Script", but it's throwing below error:
[WinError 32] The process cannot access the file because it is being used by another process:
'C:\\Users\\roul\\AppData\\Local\\Temp\\tmp4fbw2bb4.cnf'
After reading some article I think we may have one option of attaching the script file location volume into the container and run the script from docker command prompt, but that I'm unable to do it.
Anyone here have already done that?
Find the datadir of you MySQL Server:
SHOW VARIABLES WHERE variable_Name LIKE "datadir"
Copy the content of the folder to your datadir (\. copy the content of the folder, not the folder; maybe you want improve this to not mess the datadir):
docker cp test_db-master/. CONTAINER:/var/lib/mysql/
Run the script inside the container:
docker exec -i CONTAINER /bin/bash -c "cd /var/lib/mysql/ && /usr/bin/mysql -u root --password=123456 < /var/lib/mysql/employees.sql"

Move MySQL /var/lib/mysql to shared volume

I'm running a container with MySQL 8.0.18 in Docker on a Synology nas. I'm just using it to support another container (Mediawiki) on that box. MySQL has one volume mounted at path /var/lib/mysql. I would like to move that to a shared volume so i can access it with File Station and also periodically back it up. How can i move it to the docker share shown below without breaking MySQL?
Here are available shares on the Synology nas.
Alternatively, is there a way i can simply copy that /var/lib/mysql to the shared docker folder? That should work as well for periodic backups.
thanks,
russ
EDIT: Showing result after following Zeitounator's plan. Before running the docker command (2.) i created the mediawiki_mysql_backups and 12-29-2019 folders in File Station. After running 2. and 3. all the files from mysql are here and i now have a nice backup!
Stop your current container to make sure mysql is not running.
Create a dummy temp container where you mount the 2 needed volumes. I tend to user busybox for this kind of tasks => docker run -it --rm --name temp_mysql_copy -v mediaviki-mysql:/old-mysql -v /path/to/ttpe/docker:/new-mysql busybox:latest
Copy all files => cp -a /old-mysql/* /new-mysql/
Exit the dummy container (which will cleanup by itself if you used my above command)
Create a new container with mysql:8 image mounting the new folder in /var/lib/mysql (you probably need to do this in your syno docker gui).
If everything works as expected, delete the old mediaviki-mysql volume

Entrypoint in Dockerfile

I made docker image.
I properly copied init.sql script. Additionally I configured database properly. When I ran this command without ENTRYPOINT, inside container, everything works, but when I added this to Dockerfile to ENTRYPOINT and built image again I see that is showing something like mysql man page. Exited(1). I cant get any logs, what can be wrong with this command?
I think it's not problem of database, but docker itself.
ENTRYPOINT ["mysql", "-u", "my_user", "-pmy_pw", "my_databse", "<", "init.sql"]
As you can read in the documentation, the exec format does not execute a command using the sh or bash shell. So the redirect won't work there.
In order to have that entrypoint working, you should rewrite it:
ENTRYPOINT ["sh", "-c", "mysql -u my_user -pmy_pw my_databse < init.sql"]
You do need to override the DB container entrypoint, as the entrypoint perform some complex operation like creating DB from mapped init script and set user etc.
All you need to mapp "init.sql" with container path /docker-entrypoint-initdb.d
Initializing a fresh instance
When a container is started for the first time, a new database with
the specified name will be created and initialized with the provided
configuration variables. Furthermore, it will execute files with
extensions .sh, .sql and .sql.gz that are found in
/docker-entrypoint-initdb.d. Files will be executed in alphabetical
order. You can easily populate your mysql services by mounting a SQL
dump into that directory and provide custom images with contributed
data. SQL files will be imported by default to the database specified
by the MYSQL_DATABASE variable.

Docker toolbox with mysql container problem on Windows 10 Home (and Pro)

I unable to run MySQL containers made from MySQL images with database volumes mapped to my host machine's folder.
It doesn't matter if the host folder is empty or with existing database files. I do know that Docker Toolbox could mount volumes on Windows only from c:\Users\ so my test folder is under that one.
I was trying different (official and not) MySQL images from 5.5 to latest with no result. Anytime when location /var/lib/mysql in container is pointing to a folder on my host machine (c:\Users\someuser\testfolder) I've got an error on container`s running with InnoDB error ("InnoDB: Operating system error number 22 in a file operation" or "InnoDB: File ./ib_logfile0: 'aio write' returned OS error 122").
I was trying to modify mysql container's /etc/my.cnf (under [mysqld] section, using "docker cp" command) adding "innodb_use_native_aio=OFF" or (sometimes even and) "innodb_use_native_aio=0" keys and even was trying to run "docker run" with "--user 1000:50" with no result either.
Just after I delete mount point from container's /var/lib/mysql to my host folder, the container runs normally.
There are many alike questions but no one has complete step by step solution how to run a MySQL container with a Docker Toolbox under Windows 10 (Home & Pro) to bring container work with an existing database on the host's volumes.
It took me a while to get an answer but finally everything worked! For those who are new to Docker and have problems mounting MySQL folder to the host here is a short guide. Please note I chose bitnami/mysql image for my experiments (for another images folders can be differ).
Create a folder c:\Users\[YourAccount]\MySQLData for MySQL data.
Create a folder c:\Users\[YourAccount]\MySQLConf for a custom MySQL config file.
Create a custom MySQL config file c:\Users\[YourAccount]\MySQLConf\my_custom.cnf and add two lines in it:
[mysqld]
innodb_use_native_aio=0
4. Now create and run the container mounting your custom config and data folder to it:
docker run -d --name mysql -e ALLOW_EMPTY_PASSWORD="yes" \
-v //c/Users/[YourAccount]/MySQLData:/bitnami/mysql/data \
-v //c/Users/[YourAccount]/MySQLConf/my_custom.cnf:/opt/bitnami/mysql/conf/my_custom.cnf:ro \ bitnami/mysql:latest
Hooray!

MySQL container crash after /etc/mysql/my.cnf change, how to edit back?

I changed some mysql config settings and set something wrong, now Docker container keeps restarting and I cannot find the my.cnf file to edit in host filesystem. I have tried aufs/diff folders but so far unable to find it. Also tried:
find / -name my.cnf -exec nano {} \;
But it does not bring up the file I changed. And I tried to change config.v2.json to start /bin/bash instead of mysqld and restarted docker, but yet it started mysqld (due supervisor or something?) using official mysql container image.
I am seeing two possible solutions for your problem:
Bypass the ENTRYPOINT for the MySQL image
Find your image name by running docker images then run:
docker run -it --entrypoint="/bin/sh" OPTIONS image
That should take you to the bash inside the container and from there you can execute all the commands you want to find your my.cnf file. Although I don't know if editing the file from there, save it and try to run it again will works. I didn't tried.
Delete the old image and use the proper way to edit the my.cnf file
Find your image name by running: docker images and then delete it by running docker rmi <image_name>
Check the docs for the default MySQL images at MySQL Dockerhub is pretty straight on this and I quote:
Using a custom MySQL configuration file The MySQL startup
configuration is specified in the file /etc/mysql/my.cnf, and that
file in turn includes any files found in the /etc/mysql/conf.d
directory that end with .cnf. Settings in files in this directory will
augment and/or override settings in /etc/mysql/my.cnf. If you want to
use a customized MySQL configuration, you can create your alternative
configuration file in a directory on the host machine and then mount
that directory location as /etc/mysql/conf.d inside the mysql
container.
If /my/custom/config-file.cnf is the path and name of your custom
configuration file, you can start your mysql container like this (note
that only the directory path of the custom config file is used in this
command):
$ docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e
MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
This will start a new
container some-mysql where the MySQL instance uses the combined
startup settings from /etc/mysql/my.cnf and
/etc/mysql/conf.d/config-file.cnf, with settings from the latter
taking precedence.
From that point and if you create the my.cnf file on your host then you'll never run into this problem again since you can edit the file as many times as you want.