Docker-Compose Spring boo and Mongodb: Connection Refused

Asked

Viewed 441 times

1

I’m studying Docker, and I’m trying to run a small application made in Springboot along with Mongodb in a single container.

I wrote the following Dockerfile:

FROM openjdk:8-jre-alpine

ENV SPRING_OUTPUT_ANSI_ENABLED=ALWAYS \
JAVA_OPTS=""

ADD /target/*.jar /app.jar

EXPOSE 8080

CMD java ${JAVA_OPTS} -jar /app.jar

I also made a Docker-Compose.yml

version: "2"

services:
  mongodb:
    image: mongo
    ports:
      - "27017:27017"

 springapp:
   build: .
   depends_on:
     - mongodb
   ports:
     - 8080:8080
   links:
     - mongodb  

When I start the application, I get "Connection Refused". Below is my application.yml:

spring:
  data:
    monbodb:
      host: mongodb
      port: 27017
      database: customer

This example of Docker-Compose.yml picked up on the documentation, however I haven’t figured out what I’m doing wrong.

Thank you so much for the strength :)

1 answer

0

I made some changes to Docker-Compose.yml and it worked:

version: "3"
services:
   mongodb:
     container_name: sample-datastore
     image: mongo
   springapp:
     container_name: sample-app
     build: .
     depends_on:
       - mongodb
     ports:
       - 8080:8080
     links:
       - mongodb
     environment:
       SPRING_DATA_MONGODB_URI: mongodb://mongodb/customer

Basically, I removed the port settings, and I pass the URI from the database as a parameter for the application, no need to mark any database settings in Application.yml! I don’t know if this is the best way, yet you’ve answered my problem. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.