简体   繁体   English

使用 docker stack 进行卷挂载

[英]Volume mounting with docker stack

Following is my docker-stack file.以下是我的 docker-stack 文件。

version: "3"
services:
  my-app:    
    image: my-image:latest    
    volumes:
      - ./certs:/certs   
    ports:
      - 6401:6401
    networks:
      my-net:        
         ipv4_address: 192.168.0.4
networks:
  my-net:
    external: true

It works fine on my machine, binding certs folder to certs inside the container.它在我的机器上运行良好,将 certs 文件夹绑定到容器内的 certs。 However doesn't work in my CI pipeline where i am deploying this service inside a docker-stack-node image.但是,在我在 docker-stack-node 映像中部署此服务的 CI 管道中不起作用。 The error i get is我得到的错误是

invalid mount config for type "bind": bind mount source path does not exist. “绑定”类型的安装配置无效:绑定安装源路径不存在。

I read that If you bind mount a host path into your service's containers, the path must exist on every swarm node.我读到如果您将主机路径绑定到服务的容器中,则该路径必须存在于每个 swarm 节点上。 So, I think i need to create a named volume.所以,我想我需要创建一个命名卷。 However, with the named volume i can't specify source path of certs.但是,对于命名卷,我无法指定证书的源路径。 It's confusing.这很混乱。 Can someone help with this?有人可以帮忙吗?

@Niraj I have ran into this issue as well and have figured out that each node needs the files to run. @Niraj 我也遇到过这个问题,并且发现每个节点都需要运行这些文件。 In your case I would do this:在你的情况下,我会这样做:

  1. In your Dockerfile create the certs folder在您的Dockerfile 中创建 certs 文件夹
    • RUN mkdir /certs
  2. Change compose-file to use absolute path更改撰写文件以使用绝对路径
    • - $PWD/certs:/certs
  3. Copy certs into container将证书复制到容器中
    • docker cp <file_name> <container_id>:/certs

Hope this helps!希望这可以帮助!

To bind to a directory the directory has to exist on the host machine so ./certs doesn't exist on the host running the container so...要绑定到目录,该目录必须存在于主机上,因此./certs在运行容器的主机上不存在,因此...

If you want to bind the volume you will need to run:如果要绑定卷,则需要运行:

mkdir -p ./certs

on the machine running the container and it will work.在运行容器的机器上,它将工作。

It works on your machine because the directory exists locally.它适用于您的机器,因为该目录存在于本地。

Copy directory to host复制目录到主机

scp -r /local/directory/ username@to_host:/remote/directory/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM