gpt4 book ai didi

Docker compose 总是创建一个新卷而不是使用命名的卷

转载 作者:行者123 更新时间:2023-12-02 18:26:09 24 4
gpt4 key购买 nike

我的问题如标题所述。
以下是重现的步骤。注意我使用的是 docker compose 文件版本 3.3,因为我在 docker-compose 的 apt python 版本上运行它,因为 ARM64 没有二进制文件。
使用以下内容创建一个 docker 文件:

version: "3.3"

volumes:
owncloud_data:
external: true
owncloud_mysql:
external: true
owncloud_backup:
external: true
owncloud_redis:
external: true

services:

traefik:
image: "traefik"
container_name: "traefik"
restart: "always"
command:
- "--log.level=DEBUG"
- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.websecure.address=:443"
- "--certificatesresolvers.myresolver.acme.tlschallenge=true"
#- "--certificatesresolvers.myresolver.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory"
# Be sure to have LeGo installed
- "--certificatesresolvers.myresolver.acme.email=<my email>"
- "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
ports:
- "80:80"
- "443:443"
- "8080:8080"
volumes:
- "./letsencrypt:/letsencrypt"
- "/var/run/docker.sock:/var/run/docker.sock:ro"


whoami:
image: "containous/whoami"
container_name: "simple-service"
restart: "always"
labels:
- "traefik.enable=true"
- "traefik.http.routers.whoami.rule=Host(`<my domain>`)"
- "traefik.http.routers.whoami.entrypoints=websecure"
- "traefik.http.routers.whoami.tls.certresolver=myresolver"

owncloud:
image: "owncloud/server"
container_name: "owncloud"
restart: "always"
depends_on:
- db
- redis
environment:
- OWNCLOUD_DOMAIN=owncloud.<my domain>
- OWNCLOUD_DB_TYPE=mysql
- OWNCLOUD_DB_NAME=owncloud
- OWNCLOUD_DB_USERNAME=owncloud
- OWNCLOUD_DB_PASSWORD=owncloud
- OWNCLOUD_DB_HOST=db
- OWNCLOUD_ADMIN_USERNAME=<my username>
- OWNCLOUD_ADMIN_PASSWORD=<my password>
- OWNCLOUD_MYSQL_UTF8MB4=true
- OWNCLOUD_REDIS_ENABLED=true
- OWNCLOUD_REDIS_HOST=redis
healthcheck:
test: ["CMD", "/usr/bin/healthcheck"]
interval: 30s
timeout: 10s
retries: 5
labels:
- "traefik.enable=true"
- "traefik.http.routers.owncloud.rule=Host(`<my domain>`)"
- "traefik.http.routers.owncloud.entrypoints=websecure"
- "traefik.http.routers.owncloud.tls.certresolver=myresolver"
volumes:
- type: volume
source: owncloud_data
target: /owncloud/data

db:
image: webhippie/mariadb:latest
restart: always
environment:
- MARIADB_ROOT_PASSWORD=owncloud
- MARIADB_USERNAME=owncloud
- MARIADB_PASSWORD=owncloud
- MARIADB_DATABASE=owncloud
- MARIADB_MAX_ALLOWED_PACKET=128M
- MARIADB_INNODB_LOG_FILE_SIZE=64M
healthcheck:
test: ["CMD", "/usr/bin/healthcheck"]
interval: 30s
timeout: 10s
retries: 5
volumes:
- type: volume
source: owncloud_mysql
target: /owncloud/mysql
- type: volume
source: owncloud_backup
target: /owncloud/backup

redis:
image: webhippie/redis:latest
restart: "always"
environment:
- REDIS_DATABASES=1
healthcheck:
test: ["CMD", "/usr/bin/healthcheck"]
interval: 30s
timeout: 10s
retries: 5
volumes:
- type: volume
source: owncloud_redis
target: /owncloud/redis
我在启动容器之前运行此命令:
for v in owncloud_data owncloud_mysql owncloud_backup owncloud_redis; do
sudo docker volume create $v
done
然后我运行 sudo docker-compose up .
当我运行 sudo docker volume ls我得到了我之前创建的四个(悬空卷),以及从 docker compose 创建的另外 4 个。这是怎么回事?我明确指定了他们的名字。我不确定为什么 traefik 容器的绑定(bind)有效。
我似乎已经尝试了一切。我把事情放在引号里。我尝试了前面的项目名称。我尝试了简短的语法 owncloud_data:/owncloud/data .我尝试了绑定(bind),但我不想使用绑定(bind),因为备份卷更容易。
谢谢你,洛根

最佳答案

docker-compose正在使用您命名的卷,对于您指定的绑定(bind) .但是,它也会为每个 VOLUME 创建未命名的卷。在您使用的图像上声明并且没有显式绑定(bind)。
我制作了这个小脚本来从您正在使用的图像中提取声明的卷:

images="traefik containous/whoami owncloud/server webhippie/mariadb:latest webhippie/redis:latest"

echo $images | xargs -n1 docker pull

docker inspect $images -f '{{.RepoTags}}, {{.Config.Volumes}}'
结果:
[traefik:latest], map[]
[containous/whoami:latest], map[]
[owncloud/server:latest], map[/mnt/data:{}]
[webhippie/mariadb:latest], map[/var/lib/backup:{} /var/lib/mysql:{}]
[webhippie/redis:latest], map[/var/lib/redis:{}]
您的绑定(bind)都没有绑定(bind)到声明的卷,因此您实际上是在创建新的并将声明的卷保留为空。

关于Docker compose 总是创建一个新卷而不是使用命名的卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63026666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com