Set a postgres docker config file as
Dockerfile
FROM postgres:12-alpine
USER postgres
RUN chmod 0700 /var/lib/postgresql/data &&\
initdb /var/lib/postgresql/data &&\
echo "host all all 0.0.0.0/0 md5" >> /var/lib/postgresql/data/pg_hba.conf &&\
echo "listen_addresses='*'" >> /var/lib/postgresql/data/postgresql.conf
pg_ctl start &&\
psql -c "ALTER USER postgres WITH ENCRYPTED PASSWORD 'mypassword';"
EXPOSE 5432
Deployed it on a server with docker-compose
version: "3.8"
services:
postgresql:
build:
context: ./postgresql
dockerfile: Dockerfile
image: postgresql
container_name: postgresql
hostname: postgresql
When build it, it can successful finished. But when up it, got
docker-compose up postgresql
Creating postgresql ... done
Attaching to postgresql
postgresql | Error: Database is uninitialized and superuser password is not specified.
postgresql | You must specify POSTGRES_PASSWORD to a non-empty value for the
postgresql | superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
postgresql |
postgresql | You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
postgresql | connections without a password. This is *not* recommended.
postgresql |
postgresql | See PostgreSQL documentation about "trust":
postgresql | https://www.postgresql.org/docs/current/auth-trust.html
postgresql exited with code 1
Check docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1lsls201qs0h postgresql "docker-entrypoint.s…" 2 minutes ago Exited (1) 2 minutes ago postgresql
The file permission on the server
ls -la postgresql/
Total 4
drwxr-xr-x 2 runuser runuser 24 April 14 01:02 .
drwxr-xr-x 5 runuser runuser 54 April 14 01:02 ..
-rwxr-xr-x 1 runuser runuser 541 April 14 01:02 Dockerfile
ls -la docker-compose.yaml
-rwxr-xr-x 1 runuser runuser 1110 April 14 01:02 docker-compose.yaml
I tested it on local, it works. The container can run correctly and the initial password setting also works. But on the server, is there any permission should be changed?