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