Installing, configuring and running mariadb in foreground on Centos 7 via docker without systemd or services

Under CentOS 7, I understand we're moving from mysql-server to implementation-compatible MariaDB. I'm using a docker image of centos:latest, which puts me under the auspices of Centos 7.

mysqld_safe runs blocking in the foreground. That makes it easy: I just need to 0) install the package 1) change the root password and 2) run a server from within a Dockerfile

In the docker paradigm, for I need to be able to install MariaDB as if it were in a bash script. I've found various ways to do this using aptitude under Ubuntu but have yet to find an equivalent answer under yum: How do I install, configure and run mariadb on Centos 7 as if it were being installed via Bash script? mysql_secure_installation appears to require a TTY.

I've tried running the mysqladmin password command manually, but it complaints that it can't connect to a running MySQL instance. Because the containers are thrown away between steps, I believe I need to somehow run mysql and change the password in the same step.

I've tried installing initscripts package gets me /bin/service but it tries to redirect me to use systemctl start mariadb.service, which isn't usable because Docker containers get a fakesystemd and not systemd. Any ideas?

Here's my current Dockerfile variant (in this one, trying a tail -f to keep the process alive as a CMD)

FROM    centos:latest
MAINTAINER Me (some@guy.com)

RUN yum -y install wget epel-release
RUN cd /usr/local/src && wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm && rpm -Uvh remi-*.rpm && rm remi-*.rpm
RUN sed -i 's/enabled=0/enabled=1/' /etc/yum.repos.d/remi*.repo
RUN cd /usr/local/src && wget http://apt.sw.be/redhat/el7/en/x86_64/rpmforge/RPMS/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm  && rpm -Uvh rpmforge-*.rpm && rm rpmforge-*.rpm
RUN rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt
RUN sed -i 's/enabled=0/enabled=1/' /etc/yum.repos.d/rpmforge*.repo
RUN yum -y update
RUN yum -y upgrade

# mysql
RUN yum -y install mariadb-server
RUN yum -y install initscripts
WORKDIR /usr
#RUN echo "bind-address=0.0.0.0" >> /etc/my.cnf
RUN /usr/bin/mysql_install_db --datadir="/var/lib/mysql" --user=mysql
RUN /usr/bin/mysqld_safe --datadir="/var/lib/mysql" --socket="/var/lib/mysql/mysql.sock" --user=mysql  >/dev/null 2>&1 &
RUN /usr/bin/mysqladmin -u root password SOMEPASSWORD
CMD tail -f /var/log/mariadb/mariadb.log
EXPOSE 3306

Related:

I'm working on a similar issue, setting up mariadb in a Docker container. I found this looking for ways to run mysql_secure_install. There are ways to feed the input to the script, or you can simply run the commands that the script would run. I opted for the latter.

I also ran into the same problem of the commands not finding a running MySQL instance. The problem is that each step of your build is run in a separate container, so you would have to start mysql for each command, or run them all in one step. This is what I have in my Dockerfile:

ARG DATABASE_ROOT_PASS

RUN service mysql start \
    && mysqladmin -u root password "$DATABASE_ROOT_PASS" \
    && mysql -u root -p"$DATABASE_ROOT_PASS" -e "UPDATE mysql.user SET Password=PASSWORD('$DATABASE_ROOT_PASS') WHERE User='root'" \
    && mysql -u root -p"$DATABASE_ROOT_PASS" -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')" \
    && mysql -u root -p"$DATABASE_ROOT_PASS" -e "DELETE FROM mysql.user WHERE User=''" \
    && mysql -u root -p"$DATABASE_ROOT_PASS" -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\_%'" \
    && mysql -u root -p"$DATABASE_ROOT_PASS" -e "FLUSH PRIVILEGES"

You can then build it with

docker build . --build-arg DATABASE_ROOT_PASS='changeme123'

You can find the documentation for --build-arg here.

You misunderstand me. My problem with it is not running the service as a service under systemd, like every other container technology would.

Ahh, reason #1 why I don’t like Docker.

For the record, there are officially supported Docker images – but they don’t run on CentOS, and I am somewhat of a CentOS “fanboy.”