I am trying to get a bash script to run when my MySQL container starts. Not the initial time when there are no databases to create, but subsequent times (so placing the files in docker-entrypoint-initdb.d will not work).
My objective is to re-build my container with some database upgrade scripts (schema changes, etc). The thought being I deploy the container with the initial scripts and deploy subsequent updates with my database upgrades as the application ages. It seems like this would be an easy task, but I am having trouble.
Most of the things I have tried came from suggestions I found googling. Here are things I have tried with no success:
Modify the
entrypoint.sh
(and/usr/local/bin/docker-entrypoint.sh
) in the Dockerfile build to add in a call to my script.- This does not even seem to be called, which I suspect is a sign, but my database starts (also note it creates my schema fine the first time)
- I do this with a
RUN sed
in my Dockerfile and have confirmed my changes exist after the container starts
Tried running my script on startup by:
- adding a script to
/etc/r.d/rc.local
- adding a restart cron job (well, I tried, but the Oracle Linux distro doesn’t have it)
— Modifying the
/etc/bashrc
— Adding a script to/etc/profile.d/
— Appending to/etc/profie.d/sh.local
- adding a script to
Tried adding a command to my
docker-compose.yml
, but it said that wasn’t found.
My actual database upgrade script works great when I log in to the container manually and execute it. All of my experiments above have been just touching a file or echoing to a file as a proof of concept. Once I get that working, I'll add in the logic to wait for MySQL to start and then run my actual script.
Dockerfile:
FROM mysql:8.0.32
VOLUME /var/lib/mysql
## these are my experiments
RUN sed -i '/main "$@"/a echo "run the script here" > /usr/tmp/XXX' /entrypoint.sh
RUN sed -i '/main "$@"/a echo "run the script here" > /usr/tmp/XXX' /usr/local/bin/docker-entrypoint.sh
RUN echo "touch /usr/tmp/XXX" >> /etc/profile.d/sh.local
RUN sed -i '/doublesourcing/a echo “run the script here > /usr/tmp/XXX' etc/bashrc
I build and run it using:
docker build -t mysql-database -f Dockerfile .
docker run -it --rm -d -p 3306:3306 --name database -v ~/Docker-Volume-Share/database:/var/lib/mysql mysql-database
Some other information that may be useful
- I am using a volume on the host. I’ve run my experiments with an existing schema as well as by deleting this directory so it starts fresh
- I am using
mysql:8.0.32
as the image (Oracle Linux Server release 8.7) - Docker version
20.10.22, build 3a2c30b
- Host OS is
macOS 13.2.1
Thanks in advance for any tips and guidance!