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