docker容器使用ssh密钥将卷连接到远程服务器

时间:2020-02-04 20:06:06

标签: docker ssh docker-volume sshfs

我最近一直在使用Docker,而我是Docker的新手,所以我设法为nginx / php和mysql设置了容器。

我拥有网站的资产,这些资产位于单独的服务器上,并且已使用sshfs连接到该网站,并且能够在容器文件夹和远程文件夹之间传输文件。 但这是使用密码完成的,现在我需要使用ssh键进行连接。

下面是docker-compose文件的示例


version: '3.5'

services:
  php-fpm:
    build:
      context: ./images/fpm
    working_dir: /var/www/html
    volumes:
      - ../<code_base>/:/var/www/html
      - sshfs_test:/var/www/html/public/assets

volumes:
  sshfs_test:
    name: "sshfs_test"
    driver: vieux/sshfs:latest
    driver_opts:
      sshcmd: "<server_user>@<server_ip>:/path/to/assets"
      password: "<my_password>"
      allow_other: ""

以上示例有效,但我需要使用ssh私钥而不是密码进行连接。

1 个答案:

答案 0 :(得分:0)

看:https://github.com/vieux/docker-volume-sshfs/issues/19#issuecomment-748609520

终于在我的系统上找到了正确的方法(一个 rpi 作为服务器,一个 mac 托管远程卷),多亏了这个不必共享我的密码。

  1. 使用 make all 安装 vieux/sshfs(正如@ogmundur 在所有机器上推荐的那样,对我来说:rpi(我的服务器所在的位置)和 mac(共享我的卷)

  2. 确保已经完成了根密钥基因(对我而言:在 Pi 中),并确定 rsa 所在的文件夹:/root/.ssh/,确保已共享公钥(对我而言: mac),否则看看:https://www.raspberrypi.org/documentation/remote-access/ssh/passwordless.md)

  3. (对于 pi 上的我)docker plugin disable vieux/sshfs:next

  4. (对于 pi 上的我)docker plugin set vieux/sshfs sshkey.source=/root/.ssh/(参见:https://blog.raphaelpiccolo.com/post/850

  5. (对于 pi 上的我)docker plugin enable vieux/sshfs:next

  6. (对我来说是 pi),这是我要发布的 docker-compose.yml :

    version: "3"
    
    services:
      myserver:
        image: nginx:latest
        ports:
          - "80:80"
        volumes:
          - COMMONVOLUME:/usr/share/nginx/html
    
      volumes:
        COMMONVOLUME:
          driver: "vieux/sshfs:next"
          driver_opts:
            sshcmd: @:/REMOTEPATH/WHERE/THE/FOLDER/TOBESYNCFORVOLUME/IS
            allow_other: ""