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

From PG Bug reporting form
Subject BUG #17191: Docker / buildx issue - An image created with buildx requires to set custom PGDATA location
Date
Msg-id 17191-8b39f97dabd7d8f9@postgresql.org
Whole thread Raw
Responses Re: BUG #17191: Docker / buildx issue - An image created with buildx requires to set custom PGDATA location  ("Euler Taveira" <euler@eulerto.com>)
List pgsql-bugs
The following bug has been logged on the website:

Bug reference:      17191
Logged by:          Wlad
Email address:      wlad@0trust.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


pgsql-bugs by date:

Previous
From: hubert depesz lubaczewski
Date:
Subject: Re: BUG #17189: Index not created when primary key created
Next
From: "David G. Johnston"
Date:
Subject: Re: BUG #17189: Index not created when primary key created