Fabot
Fabot

Reputation: 21

How to run a script shell just after the Dockerfile 'CMD ["mysqld"]'?

I have a MariaDB image running on Docker container, which has to update its root password at each initialization, for security reasons. There is a vault server from which my container gets new password each time it initializes. After a new password is obtained, my script has to be run to login into MariaDB database and update the password. I have tried several strategies to run such script as soon as the MariaDB database is up and runnig, but no success up to now. I do use a Dockerfile to build my MariaDB extended image. At the end, there is a 'CMD ["mysqld"]' that starts up the service. The problem is that my script has to be run just after the "CMD ["mysqld"]" command, since it is the command that initializes MariaDB process and then the it becomes "Ready for connections".

Is there any way to workaround this situation in order to run my shell script (["/foreground.sh"]) just after MariaDB service is up and runnig? It is important to recall that no human manipulation is allowed after the container is initialized. Therefore, every action has to take place via script shell, since this project is for a production environment, from where I have no access.

Relevant part of my Dockerfile

VOLUME /var/lib/mysql
EXPOSE 3306
COPY docker-entrypoint.sh /
COPY foreground.sh /
RUN chmod +x /docker-entrypoint.sh /foreground.sh
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["mysqld"] ["/foreground.sh"]

Relevant part of my "foreground.sh"

    mysql -u"root" -p"$ROOT_PASSWORD" <<MariaDB_INPUT
    UPDATE mysql.user SET authentication_string=PASSWORD("$MYSQL_ROOT_PASSWORD_NEW_VAULT") WHERE USER="root";
    UPDATE mysql.user SET authentication_string=PASSWORD("$MYSQL_PASSWORD_NEW_VAULT") WHERE USER="user";
    UPDATE mysql.user SET plugin="mysql_native_password";
    FLUSH PRIVILEGES;
    quit

MariaDB_INPUT

Upvotes: 1

Views: 1866

Answers (1)

rom
rom

Reputation: 1039

If you are using the official image for MariaDB, you can just add your script to the folder /docker-entrypoint-initdb.d/*.

Take a look in the docker-entrypoint.sh script:

  • Line 110 - the script is waiting for the instance to be running and accept queries.
  • Line 178 - the script runs customized scripts from the directory /docker-entrypoint-initdb.d/*

So you can just add your changing password script to the right place, and it will run after the instance is ready for updating the password. You don't have to change the CMD or the ENTRYPOINT values.

Upvotes: 2

Related Questions