简体   繁体   English

将 docker 容器连接到本地站点

[英]connect docker container to local site

I have services openhab and mosquitto .我有openhabmosquitto服务。 I have internal.network between openhab and mosquitto , it is ok我在openhabmosquitto之间有 internal.network ,没关系

I have in local.network 3 computers 192.168.1.16 , 192.168.1.17 , 192.168.1.18 on 192.168.1.16 run docker and mosquitto container我在 local.network 中有 3 台计算机192.168.1.16192.168.1.17192.168.1.18192.168.1.16上运行 docker 和mosquitto容器

Now I need add for mosquitto container new ip 192.168.1.20 , because I need send data from others computers in.network to mosquitto现在我需要为mosquitto容器添加 new ip 192.168.1.20 ,因为我需要从其他计算机 in.network 发送数据到mosquitto

How can I do it?我该怎么做? my docker-compose file我的 docker-compose 文件

version: '3.7'

services:
  openhab:
    image: "openhab/openhab:3.3.0"
    container_name: "openhab"
    restart: always
    networks:
      openhabnet:
        aliases:
          - openhab
    ports:
      - 8082:8080
      - 8444:8443
    volumes:
      - "/etc/localtime:/etc/localtime:ro"
      - "/etc/timezone:/etc/timezone:ro"
      - "./openhab_addons:/openhab/addons"
      - "./openhab_conf:/openhab/conf"
      - "./openhab_userdata:/openhab/userdata"
    environment:
      CRYPTO_POLICY: "unlimited"
      EXTRA_JAVA_OPTS: "-Duser.timezone=Europe/Berlin"
      OPENHAB_HTTP_PORT: "8080"
      OPENHAB_HTTPS_PORT: "8443"
      USER_ID: "1000"
      GROUP_ID: "1000"

  mosquitto:
    image: "eclipse-mosquitto:latest"
    container_name: "mosquitto"
    user: "1000:1000"
    restart: always
    networks:
      openhabnet:
        aliases:
          - mosquitto
    ports:
      - 1884:1883
      - 9001:9001
    volumes:
      - "./mosquitto/config:/mosquitto/config"
      - "./mosquitto/log:/mosquitto/log"
      - "./mosquitto/data:/mosquitto/data"
    environment:
      - TZ=Europe/Bratislava

networks:
  openhabnet:
    driver: bridge

Your mosquito container is already reacheable on the hosts.network with the ip of the docker host, 192.168.1.16 and on the ports you forwarded:您的蚊子容器已经可以在 hosts.network 上使用 docker 主机的 ip、192.168.1.16 以及您转发的端口访问:

  ports:
      - 1884:1883
      - 9001:9001

So on 192.168.1.16:1884 you can reach the mosquito containers 1883 port and 192.168.1.16:9001 you can reach the mosquito container 9001 port from your other computers too, given you allowed these on the firewalls on the computers, including the docker host.所以在 192.168.1.16:1884 上你可以到达蚊子容器 1883 端口和 192.168.1.16:9001 你也可以从你的其他计算机到达蚊子容器 9001 端口,假设你在计算机的防火墙上允许这些,包括 docker 主机.

But if you really want an IP for the mosquito container itself on your host.network then you will need to do macvlan: https://docs.docker.com.network/macvlan/ With this your container will get a virtual NIC and will connect to the physical.network the docker host is running on.但是如果你真的想要一个 IP 用于你的 host.network 上的蚊子容器本身,那么你需要做 macvlan: https://docs.docker.com.network/macvlan/有了这个你的容器将获得一个虚拟 NIC 并将连接到运行 docker 主机的物理网络。 But I think you won't need this, please further explain your use case.但我认为你不需要这个,请进一步解释你的用例。

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

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