3

I have services openhab and mosquitto. I have internal network between openhab and mosquitto, it is ok

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

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

How can I do it? my docker-compose file

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
mpromonet
  • 11,326
  • 43
  • 62
  • 91
user2219071
  • 113
  • 10
  • 3
    Why do you need to add another IP for the Container? The other computers in your network should be able to reach mosquitto on `192.168.1.16:1884` and `192.168.1.16:9001`. – Garuno Nov 16 '22 at 14:54
  • I deleted my comments because they were misleading, Garuno is right, the container's ports are exposed through `ports:` in the compose file. Docker is setting up NAT for you and forwards the ports to the underlying backends. – Daniel W. Nov 16 '22 at 15:14

1 Answers1

3

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:

  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.

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. But I think you won't need this, please further explain your use case.

zsolt
  • 1,233
  • 8
  • 18