Q-Logo 我的学习笔记分享

Windows上PostgreSQL Docker容器数据持久化问题

在PostGresQL官方镜像的说明页面 https://hub.docker.com/_/postgres ,对存储数据是这样描述的

Where to Store Data

Important note: There are several ways to store data used by applications that run in Docker containers. We encourage users of the postgres images to familiarize themselves with the options available, including:

  • Let Docker manage the storage of your database data by writing the database files to disk on the host system using its own internal volume management. This is the default and is easy and fairly transparent to the user. The downside is that the files may be hard to locate for tools and applications that run directly on the host system, i.e. outside containers.
  • Create a data directory on the host system (outside the container) and mount this to a directory visible from inside the container. This places the database files in a known location on the host system, and makes it easy for tools and applications on the host system to access the files. The downside is that the user needs to make sure that the directory exists, and that e.g. directory permissions and other security mechanisms on the host system are set up correctly.

The Docker documentation is a good starting point for understanding the different storage options and variations, and there are multiple blogs and forum postings that discuss and give advice in this area. We will simply show the basic procedure here for the latter option above:

  1. Create a data directory on a suitable volume on your host system, e.g. /my/own/datadir.
  2. Start your postgres container like this:

$ docker run --name some-postgres -v /my/own/datadir:/var/lib/postgresql/data -e POSTGRES_PASSWORD=mysecretpassword -d postgres:tag

The -v /my/own/datadir:/var/lib/postgresql/data part of the command mounts the /my/own/datadir directory from the underlying host system as /var/lib/postgresql/data inside the container, where PostgreSQL by default will write its data files.

第一种是将数据直接存储在Docker容器里,这样最简单,几乎不需要什么专门的配置,在测试时很方便,但这样的话以后想要将数据导出到容器外面就不太方便。

第二种是在宿主机上创建一个目录,然后通过挂载卷的方式将此目录挂载到容器内的一个目录上,这样做的好处是数据库文件会保存在宿主机上的指定目录中,数据与容器服务相分离,好处多多。一般情况下可以直接将宿主机上的目录直接挂载到PostgreSQL容器的默认数据存储目录(即/var/lib/postgresql/data目录)。这种方式在Linux机器上是没有问题的,但在Windows机器上,会报错。原因与Windows目录到Linux目录之间的文件系统语义映射有关,到目前为止这仍是一个在Window上运行Docker时的已知限制和问题( https://github.com/docker/for-win/issues/445 )。因此,只能试图去绕过啦。参考 SuperSandro2000的回复 https://github.com/docker/for-win/issues/39#issuecomment-580903715

What you are trying to achieve: Running a native linux database with a native linux container platform in a native environment emulated in a vm and then mount that thing non natively on a totally different file system which does not support features the native linux programs expect to be there and the tools just fake to make it somehow work.
I am sorry but you are just trying to do a thing which is not possible right now and instead of searching for a working solution or contributing one you are whining here that it does not work.
What you can do:
use linux
use a linux vm
don't mount that thing at all and manage it solely with postgres tools and backup a dump
use a db over the network on a linux server.
use wsl in the native file directory area
use posgres for windows
use posgtres for windows in a native container docker-library/postgres#324