在映像构建时在PostgreSQL 11 Docker中恢复SQL转储

时间:2019-07-08 11:11:07

标签: postgresql docker postgresql-11

我想构建一个自定义的Postgres11映像,在其中创建一些用户并安装一些扩展。因为我希望在构建时创建它们,所以我不想使用docker-entrypoint-initdb.d。下一步将是也还原sql转储。

FROM postgres:11

ENV PG_MAJOR 11
ENV POSTGISV 2.5
ENV TZ Europe/Brussels

RUN apt-get update \
  && apt-get install -y --no-install-recommends \
  postgresql-$PG_MAJOR-postgis-$POSTGISV \
  postgresql-$PG_MAJOR-postgis-$POSTGISV-scripts

USER postgres

RUN  initdb && pg_ctl  -o "-c listen_addresses='*'" start &&\
    psql -h 0.0.0.0 --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
    psql -h 0.0.0.0 --command "CREATE USER akela_test WITH PASSWORD 'akela';" &&\
    createdb -E UTF8 -U postgres -h 0.0.0.0 -O akela_test akela_test --template template0 &&\
    psql -U postgres -d akela_test -h 0.0.0.0 -c 'CREATE EXTENSION IF NOT EXISTS "hstore";' &&\
    psql -U postgres -d akela_test -h 0.0.0.0 -c 'CREATE EXTENSION IF NOT EXISTS "postgis";' &&\
    psql -U postgres -d akela_test -h 0.0.0.0 -c 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp";' &&\
    psql -U postgres -d akela_test -h 0.0.0.0 -c "CREATE ROLE akela_db WITH LOGIN PASSWORD 'akela'" &&\
    psql -U postgres -d akela_test -h 0.0.0.0 -c "GRANT ALL PRIVILEGES ON DATABASE akela_test to akela_db" &&\
    psql -U postgres -d akela_test -h 0.0.0.0 -c "CREATE schema db" &&\
    pg_ctl stop
    # gunzip -c /tmp/dump.sql.gz | psql -U akela -h 0.0.0.0 akela
USER root

似乎可以工作:

...
CREATE SCHEMA
ALTER SCHEMA
CREATE ROLE
GRANT
CREATE SCHEMA
ALTER SCHEMA
waiting for server to shut down....2019-07-08 12:58:06.962 CEST [22] LOG:  received fast shutdown request
2019-07-08 12:58:06.964 CEST [22] LOG:  aborting any active transactions
2019-07-08 12:58:06.965 CEST [22] LOG:  background worker "logical replication launcher" (PID 29) exited with exit code 1
2019-07-08 12:58:06.965 CEST [24] LOG:  shutting down
2019-07-08 12:58:07.006 CEST [22] LOG:  database system is shut down
 done
server stopped
...

但是,运行该映像时既没有显示用户也没有数据库:

postgres=# \l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
-----------+----------+----------+------------+------------+-----------------------
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
(3 rows)

postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

可能是什么问题?

1 个答案:

答案 0 :(得分:1)

postgres defines a volume的Dockerfile,这意味着通过RUN步骤对该目录所做的任何更改都将被丢弃。要更改此目录,您需要执行以下操作之一:

  1. 在运行时进行更改而不是进行构建,然后保存生成的卷。
  2. 在构建过程中进行更改,但在其他目录中。这将需要更改postgres配置以使用其他目录。
  3. 将更改保存到其他目录,然后在启动容器时恢复这些更改(有关示例,请参见save and load volume scripts)。
  4. 构建没有体积定义的自己的postgres图像。