如何在 docker-compose 命名卷中使用现有目录 ~/.ssh?

How to use existing directory ~/.ssh in docker-compose named volumes?

我想将我的 ~/.ssh 映射到 docker 容器 .ssh。我有 docker 组成

version: '3'

volumes:
  ssh-data:
    driver: local
    driver_opts:
      type: none
      device: ~/.ssh/
      o: bind

services:
  mysql:
    container_name: mysql
    image: chagridsada/galera-mariadb
    ports:
      - "3306:3306"
    volumes:
      - "ssh-data:/app/.ssh/"
    environment:
      CLUSTER_NAME: galera-db-cluster
      CLUSTER_JOIN:
      MYSQL_ROOT_PASSWORD: dev
      XTRABACKUP_PASSWORD: dev

但是当我尝试 运行 容器时,它给出了错误。

$ docker-compose up -d

Creating network "training_default" with the default driver
Creating volume "training_ssh-data" with local driver
Creating mysql ... error

ERROR: for mysql  Cannot start service mysql: error while mounting volume '/var/lib/docker/volumes/training_ssh-data/_data': failed to mount local volume: mount ~/.ssh/:/var/lib/docker/volumes/training_ssh-data/_data, flags: 0x1000: no such file or directory

ERROR: for mysql  Cannot start service mysql: error while mounting volume '/var/lib/docker/volumes/training_ssh-data/_data': failed to mount local volume: mount ~/.ssh/:/var/lib/docker/volumes/training_ssh-data/_data, flags: 0x1000: no such file or directory
ERROR: Encountered errors while bringing up the project.

它试图与容器中的 /var/lib/docker/volumes/training_ssh-data/_data 映射,但在 docker-compose.yml 中它映射到 /app/.ssh/

如果我将映射更改为 ~/.ssh:/app/.ssh,那么它将正常工作,但我也必须为其他容器使用 ssh-data 卷,因此我必须创建 named volume

为什么它试图与 /var/lib/docker/volumes/training_ssh-data/_data 映射?

我认为 (!?) 问题是您在 Docker Compose 中使用了相对 (~/.ssh) 引用,您应该使用绝对引用

如果我没记错的话,您可以在 Docker Compose 文件中使用环境变量,因此可以在 Linux 上将 ~/.ssh 替换为 ${HOME}/.ssh