1

I'm trying to connect my containerized spring boot application with another containerized MQTT broker. Both of them are on their own projects as follows:

mqtt docker-compose.yml:

version: '3.9'

services:
  mqttbroker:
    container_name: mqttbroker
    restart: always
    volumes:
      - ./config:/mosquitto/config
      - ./data:/mosquitto/data
      - ./log:/mosquitto/log
    ports:
      - 8883:8883
    networks:
      - mynetwork
volumes:
  config:
  data:
  log:

mqtt Dockerfile

FROM eclipse-mosquitto
WORKDIR /mosquitto
COPY . .
EXPOSE 8883

And then the spring boot project is like:

spring boot docker-compose.yml

version: '3.8'
services:
  myapp:
    build: .
    container_name: myapp
    ports:
      - '8082:8082'
    stdin_open: true
    tty: true
    networks:
      - mynetwork

In my application.properties I try to connect to the MQTT broker like: mosquitto.url=tcp://mqttbroker:8883 and I get connection refused. However, if I run the spring boot application locally, I can connect to the docker container with mosquitto.url=tcp://localhost:8883. I would rather have all the configurations in my docker-compose files to decrease manual codes. I really appreciate your help in advance!

Wooopsa
  • 320
  • 1
  • 6
  • 22
  • 3
    [Communication between multiple docker-compose projects](https://stackoverflow.com/questions/38088279/communication-between-multiple-docker-compose-projects) discusses setups like these; either you need to run both parts in the same Compose file, or you need to explicitly configure the two projects to use the same Docker network. Do the answers there help you? – David Maze Nov 01 '22 at 11:37

1 Answers1

1

Yes, You do have to share the network as external.

you cab check the networks you have with this command : docker network ls

You would notice that they each have their own networks.

adding this to spring boot docker-compose file should fix the issue :

networks:
  default:
    name: mynetwork
    external: true

For more information about docker compose networks

George
  • 2,292
  • 2
  • 10
  • 21
  • This didn't work for my case. Wierd, but after inspecting the network, both of them were connected to bridge instead of mynetwork. After seeing this, I changed the run command and added --network=mynetwork to both of the docker containers, but still, getting the same error. – Wooopsa Nov 01 '22 at 11:11
  • I have updated my question, maybe you can try it out again. – George Nov 01 '22 at 11:46