So, I want that a script run just after the MySQL Docker container started (and had gone through the entrypoint defined in the mysql
image), I know about the existence of docker-entrypoint-initdb.d
, but there is a problem with this approach that I don't know how to fix: the script is executed under mysql
user, not root
, and I need root
access.
I tried creating a custom entrypoint that would source the docker-entrypoint.sh
, but it did not work:
#!/bin/bash
echo aaaa
source /.../docker-entrypoint.sh
_main "$@"
echo bbbb
This did not work, MySQL was not initialized after (the echos worked though).
So, what should I do to run a script as root
after the initialization of the database?
├── Dockerfile
└── entrypoint.sh
🗎 Dockerfile
FROM mysql:latest
COPY entrypoint.sh /
RUN chmod +x /entrypoint.sh
ENTRYPOINT [ "/entrypoint.sh" ]
🗎 entrypoint.sh
(This is the customised entrypoint. It runs the entrypoint from the base image then waits for the MySQL server to become available. Once it's available it confirms that the current user is root
and then it's ready to do whatever you require.)
#!/bin/bash
/usr/local/bin/docker-entrypoint.sh mysqld >/dev/null 2>&1 &
PID="$!"
while ! mysqladmin ping -h"localhost" --silent; do
echo "🟠 Wait for MySQL to be ready..."
sleep 1
done
echo "🟢 MySQL is ready."
echo "🟦 Current user: $(whoami)"
# Do other things here...
wait "$PID"