Skip to content

Instantly share code, notes, and snippets.

@wlad
Last active September 13, 2021 16:29
Show Gist options
  • Save wlad/db6e99e46c5da8751d50d454479db42c to your computer and use it in GitHub Desktop.
Save wlad/db6e99e46c5da8751d50d454479db42c to your computer and use it in GitHub Desktop.
PostgreSQL Docker / buildx issue - An image created with buildx (Moby BuildKit) requires to set custom PGDATA location

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)

# 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
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.

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
COPY scripts/db-setup.sql /docker-entrypoint-initdb.d/

# ALLOW CONNECTIONS FROM ALL ADRESSES & LISTEN TO ALL INTERFACES
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment