代码之家  ›  专栏  ›  技术社区  ›  morpheus

如何将文件从一个docker容器复制到另一台主机上的另一个容器?

  •  2
  • morpheus  · 技术社区  · 7 年前

    将文件从一个docker容器复制到另一台主机上的另一个容器的最佳方法是什么?我们在尝试这样做时遇到了几个问题。

    问题#1:我们提供了一个覆盖网络并将容器连接到它。创建容器时,容器可以使用IP地址相互ping,但不使用使用-h标志提供的主机名

    容器1:

    root@rca-ord:/# cat /etc/hosts
    127.0.0.1   localhost
    ::1 localhost ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    10.0.29.10  rca-ord
    

    root@ica-ord:/# cat /etc/hosts
    127.0.0.1   localhost
    ::1 localhost ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    10.0.29.11  ica-ord
    

    使用主机名ping失败

    root@ica-ord:/# ping rca-ord
    ping: unknown host rca-ord
    

    使用IP地址ping有效

    root@ica-ord:/# ping 10.0.29.10
    PING 10.0.29.10 (10.0.29.10) 56(84) bytes of data.
    64 bytes from 10.0.29.10: icmp_seq=1 ttl=64 time=0.684 ms
    64 bytes from 10.0.29.10: icmp_seq=2 ttl=64 time=0.434 ms
    64 bytes from 10.0.29.10: icmp_seq=3 ttl=64 time=0.731 ms
    64 bytes from 10.0.29.10: icmp_seq=4 ttl=64 time=0.672 ms
    64 bytes from 10.0.29.10: icmp_seq=5 ttl=64 time=0.545 ms
    64 bytes from 10.0.29.10: icmp_seq=6 ttl=64 time=1.25 ms
    ^C
    --- 10.0.29.10 ping statistics ---
    6 packets transmitted, 6 received, 0% packet loss, time 5010ms
    rtt min/avg/max/mdev = 0.434/0.719/1.251/0.258 ms
    

    看起来我们需要运行一些DNS服务器。怎样详情是什么?

    root@ica-ord:/# scp 10.0.29.10:test-file.txt .
    The authenticity of host '10.0.29.10 (10.0.29.10)' can't be established.
    ECDSA key fingerprint is SHA256:Bdnjhvxlk1ILzWpa8tvu+YLaHRqh9XBftIoeLSpKLBQ.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '10.0.29.10' (ECDSA) to the list of known hosts.
    root@10.0.29.10's password: 
    

    我们认为应该有一种简单的方法在多台主机上的容器之间复制文件。有吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   David Maze    7 年前

    Docker没有提供任何神奇的东西来让这变得简单。通常Docker打包的组件不会通过共享文件相互通信;相反,它们使用HTTP等网络协议或RabbitMQ等消息总线系统相互通信。

    我在这里看到的典型架构如下所示:

    1. 生产服务将文件写入存储层。

    2. 生产服务联系消费服务(通过HTTP连接;到共享RabbitMQ队列;知道另一个服务的服务器的主机名,或者使用服务发现系统(如Concur或Kubernetes service objects)来查找它)并告诉它文件的位置。

    3. 消费服务读取文件并对其进行处理。

    这种设置的优点是,您不需要处理N个服务试图将经过身份验证的scp设置到N个其他服务(甚至M个其他主机),您不必处理Docker卷共享(以及并发访问和权限管理)的复杂性,即使服务移动到另一个主机,并且没有立即获得文件的本地副本,它仍然可以工作。

    推荐文章