Docker PostgreSQL - /docker-entrypoint-initdb.d 中的脚本不运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40386221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:25:57  来源:igfitidea点击:

Docker PostgreSQL - Scripts in /docker-entrypoint-initdb.d doesn't run

postgresqldockerdocker-compose

提问by Rodrigo Saraiva

So, I have a docker-compose project with this structure:

所以,我有一个具有这种结构的 docker-compose 项目:

DockerDev
- docker-compose.yaml
- d-php
  - Dockerfile
  - scripts-apache
- d-postgresql
  - Dockerfile
  - scripts
    - dev_data_setup.sql
- logs
- pgdata
- www

PHP, Redis, ElasticSearch is OK. But Postgresql doesn't run dev_data_setup.sql, with any diferent solutions to /dockes-entrypoint-initdb.d that I found (volume, ADD, COPY, etc). I tried to run and sh script and nothing.

PHP、Redis、ElasticSearch 都可以。但是 Postgresql 不运行 dev_data_setup.sql,我发现 /dockes-entrypoint-initdb.d 有任何不同的解决方案(volume、ADD、COPY 等)。我试图运行和 sh 脚本,但什么也没有。

Could you see this docker-compose and Dockerfile and help me? Thanks

你能看到这个 docker-compose 和 Dockerfile 并帮助我吗?谢谢

Dockerfile:

Dockerfile:

FROM postgres:latest
ADD ./scripts/dev_data_setup.sql /docker-entrypoint-initdb.d

docker-compose.yaml:

docker-compose.yaml:

version: '2'
services:
  php:
    build: ./d-php/
    hostname: www.domain.com
    ports:
      - "80:80"
    volumes:
      - ./www:/var/www/html
      - ./d-php/scripts-apache2/apache2.conf:/etc/apache2/apache2.conf
      - ./d-php/scripts-apache2/web.conf:/etc/apache2/sites-enabled/web.conf
      - ./d-php/scripts-apache2/webservice.conf:/etc/apache2/sites-enabled/webservice.conf
      - ./logs:/var/log/apache2
    links:
      - db
      - redis
      - elasticsearch
  db:
    build: ./d-postgresql/
    volumes:
      - ./pgdata:/pgdata
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - PGDATA=/pgdata
  redis:
    image: redis:latest
  elasticsearch:
    image: elasticsearch:2.4.1

采纳答案by Rodrigo Saraiva

So I found the problem.

所以我发现了问题。

  • First: my sql script was trying to recreate postgres user. then, the dockedev_db exited.
  • Second: I needed to remove all images related to db to docker-compose run the script again.
  • 第一:我的 sql 脚本试图重新创建 postgres 用户。然后,dockedev_db 退出。
  • 第二:我需要删除与 db 相关的所有图像,以便 docker-compose 再次运行脚本。

Thanks for your help.

谢谢你的帮助。

回答by nwinkler

Your problem is caused by the way you use ADDin your Dockerfile:

您的问题是由您ADD在 Dockerfile 中使用的方式引起的:

FROM postgres:latest
ADD ./scripts/dev_data_setup.sql /docker-entrypoint-initdb.d

This creates a file called /docker-entrypoint-initdb.dwith the content of the dev_data_setup.sqlfile. What you want is to treat /docker-entrypoint-initdb.das a directory.

这将创建一个使用文件/docker-entrypoint-initdb.d内容调用的dev_data_setup.sql文件。您想要的是将其/docker-entrypoint-initdb.d视为目录。

You should change your ADDcommand to one of the following:

您应该将ADD命令更改为以下之一:

ADD ./scripts/dev_data_setup.sql /docker-entrypoint-initdb.d/

The trailing slash will treat the destparameter as a directory. Or use

尾部斜杠将dest参数视为目录。或使用

ADD ./scripts/dev_data_setup.sql /docker-entrypoint-initdb.d/dev_data_setup.sql

Which will specifically spell out the file name.

这将专门拼出文件名。

Reference: https://docs.docker.com/engine/reference/builder/#/add

参考:https: //docs.docker.com/engine/reference/builder/#/add

If <dest>does not end with a trailing slash, it will be considered a regular file and the contents of <src>will be written at <dest>.

如果<dest>不以斜杠结尾,则将其视为常规文件,并将其内容<src>写入<dest>.