Docker-Compose Postgresql 导入转储
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37834435/
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
Docker-Compose Postgresql import dump
提问by Simon
I have a question about docker and postgres. I am setting up a new postgres database every time docker is starting up and want to import a given dump.
我有一个关于 docker 和 postgres 的问题。每次 docker 启动并想要导入给定的转储时,我都会设置一个新的 postgres 数据库。
My Problem is like, but the answers are not sufficient for me: Docker postgres does not run init file in docker-entrypoint-initdb.d
我的问题就像,但答案对我来说还不够:Docker postgres 不会在 docker-entrypoint-initdb.d 中运行 init 文件
Docker-Compose:
Docker-撰写:
postgres:
environment:
- POSTGRES_USER=****
- POSTGRES_PASSWORD=****
- POSTGRES_DB=****
build:
context: .
dockerfile: dockerfile-postgres
My Dockerfile: (I tried it already with a script with .sh ending)
我的 Dockerfile:(我已经用 .sh 结尾的脚本试过了)
FROM postgres
ADD dump.sql /docker-entrypoint-initdb.d/
According to https://hub.docker.com/_/postgres/dump.sql must be used to import the database.
根据https://hub.docker.com/_/postgres/dump.sql 必须用于导入数据库。
Starting up the application with docker only gives:
使用 docker 启动应用程序只会提供:
postgres_1 | LOG: invalid record length at 0/1708600
postgres_1 | LOG: redo is not required
postgres_1 | LOG: MultiXact member wraparound protections are now enabled
postgres_1 | LOG: database system is ready to accept connections
postgres_1 | LOG: autovacuum launcher started
Besides I tested if my database has been imported, there is no table in my database. What am I doing wrong (files are read- and executable on target system)? Importing it with psql is no problem, so my dump is correct.
另外我测试了我的数据库是否已导入,我的数据库中没有表。我做错了什么(文件在目标系统上是可读和可执行的)?用 psql 导入没有问题,所以我的转储是正确的。
I hope you can help me and I want to thank you in advance for it.
我希望你能帮助我,我想提前感谢你。
回答by Simon
Okay I found the trick, I have to execute "docker-compose rm" in order to execute the scripts and sql files within this folder. Once built and not removed the init folder is being ignored.
好的,我找到了诀窍,我必须执行“docker-compose rm”才能执行此文件夹中的脚本和 sql 文件。一旦构建并且未删除,init 文件夹将被忽略。
UPDATEI ran into this problem again, this time removing the images docker created resolved the problem.
更新我再次遇到这个问题,这次删除docker创建的图像解决了这个问题。
回答by Yogesh Yadav
It is happening because most probably
它正在发生,因为很可能
The initdb scripts docker-entryfile.sh
which is then responsible for running /docker-entrypoint-initdb.d/*
files in alphabetical order runs for the first time only when the volumes are being created.
docker-entryfile.sh
负责/docker-entrypoint-initdb.d/*
按字母顺序运行文件的 initdb 脚本仅在创建卷时首次运行。
You can see some details here.
您可以在此处查看一些详细信息。
Solution ( works with me ) This is snippet from my docker-compose file
解决方案(与我一起工作)这是我的 docker-compose 文件的片段
postgres_service:
build:
context : docker-postgres
dockerfile: Dockerfile-base
image: 'datahub/postgres:development'
user: postgres
ports:
- "5432:5432"
env_file:
- credentials/postgres/development.env
volumes:
- /Users/yogesh.yadav/DockerData/datahub/postgresql/data:/var/lib/postgresql/data
restart: unless-stopped
networks:
- datahubnetwork
Steps -
脚步 -
1) docker-compose -f docker-compose-filename.yml down
1) docker-compose -f docker-compose-filename.yml down
or
或者
docker-compose -f docker-compose-filename.yml stop postgres_service
docker-compose -f docker-compose-filename.yml stop postgres_service
2) Remove the volume/volumes ( /Users/yogesh.yadav/DockerData/datahub/postgresql/data ) attached with postgres_service docker service. You can do this manually or via docker-compose rm
or docker volume rm
. Please identify your volume which are attached to that postgres service before removing it. More info here
2) 删除 postgres_service docker 服务附带的卷/卷( /Users/yogesh.yadav/DockerData/datahub/postgresql/data )。您可以手动或通过docker-compose rm
或执行此操作docker volume rm
。在删除它之前,请确定附加到该 postgres 服务的卷。更多信息在这里
3) Docker uses very good cache management via building images and running them. If you did not modify your Dockerfile
, docker is going to run the already build image from the cache and run it. So I would advise to remove the images for postgres_service as well.
3) Docker 通过构建镜像并运行它们来使用非常好的缓存管理。如果您没有修改您的Dockerfile
,docker 将从缓存中运行已经构建的镜像并运行它。所以我建议也删除 postgres_service 的图像。
To List images
列出图像
docker images -a
Output -
输出 -
REPOSITORY TAG IMAGE ID CREATED SIZE
datahub/postgres development e7707e670ad4 35 minutes ago 265 MB
Remove that image ( Use -f if required )
删除该图像(如果需要,请使用 -f )
docker rmi IMAGE_ID_HERE
4) Restart your service again
4)再次重启你的服务
docker-compose -f docker-compose-filename.yml up --build postgres_service
This time you can see that your docker-entrypoint.sh
and dump.sql
will execute.
这一次你可以看到你的docker-entrypoint.sh
和dump.sql
将执行。
回答by alexkb
The answers here somewhat solved the problem for me, but the final step to get the docker-entrypoint-initdb/*.sql
scripts working was to ensure there were no syntax issues in the sql scripts themselves (I had issues because I changed SQL versions in my Dockerfile).
这里的答案在某种程度上解决了我的问题,但让docker-entrypoint-initdb/*.sql
脚本工作的最后一步是确保 sql 脚本本身没有语法问题(我遇到了问题,因为我更改了 Dockerfile 中的 SQL 版本)。
If any exist, everything in the docker-entrypoint-initdb/*.sql
scripts seem to get rolled back.
如果存在,docker-entrypoint-initdb/*.sql
脚本中的所有内容似乎都会回滚。
To check if there are any syntax script issues (or other issues) you may find it useful to look at the docker logs:
要检查是否存在任何语法脚本问题(或其他问题),您可能会发现查看 docker 日志很有用:
$ docker ps -all
Note: the -all
ensures it lists those images that may have failed to start up too.
注意:-all
确保它列出了那些可能也无法启动的图像。
Find the image you just created and look for the container id which should be a 12 character hash:
找到您刚刚创建的图像并查找容器 ID,它应该是 12 个字符的哈希值:
$ docker logs baf32ff7ec03
Remember you still need to follow the other answers here - which are to rm the previously built images and to delete your data folder (back it up if you need to).
请记住,您仍然需要遵循此处的其他答案 - 即 rm 以前构建的图像并删除您的数据文件夹(如果需要,请备份它)。