PGAdmin 使用每个 "docker-compose up" 创建新的随机卷

PGAdmin creates new random volume with each "docker-compose up"

每次我运行以下命令:

> docker-compose up -d

...在我有以下 docker-compose.yaml 文件的目录中,我得到一个随机命名的新卷。

docker-compose.yaml:

version: '3.7'

services:

  pgadmin:
    restart: always
    image: dpage/pgadmin4
    container_name: pgadmin_container
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-foobar}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-password}
    ports:
      - "${PGADMIN_PORT:-8080}:80"

命令行输出:

C:\postgres> docker-compose up -d
Creating network "postgres_default" with the default driver
Creating pgadmin_container ... done
C:\postgres> docker volume ls
DRIVER              VOLUME NAME
local               3ee8f970ff477052c6fba9001575a0efa0254deb8dcf73ca97d0422231c74931
C:\postgres> docker-compose down
Stopping pgadmin_container ... done
Removing pgadmin_container ... done
Removing network postgres_default
C:\postgres> docker-compose up -d
Creating network "postgres_default" with the default driver
Creating pgadmin_container ... done
C:\postgres> docker volume ls
DRIVER              VOLUME NAME
local               3ee8f970ff477052c6fba9001575a0efa0254deb8dcf73ca97d0422231c74931
local               705dad9c905eb8f1679a9ee4ff290363c40f5285b8048204cab44bce26916845
C:\postgres>

您会看到在第一个 "up" 之后有一个名称为 64 个字符的卷,在第二次调用 "docker-compose up" 之后有两个。这种模式仍在继续。

是什么导致了随机命名的卷?我如何防止它们的创建强制系统重新使用它们?

我实际上已经编辑了我的 docker-compose.yaml 文件以达到最低限度以重现问题。实际上,还有一个使用相同文件启动的 Postgres 数据库。

如果你用docker inspect dpage/pgadmin4看看,你会看到下一个:

"Volumes": {
    "/var/lib/pgadmin": {}
}, 

这意味着在它的 Dockerfile 中,它定义了一个 Anonymous volumes 如下:

VOLUME ["/var/lib/pgadmin"]

以上将在您每次 up/down 服务时更改卷名。要使其不发生变化,您可以使用 Named volumes 覆盖它,如下所示:

version: '3.7'

services:
  pgadmin:
    restart: always
    image: dpage/pgadmin4
    container_name: pgadmin_container
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-foobar}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-password}
    ports:
      - "${PGADMIN_PORT:-8080}:80"
    volumes:
      - my-data:/var/lib/pgadmin

volumes:
    my-data:

您可以参考this & this了解更多信息。

根据 docs 映像在以下位置创建了一个卷:

/var/lib/pgadmin

This is the working directory in which pgAdmin stores session data, user files, configuration files, and it's configuration database. Mapping this directory onto the host machine gives you an easy way to maintain configuration between invocations of the container.

一个docker inspect container_id显示:

"Mounts": [
    {
        "Type": "volume",
        "Name": "70c8ae8e1de3e5d6c71fa9c63930cc75761132f4fdb75a2982b32454313b78c6",
        "Source": "/var/lib/docker/volumes/70c8ae8e1de3e5d6c71fa9c63930cc75761132f4fdb75a2982b32454313b78c6/_data",
        "Destination": "/var/lib/pgadmin",
        "Driver": "local",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    }
],

所以好像每次都会创建一个anonymos volume 为了重用该卷,请使用 docker-compose start/stop 而不是 docker-compose up/down。讨论了类似问题 here