I'm new to docker and trying to initialize MySQL database. I have been following the resources in: How can I initialize a MySQL database with schema in a Docker container?
My folder structure:
My Dockerfile:
FROM mysql
# Copy the database schema to the /data directory
ADD files /tmp/
# init_db will create the default
# database from epcis_schema.sql, then
# stop mysqld, and finally copy the /var/lib/mysql directory
# to default_mysql_db.tar.gz
RUN chmod +x /tmp/init_db
RUN /tmp/init_db
# run_db starts mysqld, but first it checks
# to see if the /var/lib/mysql directory is empty, if
# it is it is seeded with default_mysql_db.tar.gz before
# the mysql is fired up
ENTRYPOINT "/tmp/files/run_db"
However, I'm getting an error when I try to run init_db script:
docker-compose up
Building db
Step 1/5 : FROM mysql
---> b5c10a3624ce
Step 2/5 : ADD files /tmp/
---> Using cache
---> 442ac0d3181e
Step 3/5 : RUN chmod +x /tmp/init_db
---> Using cache
---> c7f2a0abc246
Step 4/5 : RUN /tmp/init_db
---> Running in 7b4f397586f3
/bin/sh: 1: /tmp/init_db: not found
ERROR: Service 'db' failed to build : The command '/bin/sh -c /tmp/init_db' returned a non-zero code: 127
When I do chmod in Step 3, it is able to find the file; however, failing to run in Step 4.
init_db:
#!/bin/bash
# Initialize MySQL database.
# ADD this file into the container via Dockerfile.
# Assuming you specify a VOLUME ["/var/lib/mysql"] or `-v /var/lib/mysql` on the `docker run` command…
# Once built, do e.g. `docker run your_image /path/to/docker-mysql-initialize.sh`
# Again, make sure MySQL is persisting data outside the container for this to have any effect.
set -e
set -x
mysql_install_db
# Start the MySQL daemon in the background.
/usr/sbin/mysqld &
mysql_pid=$!
until mysqladmin ping >/dev/null 2>&1; do
echo -n "."; sleep 0.2
done
# Permit root login without password from outside container.
mysql -e "GRANT ALL ON *.* TO root@'%' IDENTIFIED BY '' WITH GRANT OPTION"
# create the default database from the ADDed file.
mysql < /tmp/create_stock_data_db.sql
# create historical_data table
mysql < /tmp/create_historical_data.sql
# Tell the MySQL daemon to shutdown.
mysqladmin shutdown
# Wait for the MySQL daemon to exit.
wait $mysql_pid
# create a tar file with the database as it currently exists
tar czvf default_mysql.tar.gz /var/lib/mysql
# the tarfile contains the initialized state of the database.
# when the container is started, if the database is empty (/var/lib/mysql)
# then it is unpacked from default_mysql.tar.gz from
# the ENTRYPOINT /tmp/run_db script
Any assistance will be appreciated. Thank you!