1

I'm trying to build a custom MariaDB docker image based on the newly released Rocky Linux for testing purposes. However it seems that running the service, preparing initial database schema like setting up the root password, importing data, etc. is complicated due to the absence of systemd. Even by enabling systemctl through python (workaround), the build process halts when encountering systemctl start mariadb.

Dockerfile:

FROM rockylinux/rockylinux

# INT
RUN yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y
RUN yum install supervisor -y
ADD ./assets/SYS/supervisor.conf /etc/supervisor.conf

# SYS
RUN yum install python2 -y
RUN ln -s /usr/bin/python2 /usr/bin/python
RUN unlink /usr/bin/systemctl
ADD ./assets/SYS/systemctl /usr/bin/systemctl
RUN chmod +x /usr/bin/systemctl

# DBS
RUN yum install mariadb-server -y

# Method 1
RUN systemctl start mariadb
# [ freezes the build process ]

# Method 2
RUN exec /usr/bin/mysqld_safe
# [ runs the service but there is a mysql.sock error ]

# Method 3
RUN exec /usr/libexec/mysqld --defaults-file=/etc/my.cnf --user=root
# [ does not run the service ]

RUN mysql_upgrade -u root --force
RUN mysql -u root -e 'DELETE FROM mysql.user WHERE User="";'
RUN mysql -u root -e 'DELETE FROM mysql.user WHERE User="root" AND Host NOT IN ("localhost", "127.0.0.1", "::1");'
RUN mysql -u root -e 'GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "";'
RUN mysql -u root -e 'DROP DATABASE IF EXISTS test;'
RUN mysql -u root -e 'DELETE FROM mysql.db WHERE Db="test" OR Db="test\\_%";'
RUN mysql -u root -e 'FLUSH PRIVILEGES;'
RUN mysql -u root -e 'CREATE DATABASE somedatabase;'
ADD ./assets/DBS/database.sql database.sql
RUN mysql -u root somedatabase < database.sql
RUN unlink database.sql
RUN mysqladmin password "somepassword"
RUN systemctl stop mariadb

EXPOSE 3306

CMD ["supervisord", "-c", "/etc/supervisor.conf"]

supervisor.conf

[program:mariadb]
command=/usr/bin/mysqld_safe --basedir=/usr 
process_name=%(program_name)s_%(process_num)02d
numprocs=1
autostart=true
autorestart=false
startsecs=0
redirect_stderr=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
  • 1
    A Docker image never contains running processes, and if you try to start a process in a `RUN` step it will be terminated before the next `RUN` step happens. You might look at how the [official `mariadb` image](https://hub.docker.com/_/mariadb) does this first-time setup; you need to start the database and do the initialization all in the same script (normally at container startup time). I'd avoid systemd in a Docker container since it wants to manage the entire host, including a wide swath of things a container normally isolates. – David Maze Oct 22 '21 at 10:30
  • Thank you @DavidMaze. So basically I should prepare a single bash script to manage the entire setup process within one RUN dommand. Correct? – Cena Ebrahimiyan Oct 22 '21 at 10:35
  • Yes (it might be more portable if you can limit yourself to POSIX shell syntax without bash extensions). – David Maze Oct 22 '21 at 10:38
  • What are you gaining using a Rocky Linux base? Also see [official image source on github](https://github.com/MariaDB/mariadb-docker). – danblack Oct 22 '21 at 22:43
  • [Slightly dated POC from RHEL8](https://github.com/grooverdan/ubi-mariadb) which might be of use. – danblack Oct 23 '21 at 04:56
  • Thank you for your replies. As I've mentioned this is for testing purposes only. I'm well aware of the official images and I'm already using them. – Cena Ebrahimiyan Oct 26 '21 at 13:19
  • @danblack Thanks for the link. It's a good place to start fiddling. – Cena Ebrahimiyan Oct 26 '21 at 13:21

1 Answers1

0

I am not sure whether the python workaround refers to the docker-systemctl-replacement script. Then there is only the problem left that single RUN needs both "start" and "stop" of the service.

There is a working example for mysql that can show the defaults about it. https://github.com/gdraheim/docker-systemctl-images/blob/master/centos-lamp-stack.dockerfile

Guido U. Draheim
  • 3,038
  • 1
  • 20
  • 19
  • Thank you for your reply. First, yes, the workaround is indeed the python script. Second, the issue is, starting the service while building the image to prepare its internal schema seems to halt the build process. – Cena Ebrahimiyan Oct 26 '21 at 13:24