如何使用centos:8dockerimage运行postgresql客户端?

How to use centos:8 docker image run postgresql client?

在 centos 8 Dockerfile 中

FROM centos:8

RUN yum update -y && yum install -y openssh-server sudo && yum clean all
RUN rm -rf /run/nologin

EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

启动后,在容器中安装postgresql客户端

docker exec -it centos8 /bin/bash

dnf module enable postgresql:12
dnf install postgresql-server
postgresql-setup --initdb

遇到错误

[root@centos8 /]# postgresql-setup --initdb
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
FATAL: no db datadir (PGDATA) configured for 'postgresql.service' unit

检查状态

[root@centos8 /]# systemctl status postgresql
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

当运行psql

[root@centos8 /]# psql
psql: error: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

为什么不能使用systemctl服务?如何在上面安装 Postgres 客户端?

Systemd 在 docker 容器中不受支持。所以如果你想启动 postgresql server 服务,你必须直接通过执行 postgresql 二进制文件来启动它,就像 how official do, also refers to its Dockerfile:

exec gosu postgres "$BASH_SOURCE" "$@"

但是,你接下来提到:

How to install a Postgres client on it?

所以,如果你只想安装 postgres 客户端,你已经完成了:psql 是 postgres 客户端,不需要启动 postgresql 服务器:

[root@4234406b3e6c /]# psql --help
psql is the PostgreSQL interactive terminal.