/docker-entrypoint-initdb.d 文件夹中的脚本将被忽略

Lui*_*igi 6 postgresql docker dockerfile docker-compose

我需要使用一些 SQL 命令配置 Postogres,但我放入 /docker-entrypoint-initdb.d 文件夹中的所有内容都不会被执行。我正在使用 postgres:9.6 图像。

我的 Dockerfile 如下:

FROM postgres:9.6

COPY init.sql /docker-entrypoint-initdb.d/
USER root
RUN chown postgres:postgres /docker-entrypoint-initdb.d/init.sql
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["postgres"]
Run Code Online (Sandbox Code Playgroud)

我在 init.sql 中尝试了多个命令,例如:

CREATE DATABASE db_name;
Run Code Online (Sandbox Code Playgroud)

最后,这是 yaml 文件中与数据库相关的部分。

db:
    image: postgres-am
    ports:
      - target: 5432
        published: 5432
        protocol: tcp
        mode: host

    environment:
      POSTGRES_PASSWORD: "postgres"
      PGDATA: "/var/lib/postgresql/data/pgdata"

    volumes:
      - db_data:/var/lib/postgresql/data
Run Code Online (Sandbox Code Playgroud)

Han*_*ian 12

Postgres 仅在容器启动时未找到数据库时才初始化数据库。由于您在数据库目录上有卷映射,因此数据库很可能已经存在。

如果您删除该db_data卷并启动容器,postgres 将发现不存在数据库,然后它将使用 中的脚本为您初始化一个数据库docker-entrypoint-initdb.d