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

Viewed 984

In a 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"]

After start it, install postgresql client in the container

docker exec -it centos8 /bin/bash

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

Got an error

[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

Check status

[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

When run 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"?

Why can't use systemctl service? How to install a Postgres client on it?

1 Answers

Systemd not supported in docker container. So if you want to start postgresql server service, you will have to start it directly by execute the postgresql binary, just like how official do, also refers to its Dockerfile:

exec gosu postgres "$BASH_SOURCE" "$@"

But, you mentioned next:

How to install a Postgres client on it?

So, if you just want to install postgres client, you have already made it done: psql is the postgres client, no need to start postgresql server:

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