将Docker容器连接到本地站点

fv2wmkja  于 2022-11-22  发布在  Docker
关注(0)|答案(1)|浏览(188)

bounty将在5天后过期。回答此问题可获得+50声望奖励。user2219071希望奖励现有回答

我有服务openhabmosquitto。我在openhabmosquitto之间有内部网络,可以
我在本地网络中有3台计算机192.168.1.16192.168.1.17192.168.1.18192.168.1.16上运行Docker和mosquitto容器
现在我需要为mosquitto容器添加新的ip 192.168.1.20,因为我需要将网络中其他计算机的数据发送到mosquitto
我该怎么做呢?我的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
xzlaal3s

xzlaal3s1#

您的mosquito容器已经可以通过docker主机的ip在主机网络192.168.1.16上和您转发的端口上访问:

ports:
      - 1884:1883
      - 9001:9001

因此,在192.168.1.16:1884上,您可以访问mosquito container 1883端口,在192.168.1.16:9001上,您也可以从其他计算机访问mosquito container 9001端口,因为您在计算机(包括Docker主机)的防火墙上允许这些端口。
但是如果你真的想在你的主机网络上为蚊子容器本身设置一个IP,那么你就需要执行macvlan:https://docs.docker.com/network/macvlan/使用此选项,您的container将获得一个虚拟网卡,并连接到运行Docker主机的物理网络。但我认为您不需要此选项,请进一步解释您的用例。

相关问题