Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection refused between containers

I tried linking a container running a spring boot 2 app to a container running mongo, but I get connection refused

docker-compose file to have a container for mongo (will later add another container for spring boot here as well)

version: '3.1'

services:
  mongo:
    image: mongo
    container_name: springboot-mongo
    ports:
      - 27017:27017
    volumes:
      - $HOME/data/springboot-mongo-data:/data/db
      - $HOME/data/springboot-mongo-bkp:/data/bkp
    restart: always

dockerfile for spring boot

FROM openjdk:11
RUN apt-get update && apt-get install bash
RUN mkdir -p /opt/app
ENV PROJECT_HOME /opt/app
COPY build/libs/recipe-book.jar $PROJECT_HOME/recipe-book.jar
WORKDIR $PROJECT_HOME
CMD ["java", "-Dspring.data.mongodb.uri=mongodb://springboot-mongo:27017/recipes", "-jar","./recipe-book.jar"]

i have tried different ways of sending the command line mongo uri: with localhost instead of springboot-mongo, i also tried how it is described here https://www.baeldung.com/spring-boot-command-line-arguments, more specifically -Dspring-boot.run.arguments=--spring.data.mongodb.uri=mongodb://springboot-mongo:27017/recipes. Every time it seems to hit a connection refused.

How can I make the spring container connect to mongo?

Thank you

Update, I have also tried adding the second container to the docker-compose file, as such

version: '3.1'

services:
  springboot:
    build: .
    restart: always
    container_name: springboot
    ports:
      - 8182:8080
    working_dir: /opt/app
    depends_on:
      - mongo

  mongo:
    image: mongo
    container_name: springboot-mongo
    ports:
      - 27017:27017
    volumes:
      - $HOME/data/springboot-mongo-data:/data/db
      - $HOME/data/springboot-mongo-bkp:/data/bkp
    restart: always

update 2:

I managed so partially solve the issue by first building the image locally on my computer, and then using the created image inside the docker-compose file, and having -Dspring.data.mongodb.uri=mongodb://springboot-mongo:27017/recipes as parameter, but still no luck building it directly in the docker-compose file

like image 635
Claudiu Guja Avatar asked Apr 27 '26 06:04

Claudiu Guja


2 Answers

You don't really need to use docker-compose.

Let's work step by step:

Create a network, like:

docker network create network-labolida

Start two container with:

docker run --hostname=www1 -p 81:8080  --net=network-labolida --name=www1  -t www1 
docker run --hostname=www2 -p 82:8080  --net=network-labolida --name=www2  -t www2 

Validate it:

docker network inspect network-labolida

This is going to tell you the container names associated to that network.

Then, the trick: USE THE CONTAINER NAMES!

DON'T USE EXPOSED PORTS but INTERNAL ONES: 8080!

I mean:

If you want to access your contained-services from your HOST, using PostMan for example, use:

http://127.0.0.1:81/api/mymicroservice/

http://127.0.0.1:89/api/mymicroservice/

BUT

If, what you want is to make your container-01 access the contanier-02 , then use:

http://www2:8080/api/mymicroservice/

(HOSTNAME+InternalPort) from the container-01.

like image 74
Leonardo Labolida Avatar answered Apr 28 '26 23:04

Leonardo Labolida


If you want two containers to talk to each other then you need to put them in the same docker network

update your docker-compose.yml file like this:

version: '3.1'
services:
  springboot:
    build: .
    restart: always
    container_name: springboot
    ports:
      - 8182:8080
    working_dir: /opt/app
    depends_on:
      - mongo
    networks:
      - local

  mongo:
    image: mongo
    container_name: springboot-mongo
    ports:
      - 27017:27017
    volumes:
      - $HOME/data/springboot-mongo-data:/data/db
      - $HOME/data/springboot-mongo-bkp:/data/bkp
    restart: always
    networks:
      - local
networks:
  local:
    driver: bridge
like image 41
shushu304 Avatar answered Apr 28 '26 23:04

shushu304



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!