Docker run ... -v <volume>:/var/lib/mysql automatically exit

I try to mount a created database from host, name mydb, onto mysql container here is what I’ve tried:

sudo docker run -v mysql-data:/var/lib/mysql/mydb/ --name mysql_web -e MYSQL_ROOT_PASSWORD=12345 -p 3306:3306 -d mysql:5.7

root@ip-172-31-16-134:/var/lib/mysql# sudo docker run -v mysql-data:/var/lib/mysql/mydb/ --name mysql_web -e MYSQL_ROOT_PASSWORD=12345 -p 3306:3306 -d mysql:5.7
3c0a8eb588c0dd0d0b7b72a727912744f44e744e700de7efc64a0c4c1f651685
root@ip-172-31-16-134:/var/lib/mysql# docker exec -it mysql_web bash
Error response from daemon: Container 3c0a8eb588c0dd0d0b7b72a727912744f44e744e700de7efc64a0c4c1f651685 is not running

but it works when I change where to mount…

sudo docker run -v mysql-data:/var/lib/mydb/ --name mysql_web -e MYSQL_ROOT_PASSWORD=12345 -p 3306:3306 -d mysql:5.7

root@ip-172-31-16-134:/var/lib/mysql# sudo docker run -v mysql-data:/var/lib/mydb/ --name mysql_web -e MYSQL_ROOT_PASSWORD=12345 -p 3306:3306 -d mysql:5.7
06233eeb864c32ff16d6542e632a4da3ff6dfbd4a30fcc9aac8086dfc1245948
root@ip-172-31-16-134:/var/lib/mysql# docker exec -it mysql_web bash
root@06233eeb864c:/# cd /var/lib/mydb
root@06233eeb864c:/var/lib/mydb# exit
exit

seems It can’t mount onto specific folder and I don’t know why, I just want both databases, one from host, and the other from container, synced itself instead of loading dump.sql everytime when I start a new container.

any suggestion would help , thanks

What does “docker logs mysql_web” output?
Cant you import a mysqldump instead?

ubuntu@ip-172-31-16-134:~$ sudo docker logs mysql_web
Initializing database
2019-07-26T15:16:49.598951Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-07-26T15:16:49.600247Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2019-07-26T15:16:49.600417Z 0 [ERROR] Aborting

it says “but data directory has files in it”, however I’m sure the destination folder, /var/lib/mysql/mydb, hasn’t been existing since the container is a new run… could you be explicit on to import mysqldump , thank you !

This error occurs since there is no mysql folder in the datadir, and it will try and run it as a empty database, which will fail since you put data into it.

One way of moving a database, is by useing mysqldump

Then you can put that sql file into the container, and it will IMPORT your db instead.
https://hub.docker.com/_/mysql

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.

1 Like