Entrypoint in Dockerfile - mysql

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.

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"

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!

How can I create a docker image with a initialized database

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.

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.

Docker db container running. Another process with pid <id> is using unix socket file

I'm trying to run a docker mysql container with initialized db according instruction provided in this message https://stackoverflow.com/a/29150538/6086816. After first run it works ok, but on second run, after trying of executing /usr/sbin/mysqld from script, I get this error:
db_1 | 2016-03-19T14:50:14.819377Z 0 [ERROR] Another process with pid 10 is using unix socket file.
db_1 | 2016-03-19T14:50:14.819498Z 0 [ERROR] Unable to setup unix socket lock file.
...
mdir_db_1 exited with code 1
what can be the reason of it?
I was facing the same issue. Following are the steps that I tried to resolve this issue -
Firsly, stop your docker service by using following command - "sudo service docker stop"
Now,get into the docker folder in my Linux system using the following path -
/var/lib/docker.
Then within the docker folder you need to get into the volumes folder. This folder contains the volumes of all your containers (memory of each container) -
cd /volumes
After getting into volumes do 'sudo ls' you will find multiple folders with hash names. These folders are volumes of your containers. Each folder is named after its hash
(You need to inspect your docker container and get the hash of your container volume. For this, you need to do the following steps -
Run command "docker inspect 'your container ID' ".
Now you will get a JSON file. It is the config file of your docker container.
Seach for Mounts key within this JSON file. In Mounts, you will get the Name(hash) of your volume. (You will also get the path of your volume within the Mounts. Within Mounts "Name" key is your volume name and "Source" is the path where your volume is located.)).
Once you get the name of your volume you can go within your volume folder and within this folder you will find "_data" folder. Get into this folder.
Finally within "_data" folder use sudo ls command and you will find a folder with the name mysql.sock.lock. Remove this folder By "rm -f mysql.sock.lock".
Now restart your docker service and then start your docker container. It will start working.
Note- Use sudo in each command while you are in the docker container folder.
You should make sure the socket file have been deleted before you start mysql.Check my.cnf(/etc/mysql/my.cnf) file to get the path of socket file.
find sth like this socket = /var/run/mysqld/mysqld.sock.And delete the .sock.lock file as well.
This is a glitch with docker.
Execute following commands:
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
This will stop all containers and remove them.
After this it should work just fine.
Just faced same problem.
After many research, summary of my solution:
Find host location of docker files
$ docker inspect <container_name> --> Mounts.Source section
In my case, it was /var/snap/docker/common/.../_data
As root, you can ls -l that directory and see the files that are preventing your container from starting, the socket mysql.sock and the file mysql.sock.lock
Simply delete them as root ($ sudo rm /var/snap/.../_data/mysql.sock*) and start your docker container.
NOTE: be sure you don't have any other mysql.sock... files than those two. In that case don't use wildcar (*), delete each of them individually.
Hope this helps.
I had the same problem and got rid of it in an easy and mysterious way.
First I have noticed that I am unable to start mysql_container container. Running docker logs mysql_container indicated exactly the same problem as described repeating for few times.
I wanted to get a look around by running the container in an interactive mode by docker start -i mysql_container from one bash window while running things like
docker exec -it mysql_container cat /etc/mysql/my.cnf in another.
I have done that and was very surprised to see that this time the container started successfully. I cannot understand why. I can only guess that starting an interactive mode together with running subsequent docker exec commands slowed down init process and some another process had a bit more time to remove its locks.
Hope that helps anybody.