连接两个Docker容器

编程入门 行业动态 更新时间:2024-10-27 20:38:52
本文介绍了连接两个Docker容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个现有的docker容器网络和数据库。我想链接这两个容器,以便它们彼此通信。如果我使用--link命令,则它将把网络链接到一个新映像,而不是数据库。

I have two existing docker container web and db. I want to link these two container, so that they will communicate with each other. If i go with --link command means it will link web to a new image and not to the db.

推荐答案

使用-link 是在Docker网络出现之前连接容器的唯一方法。这些为容器间通信问题提供了更清洁的解决方案,同时解决了两个主要的链接限制:

Using --link was the only way of connecting containers before the advent of docker networks. These provide a "cleaner" solution to the problem of inter-container communication and at the same time solves 2 of the major limits of links:

  • 重新启动链接的容器会断开链接
  • 在不同主机上运行的容器之间不支持链接
  • 使用docker网络,您可以使用-net 选项在指定网络上启动容器:

    Using docker network you would use the --net option to start the containers on the specified network:

    docker network create example docker run -d --net example --name container1 <image> docker run -d --net example --name container2 <image>

    此时,两个容器可以通过地址< container相互访问-name> .example :即 container1.example 和 container2.example 。

    At this point the 2 container are mutually reachable via the address <container-name>.example: that is container1.example and container2.example.

    更多推荐

    连接两个Docker容器

    本文发布于:2023-10-08 09:57:02,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1472287.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:容器   两个   Docker

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!