3

I am trying to use a MySQL image on docker, attaching a volume, furthermore I would like to add a sql script in order to create a table if not present yet. So if the container is used in another machine the table will be Always present.


My command :

docker run -d -p 3306:3306 --name my-mysql --network sma -v /scripts:/docker-entrypoint-initdb.d/ -v /myvolume/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=myDB mysql

My situation: I am able to attach the volume with -v option (/myvolume/:/var/lib/mysql) during the run, and actually I am also able to insert the script in the init directory ( /docker-entrypoint-initdb.d/ ) but if I do these two things, only the volume attaching will work.

I guess it is something like the script is executed (because it is placed in the directory) but then the MySQL is overwritten by the volume attaching, so the only thing I am seeing is what is present in myvolume.

There is some way that makes that work?

Lorenzo Vitali
  • 310
  • 3
  • 9
  • 1
    Possible duplicate of [How to create populated MySQL Docker Image on build time](https://stackoverflow.com/questions/32482780/how-to-create-populated-mysql-docker-image-on-build-time) – David Maze Nov 30 '18 at 16:54

1 Answers1

1

I resolved using it in a swarm from a docker-compose with docker stack deploy -c docker-compose.yml swarm_name.

In the service definition of the docker-compose I added the command line in order to force it to execute the init script.

command: --init-file /docker-entrypoint-initdb.d/initDb.sql
Lorenzo Vitali
  • 310
  • 3
  • 9