pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: wlad@0trust.de
Subject: BUG #17191: Docker / buildx issue - An image created with buildx requires to set custom PGDATA location
Date: Mon, 13 Sep 2021 12:27:41 +0000
Message-ID: <17191-8b39f97dabd7d8f9@postgresql.org> (raw)

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



view thread (2+ messages)  latest in thread

Message-ID: <17191-8b39f97dabd7d8f9@postgresql.org>
Permalink:  ../17191-8b39f97dabd7d8f9@postgresql.org/
Also on:    postgresql.org/message-id/17191-8b39f97dabd7d8f9@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, pgsql-bugs@lists.postgresql.org, wlad@0trust.de
  Subject: Re: BUG #17191: Docker / buildx issue - An image created with buildx requires to set custom PGDATA location
  In-Reply-To: <17191-8b39f97dabd7d8f9@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox