BUG #17191: Docker / buildx issue - An image created with buildx requires to set custom PGDATA location

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: wlad(at)0trust(dot)de
Subject: BUG #17191: Docker / buildx issue - An image created with buildx requires to set custom PGDATA location
Date: 2021-09-13 12:27:41
Message-ID: 17191-8b39f97dabd7d8f9@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 17191
Logged by: Wlad
Email address: wlad(at)0trust(dot)de
PostgreSQL version: 13.4
Operating system: linux
Description:

I noticed a different behavior of postgres docker containers depending on
whether the image was build with `docker build`
(normal Docker) or with `docker buildx build` (Moby BuildKit).

## Case 1 - the failing case (docker buildx / Moby BuildKit)

```dockerfile
# syntax=docker/dockerfile:1
FROM --platform=$BUILDPLATFORM postgres:13.4-alpine
ARG TARGETPLATFORM
ARG BUILDPLATFORM
RUN echo "Running on $BUILDPLATFORM, building EHRbase PostgreSQL DB for
$TARGETPLATFORM" > /log

# SHOW POSTGRES SERVER AND CLIENT VERSION
RUN postgres -V; \
psql -V

# SET DEFAULT VALUES FOR DATABASE USER AND PASSWORDS
ARG EHRBASE_USER="ehrbase"
ARG EHRBASE_PASSWORD="ehrbase"
ENV EHRBASE_USER=${EHRBASE_USER}
ENV EHRBASE_PASSWORD=${EHRBASE_PASSWORD}

# COPY DB SETUP SCRIPT TO POSTGRES's DEFAULT DOCKER ENTRYPOINT FOLDER
# NOTE: check postgres's docker docs for details
# https://hub.docker.com/_/postgres/
COPY scripts/db-setup.sql /docker-entrypoint-initdb.d/

# ALLOW CONNECTIONS FROM ALL ADRESSES & LISTEN TO ALL INTERFACES
# NOTE: locally works w/o this additional settings
RUN echo "host all all 0.0.0.0/0 scram-sha-256" >>
${PGDATA}/pg_hba.conf; \
echo "listen_addresses='*'" >> ${PGDATA}/postgresql.conf; \
ls -la ${PGDATA}

```

Build image with `docker buildx build --push
--platform=linux/arm64,linux/amd64 .`

The image created this way REQUIREs TO PROVIDE a custom PGDATA location,
otherwise it will exit i.e.

### with custom PGDATA location (succeeds)
```
docker run --name ehrdb \
-e POSTGRES_PASSWORD=mypostgres \
-e EHRBASE_USER=myuser \
-e EHRBASE_PASSWORD=mypassword \
-e PGDATA=/tmp \
-d -p 5432:5432 \
ehrbase/ehrbase-postgres:13.4
```

### without custom PGDATA location (fails)
```
docker run --name ehrdb \
-e POSTGRES_PASSWORD=mypostgres \
-e EHRBASE_USER=myuser \
-e EHRBASE_PASSWORD=mypassword \
-d -p 5432:5432 \
ehrbase/ehrbase-postgres:13.4

docker logs ehrdb

>>> The files belonging to this database system will be owned by user
"postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

initdb: error: directory "/var/lib/postgresql/data" exists but is not
empty
If you want to create a new database system, either remove or empty
the directory "/var/lib/postgresql/data" or run initdb
with an argument other than "/var/lib/postgresql/data".
```

## Case 2 (normal docker build)

Apart from

```
# syntax=docker/dockerfile:1
FROM --platform=$BUILDPLATFORM postgres:13.4-alpine
```
the Dockerfile below is identical with the one from Case 1.

```dockerfile
FROM postgres:13.4-alpine
ARG TARGETPLATFORM
ARG BUILDPLATFORM
RUN echo "Running on $BUILDPLATFORM, building EHRbase PostgreSQL DB for
$TARGETPLATFORM" > /log

# SHOW POSTGRES SERVER AND CLIENT VERSION
RUN postgres -V; \
psql -V

# SET DEFAULT VALUES FOR DATABASE USER AND PASSWORDS
ARG EHRBASE_USER="ehrbase"
ARG EHRBASE_PASSWORD="ehrbase"
ENV EHRBASE_USER=${EHRBASE_USER}
ENV EHRBASE_PASSWORD=${EHRBASE_PASSWORD}

# COPY DB SETUP SCRIPT TO POSTGRES's DEFAULT DOCKER ENTRYPOINT FOLDER
# NOTE: check postgres's docker docs for details
# https://hub.docker.com/_/postgres/
COPY scripts/db-setup.sql /docker-entrypoint-initdb.d/

# ALLOW CONNECTIONS FROM ALL ADRESSES & LISTEN TO ALL INTERFACES
# NOTE: locally works w/o this additional settings
RUN echo "host all all 0.0.0.0/0 scram-sha-256" >>
${PGDATA}/pg_hba.conf; \
echo "listen_addresses='*'" >> ${PGDATA}/postgresql.conf; \
ls -la ${PGDATA}
```

Build image with `docker build -t ehrbase/ehrbase-postgres:13.4 .`

#### The image created this way can be run with OR without providing a
custom PGDATA location, i.e.

### with custom PGDATA location
```
docker run --name ehrdb \
-e POSTGRES_PASSWORD=mypostgres \
-e EHRBASE_USER=myuser \
-e EHRBASE_PASSWORD=mypassword \
-e PGDATA=/tmp \
-d -p 5432:5432 \
ehrbase/ehrbase-postgres:13.4
```

### without custom PGDATA location
```
docker run --name ehrdb \
-e POSTGRES_PASSWORD=mypostgres \
-e EHRBASE_USER=myuser \
-e EHRBASE_PASSWORD=mypassword \
-d -p 5432:5432 \
ehrbase/ehrbase-postgres:13.4
```

For better readability find all above info MarkDown formated on Github Gist
https://gist.github.com/wlad/db6e99e46c5da8751d50d454479db42c

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message David G. Johnston 2021-09-13 12:56:19 Re: BUG #17189: Index not created when primary key created
Previous Message hubert depesz lubaczewski 2021-09-13 10:36:31 Re: BUG #17189: Index not created when primary key created