【发布时间】:2021-12-08 23:43:35
【问题描述】:
我正在尝试基于新发布的 Rocky Linux 构建自定义 MariaDB docker 映像以用于测试目的。然而,由于没有 systemd,运行服务、准备初始数据库模式(如设置 root 密码、导入数据等)似乎很复杂。即使通过 python 启用 systemctl(解决方法),构建过程也会在遇到 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
【问题讨论】:
-
Docker 映像从不包含正在运行的进程,如果您尝试在
RUN步骤中启动进程,它将在下一个RUN步骤发生之前终止。您可以看看officialmariadbimage 是如何进行首次设置的;您需要启动数据库并在同一个脚本中进行所有初始化(通常在容器启动时)。我会避免在 Docker 容器中使用 systemd,因为它想要管理整个主机,包括容器通常隔离的大量事物。 -
谢谢@DavidMaze。所以基本上我应该准备一个 bash 脚本来管理一个 RUN dommand 中的整个设置过程。对吗?
-
是的(如果您可以将自己限制在没有 bash 扩展的 POSIX shell 语法,它可能更便携)。
-
使用 Rocky Linux 基础您获得了什么?另见official image source on github。
标签: docker mariadb containers rhel8