postgresql 如何在 docker compose 中为 postgres 使用音量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42107364/
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-09-09 06:22:47  来源:igfitidea点击:

How to use volume in docker compose for postgres?

postgresqldockerdocker-compose

提问by Kevin

Here is the imageI am using.

这是我正在使用的图像

I named it posgres_test

我给它起了名字 posgres_test

If I run the image individually

如果我单独运行图像

docker run -i -t -v="test_volume:/var/lib/postgresql" -p 5432:5432 posgres_test

I can access it with

我可以访问它

psql -h 192.168.99.100 -p 5432 -U pguser -W pgdb

Or I can access it with my golang app

或者我可以使用我的 golang 应用程序访问它

// host is set to postgres
db, err := sql.Open("postgres", "postgres://pguser:pguser@postgres:5432/pgdb")
// table test_db is manually created.
rows, err := db.Query("SELECT name FROM test_db WHERE)

However if I use docker compose

但是,如果我使用 docker compose

docker-compose.yml

docker-compose.yml

version: "2"
services:
  postgres:
    image: my_image/postgresql:9.3
    volumes:
      - test_volume:/var/lib/postgresql
    ports:
      - "5432:5432"
  web:
    image: my-golang-app4
    ports:
      - "8080:8080"
volumes:
  test_volume: {}

I get the following

我得到以下

pguser@pgdb ERROR:  relation "test_db" does not exist at character 15

I know for sure test_dbexist in test_volumesince

我知道肯定test_db存在的test_volume,因为

docker run -i -t -v="test_volume:/var/lib/postgresql" -p 5432:5432 posgres_test
psql -h 192.168.99.100 -p 5432 -U pguser -W pgdb

\dt

will show the table I created

将显示我创建的表

But it seems like my app in docker compose cannot find it

但似乎我在 docker compose 中的应用找不到它

Can someone help me out?

有人可以帮我吗?

回答by Arkowsky

About your docker-compose file

关于你的 docker-compose 文件

  1. Firstly I thought it's because you don't use 'links' option to link your postgres container to web container - it's good practice if you don't expand ports - but you expand postgres port.

  2. If you want use inheritance from the image you posted Instead of using this line:

  1. 首先,我认为这是因为您没有使用“链接”选项将您的 postgres 容器链接到 Web 容器 - 如果您不扩展端口,这是一种很好的做法 - 但您扩展了 postgres 端口。

  2. 如果您想使用您发布的图像的继承而不是使用此行:

my_image/postgresql:9.3

my_image/postgresql:9.3

use:

用:

docker/postgres

and create path docker/postgres and there place Dockerfile with inharitance from container you want.

并创建路径 docker/postgres 并在那里放置 Dockerfile 与您想要的容器的 inharitance。

  1. I always use sharing volumes in docker-compose.yml like this:

    .:/var/www/html

  1. 我总是像这样在 docker-compose.yml 中使用共享卷:

    .:/var/www/html

where .is my project path where I place my code files.

这里.是我把我的代码文件我的项目路径。

Image I created to test this case

我创建的图像来测试这个案例

I don't have your all docker files structure to reproduce this error and fix it, so I created docker-compose which should match your needs or help to fix your issue:

我没有你所有的 docker 文件结构来重现这个错误并修复它,所以我创建了 docker-compose 它应该符合你的需求或帮助解决你的问题:

version: '2'
services:
  web:
    build: docker/web
    ports:
      - "8080:8080"
    links:
      - dbpostgres 
    volumes:
      - .:/var/www/html   # I will share my code so I map this path
  dbpostgres:
    image: postgres
    volumes:
      - /private/var/lib/postgresql:/var/lib/postgresql
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: pguser
      POSTGRES_PASSWORD: pguser
      POSTGRES_DB: pgdb

Notes:

笔记:

  1. I will recommend use official postgres image

  2. I left comments next to the lines.

  1. 我会推荐使用官方 postgres 图像

  2. 我在行旁边留下了评论。

How I made connection:

我是如何建立联系的:

host=dbpostgres port=5432 dbname=pgdb user=pguser password=pguser

Because my web container know host dbpostgres(image name and domain name) now - I link them using links.

因为我的 Web 容器现在知道主机 dbpostgres(图像名称和域名) - 我使用链接链接它们。

If you need database from existing container

如果您需要现有容器中的数据库

If you need database from your existing container just use option docker cp to copy database localy:

如果您需要现有容器中的数据库,只需使用选项 docker cp 来复制数据库本地:

docker cp posgres_test:/var/lib/postgresql /private/var/lib/postgresql

where /private/var/lib/postgresqlis path on your localhost. You need also change credentials to db in docker-compose to your old credentials. You have to do it before run docker-compose because if db doesn't exist, will be crated.

/private/var/lib/postgresql本地主机上的路径在哪里。您还需要将 docker-compose 中的 db 凭据更改为旧凭据。您必须在运行 docker-compose 之前执行此操作,因为如果 db 不存在,则将被打包。

Any questions, let me know.

有任何问题,请告诉我。

回答by gprivitera

If the volume is external and already existing before the use of docker-compose you should declare it external, or else docker compose will create a new volume with the project name as prefix.

如果卷是外部的并且在使用 docker-compose 之前已经存在,您应该将其声明为外部,否则 docker compose 将创建一个以项目名称作为前缀的新卷。

volumes:
  test_volume:
   external: true

Source: https://docs.docker.com/compose/compose-file/#external

来源:https: //docs.docker.com/compose/compose-file/#external

回答by Thibault Loison

I think it sould be something like this for you.

我想对你来说应该是这样的。

docker run -itd -p 5432:5432 --name postgres_test -v /path/in/your/host :/path/in/your/container postgres_test psql -h 192.168.99.100 -p 5432 -U pguser -W pgdb

Read Docker docs(https://docs.docker.com/engine/tutorials/dockervolumes/), watch tutorials (there is a Docker Youtube channel whith great tutorials) and read other topics before posting a new one...

阅读 Docker 文档(https://docs.docker.com/engine/tutorials/dockervolumes/),观看教程(有一个 Docker Youtube 频道,里面有很棒的教程)并在发布新主题之前阅读其他主题......