Description
I'm trying to build a lightweight docker container that starts as quickly as possible with all the data built-in. The goal is to form a scalable kubernetes service that provides access to some immutable data.
Looks like the solution is to use a multi-stage build that was introduced by Docker recently. The data dump is being read from a backup inside an intermediate container and then all the PGDATA
directories are moved to the actual container. The resulting image does not have docker-entrypoint-initdb.d
, which makes it two times smaller and also speeds up the start, because all the required folders are already in place.
Here is my Dockerfile:
FROM postgres:9.6-alpine AS data-donor
COPY /path/to/my/db/dump/on/host/ /docker-entrypoint-initdb.d/
ENV PGDATA=/pgdata
RUN docker-entrypoint.sh --help
FROM postgres:9.6-alpine
ENV PGDATA=/pgdata
COPY --from=data-donor /pgdata /pgdata
-
I'm adding
--help
toRUN docker-entrypoint.sh --help
in the first stage just to trick the script – it otherwise does not start at all or launches a foreground process, which never exits. -
PGDATA
can't be a equal to its default value (/var/lib/postgresql/data
), because that's aVOLUME
and so things don't copy between the build stages.
The resulting lightweight image does start and does serve the data, but fails after some time. The symptoms are similar to what's described here: https://forums.docker.com/t/beta-9-postgres-stat-files-corrupted-when-data-stored-on-host-mapped-volume/10819
I tried fixing things by chown -R postgres:postgres /pgdata
and chomd -R 777 /pgdata
after everything else, but this did not help. The logs are still full of messages like these
2017-07-26T07:56:04.004328315Z LOG: using stale statistics instead of current ones because stats collector is not responding
2017-07-26T07:56:04.00448246Z WARNING: could not open statistics file "pg_stat_tmp/global.stat": Permission denied
2017-07-26T07:56:07.24726432Z LOG: incomplete startup packet
2017-07-26T07:56:12.247888609Z LOG: incomplete startup packet
2017-07-26T07:56:17.247440723Z LOG: incomplete startup packet
2017-07-26T07:56:17.248236006Z LOG: incomplete startup packet
2017-07-26T07:56:22.24818477Z LOG: incomplete startup packet
2017-07-26T07:56:27.247806065Z LOG: incomplete startup packet
2017-07-26T07:56:27.891670873Z WARNING: could not create relation-cache initialization file "global/pg_internal.init.9282": Permission denied
2017-07-26T07:56:27.89173002Z DETAIL: Continuing anyway, but there's something wrong.
2017-07-26T07:56:27.892495964Z WARNING: could not create relation-cache initialization file "base/12404/pg_internal.init.9282": Permission denied
2017-07-26T07:56:27.892530176Z DETAIL: Continuing anyway, but there's something wrong.
2017-07-26T07:56:27.893971225Z LOG: could not open temporary statistics file "pg_stat_tmp/global.tmp": Permission denied
2017-07-26T07:56:27.894134143Z LOG: could not open temporary statistics file "pg_stat_tmp/global.tmp": Permission denied
2017-07-26T07:56:27.894337278Z LOG: could not open temporary statistics file "pg_stat_tmp/global.tmp": Permission denied
2017-07-26T07:56:27.894752415Z LOG: could not open temporary statistics file "pg_stat_tmp/global.tmp": Permission denied
2017-07-26T07:56:27.925004824Z LOG: could not open temporary statistics file "pg_stat_tmp/global.tmp": Permission denied
2017-07-26T07:56:27.925026913Z LOG: could not open temporary statistics file "pg_stat_tmp/global.tmp": Permission denied
2017-07-26T07:56:27.925224732Z LOG: could not open temporary statistics file "pg_stat_tmp/global.tmp": Permission denied
2017-07-26T07:56:27.925444104Z LOG: could not open temporary statistics file "pg_stat_tmp/global.tmp": Permission denied
2017-07-26T07:56:27.925568544Z LOG: could not open temporary statistics file "pg_stat_tmp/global.tmp": Permission denied
2017-07-26T07:56:27.926003844Z LOG: could not open temporary statistics file "pg_stat_tmp/global.tmp": Permission denied
2017-07-26T07:56:27.926257893Z LOG: could not open temporary statistics file "pg_stat_tmp/global.tmp": Permission denied
2017-07-26T07:56:27.926475139Z LOG: could not open temporary statistics file "pg_stat_tmp/global.tmp": Permission denied
2017-07-26T07:56:32.247021854Z LOG: incomplete startup packet
2017-07-26T07:56:37.251664911Z LOG: incomplete startup packet
2017-07-26T07:56:37.253645873Z LOG: incomplete startup packet
2017-07-26T07:56:42.247354375Z LOG: incomplete startup packet
What could this odd behaviour caused by?