I am trying to create a container with a MySQL database and add a schema to these database.
My current Dockerfile is:
FROM mysql
MAINTAINER (me) <email>
# Copy the database schema to the /data directory
COPY files/epcis_schema.sql /data/epcis_schema.sql
# Change the working directory
WORKDIR data
CMD mysql -u $MYSQL_USER -p $MYSQL_PASSWORD $MYSQL_DATABASE < epcis_schema.sql
In order to create the container I am following the documentation provided on Docker and executing this command:
docker run --name ${CONTAINER_NAME} -e MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD} -e MYSQL_USER=${DB_USER} -e MYSQL_PASSWORD=${DB_USER_PASSWORD} -e MYSQL_DATABASE=${DB_NAME} -d mvpgomes/epcisdb
But when I execute this command the Container is not created and in the Container status it is possible to see that the CMD was not executed successfully, in fact only the mysql
command is executed.
Anyway, is there a way to initialize the database with the schema or do I need to perform these operations manually?
I am sorry for this super long answer, but, you have a little way to go to get where you want. I will say that normally you wouldn't put the storage for the database in the same container as the database itself, you would either mount a host volume so that the data persists on the docker host, or, perhaps a container could be used to hold the data (/var/lib/mysql). Also, I am new to mysql, so, this might not be super efficient. That said...
I think there may be a few issues here. The Dockerfile is used to create an image. You need to execute the build step. At a minimum, from the directory that contains the Dockerfile you would do something like :
The Dockerfile describes the image to create. I don't know much about mysql (I am a postgres fanboy), but, I did a search around the interwebs for 'how do i initialize a mysql docker container'. First I created a new directory to work in, I called it mdir, then I created a files directory which I deposited a epcis_schema.sql file which creates a database and a single table:
Then I created a script called init_db in the files directory:
(most of this script was lifted from here: https://gist.github.com/pda/9697520)
Here is the files/run_db script I created:
Finally, the Dockerfile to bind them all:
So, I cd'ed to my mdir directory (which has the Dockerfile along with the files directory). I then run the command:
You should see output like this:
You now have an image '7f6d5215fe8d'. I could run this image:
and the image starts, I see an instance string:
I could then 'stop' it, and restart it.
If you look at the logs, the first line will contain:
Then, at the end of the logs:
These are the messages from the /tmp/run_db script, the first one indicates that the database was unpacked from the saved (initial) version, the second one indicates that the database was already there, so the existing copy was used.
Here is a ls -lR of the directory structure I describe above. Note that the init_db and run_db are scripts with the execute bit set:
After to struggle a little bit with that, take a look the Dockerfile using named volumes (db-data). It's important declare a plus at final part, where I mentioned that volume is
[external]
All worked great this way!
Below is the Dockerfile I used successfully to install xampp, create a MariaDB with scheme and pre populated with the info used on local server(usrs,pics orders,etc..)
Another way based on a merge of serveral responses here before :
docker-compose file :
where
/home/user
.. is a shared folder on the hostAnd in the
/home/user/db/mysql/init
folder .. just drop one sql file, with any name, for exampleinit.sql
containing :According to the official mysql documentation, you can put more than one sql file in the
docker-entrypoint-initdb.d
, they are executed in the alphabetical orderThe easiest solution is to use tutum/mysql
Step1
Step2
Step3
Get above CONTAINER_ID and then execute command
docker logs
to see the generated password information.For the ones not wanting to create an entrypoint script like me, you actually can start mysqld at build-time and then execute the mysql commands in your Dockerfile like so:
The key here is to send mysqld_safe to background with the single
&
sign.