简体   繁体   English

Docker-Compose Postgresql导入转储

[英]Docker-Compose Postgresql import dump

I have a question about docker and postgres. 我有关于docker和postgres的问题。 I am setting up a new postgres database every time docker is starting up and want to import a given dump. 每次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: 多克尔 - 撰写:

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. 我希望你能帮助我,我想提前感谢你。

Okay I found the trick, I have to execute "docker-compose rm" in order to execute the scripts and sql files within this folder. 好吧,我找到了诀窍,我必须执行“docker-compose rm”才能执行此文件夹中的脚本和sql文件。 Once built and not removed the init folder is being ignored. 一旦构建并且未被删除,则忽略init文件夹。

UPDATE I ran into this problem again, this time removing the images docker created resolved the problem. 更新我再次遇到这个问题,这次删除图像docker创建解决了问题。

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-entrypoint-initdb.d/*文件的initdb脚本docker-entryfile.sh仅在创建卷时首次运行。

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

2) Remove the volume/volumes ( /Users/yogesh.yadav/DockerData/datahub/postgresql/data ) attached with postgres_service docker service. 2)删除postgres_service docker服务附带的卷/卷(/Users/yogesh.yadav/DockerData/datahub/postgresql/data)。 You can do this manually or via docker-compose rm or docker volume rm . 您可以手动或通过docker-compose rmdocker volume rm执行此操作。 Please identify your volume which are attached to that postgres service before removing it. 在删除之前,请确定附加到该postgres服务的卷。 More info here 更多信息在这里

3) Docker uses very good cache management via building images and running them. 3)Docker通过构建映像并运行它们来使用非常好的缓存管理。 If you did not modify your Dockerfile , docker is going to run the already build image from the cache and run it. 如果您没有修改Dockerfile ,则Dockerfile将从缓存中运行已构建的映像并运行它。 So I would advise to remove the images for postgres_service as well. 所以我建议删除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.shdump.sql将会执行。

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脚本本身没有语法问题(因为我改变了SQL版本,所以我遇到了问题)在我的Dockerfile中)。

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,该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). 请记住,您仍然需要遵循其他答案 - 这些是以前构建的图像并删除您的数据文件夹(如果需要,请备份)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM