Home:ALL Converter>Connecting spring boot docker container to MQTT docker container

Connecting spring boot docker container to MQTT docker container

Ask Time:2022-11-01T18:14:19         Author:Wooopsa

Json Formatter

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!

Author:Wooopsa,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/74274380/connecting-spring-boot-docker-container-to-mqtt-docker-container
yy