postgresql 如何使用卷将数据保存在 dockerized postgres 数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41637505/
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
How to persist data in a dockerized postgres database using volumes
提问by Alex Lenail
My docker compose file has three containers, web, nginx, and postgres. Postgres looks like this:
我的 docker compose 文件有三个容器,web、nginx 和 postgres。Postgres 看起来像这样:
postgres:
container_name: postgres
restart: always
image: postgres:latest
volumes:
- ./database:/var/lib/postgresql
ports:
- "5432:5432
My goal is to mount a volume which corresponds to a local folder called ./database
inside the postgres container as /var/lib/postgres
. When I start these containers and insert data into postgres, I verify that /var/lib/postgres/data/base/
is full of the data I'm adding (in the postgres container), but in my local system, ./database
only gets a data
folder in it, i.e. ./database/data
is created, but it's empty. Why?
我的目标是安装一个卷,该卷对应于./database
在 postgres 容器内称为/var/lib/postgres
. 当我启动这些容器并将数据插入 postgres 时,我验证/var/lib/postgres/data/base/
我添加的数据是否已满(在 postgres 容器中),但在我的本地系统中,./database
只data
在其中获取一个文件夹,即已./database/data
创建,但它是空的. 为什么?
Notes:
笔记:
UPDATE 1
更新 1
Per Nick's suggestion, I did a docker inspect
and found:
根据尼克的建议,我做了一个docker inspect
,发现:
"Mounts": [
{
"Source": "/Users/alex/Documents/MyApp/database",
"Destination": "/var/lib/postgresql",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
},
{
"Name": "e5bf22471215db058127109053e72e0a423d97b05a2afb4824b411322efd2c35",
"Source": "/var/lib/docker/volumes/e5bf22471215db058127109053e72e0a423d97b05a2afb4824b411322efd2c35/_data",
"Destination": "/var/lib/postgresql/data",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
Which makes it seem like the data is being stolen by another volume I didn't code myself. Not sure why that is. Is the postgres image creating that volume for me? If so, is there some way to use that volumeinstead of the volume I'm mounting when I restart? Otherwise, is there a good way of disabling that other volume and using my own, ./database
?
这使得数据看起来像是被另一个我没有编码的卷窃取了。不知道为什么会这样。postgres 图像是否为我创建了该卷?如果是这样,是否有某种方法可以使用该卷而不是我重新启动时正在安装的卷?否则,是否有禁用其他卷并使用我自己的卷的好方法./database
?
UPDATE 2
更新 2
I found the solution, thanks to Nick! (and another friend) Answer below.
我找到了解决方案,感谢尼克!(和另一个朋友)在下面回答。
回答by Alex Lenail
Strangely enough, the solution ended up being to change
奇怪的是,解决方案最终是改变
volumes:
- ./postgres-data:/var/lib/postgresql
to
到
volumes:
- ./postgres-data:/var/lib/postgresql/data
回答by Nishchit Dhanani
You can create a common volumefor all Postgres data
您可以为所有 Postgres 数据创建一个公共卷
docker volume create pgdata
or you can set it to the compose file
或者您可以将其设置为撰写文件
version: "3"
services:
db:
image: postgres
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgress
- POSTGRES_DB=postgres
ports:
- "5433:5432"
volumes:
- pgdata:/var/lib/postgresql/data
networks:
- suruse
volumes:
pgdata:
It will create volume name pgdataand mount this volume to container's path.
它将创建卷名pgdata并将此卷挂载到容器的路径。
You can inspect this volume
您可以检查此卷
docker volume inspect pgdata
// output will be
[
{
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/pgdata/_data",
"Name": "pgdata",
"Options": {},
"Scope": "local"
}
]
回答by Nick Burke
I would avoid using a relative path. Remember that docker is a daemon/client relationship.
我会避免使用相对路径。请记住,docker 是一种守护进程/客户端关系。
When you are executing the compose, it's essentially just breaking down into various docker client commands, which are then passed to the daemon. That ./database
is then relative to the daemon, not the client.
当您执行 compose 时,它实际上只是分解为各种 docker 客户端命令,然后将这些命令传递给守护程序。那./database
是相对于daemon,而不是客户端。
Now, the docker dev team has some back and forth on this issue, but the bottom line is it can have some unexpected results.
现在,docker 开发团队在这个问题上有一些来回,但最重要的是它可能会产生一些意想不到的结果。
In short, don't use a relative path, use an absolute path.
总之,不要使用相对路径,使用绝对路径。
回答by Joel B
I think you just need to create your volume outside docker first with a docker create -v /location --name
and then reuse it.
我认为您只需要首先使用 a 在 docker 之外创建您的卷docker create -v /location --name
,然后重用它。
And by the time I used to use docker a lot, it wasn't possible to use a static docker volume with dockerfile definition so my suggestion is to try the command line (eventually with a script ) .
当我过去经常使用 docker 时,不可能使用带有 dockerfile 定义的静态 docker 卷,所以我的建议是尝试命令行(最终使用脚本)。