创建运行在 RHEL 系统上的 Docker 容器中的 PostgreSQL 9.6

在RHEL上创建一个PostgreSQL的Docker容器。
只要是开发目的,就可以免费使用Red Hat Enterprise Linux。
https://redhat.sios.jp/red-hat-developer-program

我想利用这个来在RHEL上创建一个安装了PostgreSQL的Docker镜像。
请参考上述文章以进行开发者计划的注册。

为了作为intra-mart的验证目的,我们将提前执行intra-mart要求的设置。这样一来,我们就可以立即创建一个可用于验证的PostgreSQL 9.6环境,而无需考虑其他事项,只需运行docker命令即可。

FROM registry.access.redhat.com/rhel7.6

EXPOSE 22 5432
ENV DEBIAN_FRONTEND noninteractive

# subscription
COPY subscribe_rhel.sh /subscribe_rhel.sh
RUN chmod +x /subscribe_rhel.sh \
 && /subscribe_rhel.sh

# yum
RUN yum -y update \
 && yum provides -y '*/applydeltarpm' \
 && yum groupinstall -y 'Development Tools' \
 && yum install -y initscripts curl tar unzip mlocate openssh-server openssl-devel \
 && yum localinstall -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-redhat96-9.6-3.noarch.rpm \
 && yum install -y postgresql96-server epel-release multitail ncurses-devel ncurses-static ncurses-term \
 && rm -rf /var/cache/yum/* \
 && yum clean all

# locale
RUN yum reinstall -y glibc-common \
 && localedef -i ja_JP -f UTF-8 ja_JP.utf8 \
 && touch /etc/sysconfig/i18n \
 && echo 'LANG="ja_JP.UTF-8"' >> /etc/sysconfig/i18n \
 && echo 'LANG="ja_JP.UTF-8"' > /etc/locale.conf
ENV LANG ja_JP.UTF-8
ENV LC_ALL ja_JP.UTF-8
ENV LANGUAGE ja_JP:ja

# timezone
RUN yum install -y tzdata \
 && echo 'ZONE="Asia/Tokyo"' > /etc/sysconfig/clock \
 && echo 'UTC=false' >> /etc/sysconfig/clock \
 && ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

# root passwd
RUN bash -c 'echo "root:password" | chpasswd'

# ssh
RUN sed -i -e "s/#PasswordAuthentication yes/PasswordAuthentication yes/g" /etc/ssh/sshd_config \
 && sed -i -e "s/#PermitRootLogin yes/PermitRootLogin yes/g" /etc/ssh/sshd_config \
 && sed -i -e "s/UsePAM yes/UsePAM no/g" /etc/ssh/sshd_config

# PostgreSQL
COPY setup_postgresql.sh /setup_postgresql.sh
RUN chmod +x /setup_postgresql.sh \
 && /setup_postgresql.sh \
 && rm -f /setup_postgresql.sh

COPY run.sh /run.sh
RUN chmod +x /run.sh

RUN updatedb

CMD ["/run.sh"]

由于无法使用yum,因此执行subscription-manager register。
请设置您的用户名和密码以配置您的帐户。

#!/bin/sh

subscription-manager register --username 'FIXME' --password 'FIXME' --auto-attach

exit 0

关于setup_postgresql.sh,在CentOS 6/RHEL 6上可以按以下方式进行安装设置。

#!/bin/bash

service postgresql-9.6 initdb

echo "listen_addresses = '*'" >> /var/lib/pgsql/9.6/data/postgresql.conf
sed -i -e "s/ident$/trust/g" /var/lib/pgsql/9.6/data/pg_hba.conf
sed -i -e "s/127\.0\.0\.1\/32/0\.0\.0\.0\/0/g" /var/lib/pgsql/9.6/data/pg_hba.conf

sed -i -e "s/max_connections = 100/max_connections = 200/g" /var/lib/pgsql/9.6/data/postgresql.conf
sed -i -e "s/shared_buffers = 128MB/shared_buffers = 512MB/g" /var/lib/pgsql/9.6/data/postgresql.conf

echo "postgres:postgres" | chpasswd

service postgresql-9.6 start
sleep 1

su postgres -c "psql -c \"ALTER USER postgres WITH PASSWORD 'postgres'\""
su postgres -c "psql -c \"CREATE ROLE imart WITH LOGIN PASSWORD 'imart'\""
su postgres -c "psql -c \"CREATE DATABASE imart OWNER=imart encoding 'utf8' TEMPLATE template0\""
su postgres -c "psql -c \"CREATE DATABASE iap_db OWNER=imart encoding 'utf8' TEMPLATE template0\""
su postgres -c "psql -c \"CREATE DATABASE \\\"default\\\" OWNER=imart encoding 'utf8' TEMPLATE template0\""
su postgres -c "psql -d imart -c \"CREATE SCHEMA acceldocuments AUTHORIZATION imart\""
su postgres -c "psql -d iap_db -c \"CREATE SCHEMA acceldocuments AUTHORIZATION imart\""
su postgres -c "psql -d default -c \"CREATE SCHEMA acceldocuments AUTHORIZATION imart\""

service postgresql-9.6 stop

在这里的问题是在RHEL 7中,我们使用的是systemctl而不是service,但在docker build中无法使用systemctl。

# service postgresql-9.6 initdb
The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.

即使强行实施,

# /usr/pgsql-9.6/bin/postgresql96-setup initdb
Failed to get D-Bus connection: Operation not permitted
failed to find PGDATA setting in postgresql-9.6.service

有问题的地方是以下部分。

# this parsing technique fails for PGDATA pathnames containing spaces,
# but there's not much I can do about it given systemctl's output format...
PGDATA=`systemctl show -p Environment "${SERVICE_NAME}.service" |
                sed 's/^Environment=//' | tr ' ' '\n' |
                sed -n 's/^PGDATA=//p' | tail -n 1`

通过以下方式,

sed -i -e "s/if \[ x\"\$PGDATA\" = x \]; then/PGDATA=\/var\/lib\/pgsql\/9.6\/data\/\nif \[ x\"\$PGDATA\" = x \]; then/g" /usr/pgsql-9.6/bin/postgresql96-setup
if [ x"$PGDATA" = x ]; then

我需要更多上下文才能完整理解你的问题。可以提供更多细节或句子吗?谢谢!

PGDATA=/var/lib/pgsql/9.6/data/
if [ x"$PGDATA" = x ]; then

可以通过以下方法使用 pg_ctl 进行 initdb,而不是先替换为 /usr/pgsql-9.6/bin/postgresql96-setup initdb。这样做可能会感到有些强制。

su postgres -c "/usr/pgsql-9.6/bin/pg_ctl -D /var/lib/pgsql/9.6/data initdb"

接下来要讨论的是 service start 和 service stop 这两个问题。

# It's not recommended to modify this file in-place, because it will be
# overwritten during package upgrades.  If you want to customize, the
# best way is to create a file "/etc/systemd/system/postgresql-9.6.service",
# containing
#	.include /lib/systemd/system/postgresql-9.6.service
#	...make your changes here...
# For more info about custom unit files, see
# http://fedoraproject.org/wiki/Systemd#How_do_I_customize_a_unit_file.2F_add_a_custom_unit_file.3F

# Note: changing PGDATA will typically require adjusting SELinux
# configuration as well.

# Note: do not use a PGDATA pathname containing spaces, or you will
# break postgresql-setup.
[Unit]
Description=PostgreSQL 9.6 database server
Documentation=https://www.postgresql.org/docs/9.6/static/
After=syslog.target
After=network.target

[Service]
Type=notify

User=postgres
Group=postgres

# Note: avoid inserting whitespace in these Environment= lines, or you may
# break postgresql-setup.

# Location of database directory
Environment=PGDATA=/var/lib/pgsql/9.6/data/

# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog

# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000
Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
Environment=PG_OOM_ADJUST_VALUE=0

ExecStartPre=/usr/pgsql-9.6/bin/postgresql96-check-db-dir ${PGDATA}
ExecStart=/usr/pgsql-9.6/bin/postmaster -D ${PGDATA}
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
 

# Do not set any timeout value, so that systemd will not kill postmaster
# during crash recovery.
TimeoutSec=0

[Install]
WantedBy=multi-user.target

基于这样的情况,您可以通过以下方式启动/关闭。

# 起動
su postgres -c "PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj; PG_OOM_ADJUST_VALUE=0; /usr/pgsql-9.6/bin/pg_ctl -D /var/lib/pgsql/9.6/data -l /var/lib/pgsql/9.6/data/pg_log/postgresql.log start"
# 終了
su postgres -c "/usr/pgsql-9.6/bin/pg_ctl -D /var/lib/pgsql/9.6/data stop"

总结起来,可以按照以下方式进行设置。

#!/bin/bash

export PGSQL=/usr/pgsql-9.6
export PGDATA=/var/lib/pgsql/9.6/data
su postgres -c "${PGSQL}/bin/pg_ctl -D $PGDATA initdb"

echo "listen_addresses = '*'" >> ${PGDATA}/postgresql.conf
sed -i -e "s/ident$/trust/g" ${PGDATA}/pg_hba.conf
sed -i -e "s/127\.0\.0\.1\/32/0\.0\.0\.0\/0/g" ${PGDATA}/pg_hba.conf

sed -i -e "s/max_connections = 100/max_connections = 200/g" ${PGDATA}/postgresql.conf
sed -i -e "s/shared_buffers = 128MB/shared_buffers = 512MB/g" ${PGDATA}/postgresql.conf

echo "postgres:postgres" | chpasswd

su postgres -c "mkdir -p ${PGDATA}/pg_log"
su postgres -c "PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj; PG_OOM_ADJUST_VALUE=0; ${PGSQL}/bin/pg_ctl -D $PGDATA -l ${PGDATA}/pg_log/postgresql.log start"
sleep 2

su postgres -c "psql -c \"ALTER USER postgres WITH PASSWORD 'postgres'\""
su postgres -c "psql -c \"CREATE ROLE imart WITH LOGIN PASSWORD 'imart'\""
su postgres -c "psql -c \"CREATE DATABASE imart OWNER=imart encoding 'utf8' TEMPLATE template0\""
su postgres -c "psql -c \"CREATE DATABASE iap_db OWNER=imart encoding 'utf8' TEMPLATE template0\""
su postgres -c "psql -c \"CREATE DATABASE \\\"default\\\" OWNER=imart encoding 'utf8' TEMPLATE template0\""
su postgres -c "psql -d imart -c \"CREATE SCHEMA acceldocuments AUTHORIZATION imart\""
su postgres -c "psql -d iap_db -c \"CREATE SCHEMA acceldocuments AUTHORIZATION imart\""
su postgres -c "psql -d default -c \"CREATE SCHEMA acceldocuments AUTHORIZATION imart\""

su postgres -c "${PGSQL}/bin/pg_ctl -D $PGDATA stop"
#!/bin/bash

/subscribe_rhel.sh
/usr/sbin/sshd-keygen
/usr/sbin/sshd
su postgres -c "PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj; PG_OOM_ADJUST_VALUE=0; /usr/pgsql-9.6/bin/pg_ctl -D /var/lib/pgsql/9.6/data restart"

multitail -M 0 --follow-all --retry-all -q 1 "/var/lib/pgsql/9.6/data/pg_log/*.log"

我会建立(build)。

docker build -t mypostgresql:9.6 .

打开

docker run -itd -p 5432:5432 -p 2222:22 mypostgresql:9.6

SSH

ssh -p 2222 root@localhost

如果在docker run命令中使用了-p 2222:22选项,那么2222就是SSH的端口。
您可以使用root/password登录。
请将localhost替换为正在运行docker的计算机的IP地址。

因此,我們簡單介紹了為intra-mart準備的已配置的PostgreSQL 9.6 Docker。

广告
将在 10 秒后关闭
bannerAds