Docker-Compose - 在主机网络上为容器提供一个 IP,并允许容器到容器连接和主机网络连接

时间:2021-06-07 05:02:52

标签: docker networking docker-compose lan

我有几个容器在由 docker-compose 控制的堆栈中运行。 其中有一个运行 node-red。

节点红色容器需要能够在网络中搜索主机网络 (192.168.1.0/24) 上的设备。

使用默认网络,容器在知道 IP 时可以 ping 并连接到主机网络,不幸的是,node-red 实现只扫描本地网络,而且它只有 docker 桥接网络(172...)所以它只在那里搜索。

我尝试了多种 docker 网络设置组合来尝试让容器将 192 网络视为本地网络。当我能够获得 192 网络地址时,容器无法 ping 外部(到主机)网络上的其他主机。

TLDR:Docker 容器需要能够看到其他 Docker 容器,并且在主机网络上也有一个 IP 地址,并且能够连接到主机网络上的其他服务。

1 个答案:

答案 0 :(得分:0)

好的,我找到了一个适合我的解决方案:

在 docker-compose.yml 中

const [posts, setPosts] = useState([])
const [thumbsNumber, setThumbsNumber] = useState({})

const thumbsUp = (id) => {
  console.log('Clicked thumb: ', id)
  setThumbsNumber(thumbs => ({
    ...thumbs,
    [id]: (thumbs[id] ?? 0) + 1
  }))
}


return (
  <div>
    {posts.map((post, index) => (
    <div key={index} style={{ marginBottom: '10px' }}>
      <p style={{ float: 'right' }}>{thumbsNumber[post.id] ?? 0}</p>
      <ThumbUpAltIcon className="thumbsUpBtn" onClick={()=> thumbsUp(post.id)} >
      </ThumbUpAltIcon>
    </div>
    ))}
  </div>
)

添加 app: image: youimage network_mode: "host" 正是我所需要的