Postgresql 9.6 initdb在Centos 7 docker容器中失败

我正在尝试使用Centos7和PostgreSQL 9.6来设置Docker镜像。

我使用centos:centos7图像作为基础,并按照以下步骤操作: https ://wiki.postgresql.org/wiki/YUM_Installation

初始化数据库的步骤

sh-4.2# /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 

我试图按照这些步骤启用CentOS映像的systemd, https://hub.docker.com/_/centos/但是它没有帮助。

我也尝试在postgresql96-setup脚本中注释掉stmts

 #PGDATA=`systemctl show -p Environment "${SERVICE_NAME}.service" | # sed 's/^Environment=//' | tr ' ' '\n' | # sed -n 's/^PGDATA=//p' | tail -n 1` #if [ x"$PGDATA" = x ]; then # echo "failed to find PGDATA setting in ${SERVICE_NAME}.service" # exit 1 #fi 

现在initdb工作。 但是启动服务给了我相同的错误信息

 sh-4.2# systemctl enable postgresql-9.6.service Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-9.6.service to /usr/lib/systemd/system/postgresql-9.6.service. sh-4.2# systemctl start postgresql-9.6.service Failed to get D-Bus connection: Operation not permitted 

我错过了什么? 任何人都可以帮助我呢?

下面的问题已经解释,可以用这个答案来解决:

无法获得D-Bus连接:操作不允许 – 服务器故障

你做自己的新形象( centos7-systemd ),但它不像看起来那么难。 但要小心地做到这一点,通过运行你的新形象:

docker run --privileged -ti -e container=docker -v /sys/fs/cgroup:/sys/fs/cgroup centos7-systemd /usr/sbin/init

然后通过例如连接到它

$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6667f8acbe5c centos7-systemd "/usr/sbin/init" 33 minutes ago Up 13 minutes eager_brattain $ docker exec -it eager_brattain bash

然后在https://wiki.postgresql.org/wiki/YUM_Installation运行这些postgres指令,他们将正常工作。

“D-Bus连接:操作不允许”是Postgres想要与SystemD守护进程交谈的提示。 我通过使用https://github.com/gdraheim/docker-systemctl-replacement在一个centos-7docker容器中运行Postgres来避免这种情况。