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

2024-04-23

我需要使用一些 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"]

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

CREATE DATABASE db_name;

最后,这是 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

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

如果您删除db_data卷并启动容器,postgres 将看到没有数据库,然后它将使用以下脚本为您初始化一个数据库docker-entrypoint-initdb.d.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐