简体   繁体   English

Docker postgres 不在 docker-entrypoint-initdb.d 中运行 init 文件

[英]Docker postgres does not run init file in docker-entrypoint-initdb.d

Based on Docker's Postgres documentation , I can create any *.sql file inside /docker-entrypoint-initdb.d and have it automatically run.根据 Docker 的 Postgres文档,我可以在/docker-entrypoint-initdb.d中创建任何*.sql文件并让它自动运行。

I have init.sql that contains CREATE DATABASE ronda;我有包含CREATE DATABASE ronda; init.sql

In my docker-compose.yaml , I have在我docker-compose.yaml中,我有

web:
  restart: always
  build: ./web
  expose:
    - "8000"
  links:
    - postgres:postgres
  volumes:
    - /usr/src/app/static
  env_file: .env
  command: /usr/local/bin/gunicorn ronda.wsgi:application -w 2 -b :8000

nginx:
  restart: always
  build: ./nginx/
  ports:
    - "80:80"
  volumes:
    - /www/static
  volumes_from:
    - web
  links:
    - web:web

postgres:
  restart: always
  build: ./postgres/
  volumes_from:
    - data
  ports:
    - "5432:5432"

data:
  restart: always
  build: ./postgres/
  volumes:
    - /var/lib/postgresql
  command: "true"

and my postgres Dockerfile,和我的 postgres Dockerfile,

FROM library/postgres

RUN mkdir -p /docker-entrypoint-initdb.d
COPY init.sql /docker-entrypoint-initdb.d/

Running docker-compose build and docker-compose up work fine, but the database ronda is not created.运行docker-compose builddocker-compose up工作正常,但未创建数据库ronda

If your initialisation requirements are just to create the ronda schema, then you could just make use of the POSTGRES_DB environment variable as described in the documentation .如果您的初始化要求只是创建ronda模式,那么您可以按照文档中所述使用POSTGRES_DB环境变量。

The bit of your docker-compose.yml file for the postgres service would then be: postgres 服务的 docker-compose.yml文件的一部分将是:

postgres:
  restart: always
  build: ./postgres/
  volumes_from:
    - data
  ports:
    - "5432:5432"
  environment:
    POSTGRES_DB: ronda

On a side note, do not use restart: always for your data container as this container does not run any service (just the true command).附带一提,不要对你的data容器使用restart: always因为这个容器不运行任何服务(只是true的命令)。 Doing this you are basically telling Docker to run the true command in an infinite loop.这样做基本上是告诉 Docker 在无限循环中运行true命令。

This is how I use postgres on my projects and preload the database.这就是我在我的项目中使用postgres并预加载数据库的方式。

file: docker-compose.yml文件: docker-compose.yml

  db:
    container_name: db_service
    build:
      context: .
      dockerfile: ./Dockerfile.postgres
    ports:
      - "5432:5432"
    volumes:
      - /var/lib/postgresql/data/

This Dockerfile load the file named pg_dump.backup (binary dump) or psql_dump.sql (plain text dump) if exist on root folder of the project.如果项目的根文件夹中存在名为pg_dump.backup (二进制转储)或psql_dump.sql (纯文本转储)的文件,则此Dockerfile加载该文件。

file: Dockerfile.postgres文件: Dockerfile.postgres

FROM postgres:9.6-alpine

ENV POSTGRES_DB DatabaseName

COPY pg_dump.backup .
COPY pg_dump.sql .

RUN [[ -e "pg_dump.backup" ]] && pg_restore pg_dump.backup > pg_dump.sql

# Preload database on init
RUN [[ -e "pg_dump.sql" ]] && cp pg_dump.sql /docker-entrypoint-initdb.d/

In case of need retry the loading of the dump, you can remove the current database with the command:如果需要重试转储的加载,您可以使用以下命令删除当前数据库:

docker-compose rm db

Then you can run docker-compose up to retry load the database.然后你可以运行docker-compose up来重试加载数据库。

Had the same problem with postgres 11.与 postgres 11 有同样的问题。

Some points that helped me:对我有帮助的一些要点:

  • run:跑步:
    • docker-compose rm
    • docker-compose build
    • docker-compose up
  • The obvious: don't run compose in detached mode.显而易见:不要在分离模式下运行 compose。 You want to see the logs.您想查看日志。

After adding the step docker-compose rm to the mix it worked, finally.在将步骤docker-compose rm添加到混合后,它终于起作用了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker-Compose + Postgres:/docker-entrypoint-initdb.d/init.sql:权限被拒绝 - Docker-Compose + Postgres: /docker-entrypoint-initdb.d/init.sql: Permission denied 初始化postgres数据库(不使用/docker-entrypoint-initdb.d) - Initialize postgres database (without using /docker-entrypoint-initdb.d) 使用 /docker-entrypoint-initdb.d 初始化 postgres docker 时不会创建表 - Tables are not created when initialize postgres docker with /docker-entrypoint-initdb.d docker alpine postgres在撰写中不执行docker-entrypoint-initdb.d脚本 - docker alpine postgres in compose not executing docker-entrypoint-initdb.d scripts docker-compose postgres 在 docker-entrypoint-initdb.d 中运行脚本后重新启动 - docker-compose postgres restart after running scripts in docker-entrypoint-initdb.d Docker PostgreSQL - /docker-entrypoint-initdb.d中的脚本无法运行 - Docker PostgreSQL - Scripts in /docker-entrypoint-initdb.d doesn't run docker-entrypoint-initdb.d/* 文件在 docker-compose 首次运行时被忽略 - docker-entrypoint-initdb.d/* files ignored on docker-compose first run 为什么 postgres 容器在 Gitlab CI 中忽略 /docker-entrypoint-initdb.d/* - Why is postgres container ignoring /docker-entrypoint-initdb.d/* in Gitlab CI 在以前工作的配置中忽略 Postgres 数据库的 /docker-entrypoint-initdb.d 错误 - ignoring /docker-entrypoint-initdb.d error for Postgres database in a configuration that previously worked flyway 无法连接到 docker-entrypoint-initdb.d 脚本中的 postgres 容器 - flyway unable to connect to postgres container within docker-entrypoint-initdb.d script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM