connect docker container to local site

240 Views Asked by At

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
1

There are 1 best solutions below

0
On BEST ANSWER

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.