Reputation: 91
In these days, I am trying to deploy my Spring Boot OAuth2 project. It has 3 different modules.(Authentication Server, Resource Server and Front-end) Authentication and Resource servers have own *.yml file for configurations such as mongodb name-port, server profile-ip etc. What I am trying to do exactly? I want to deploy spring boot application on docker but i dont want to put my database(mongodb) on docker as a container. I am not sure this structure is possible or not ? Because When i run my mongodb on my local(localhost:27017) after that try to deploy spring boot application on local docker as a container, i am getting Timeout exception for MongoDB. The application couldnt connect to external mongoDB(non docker container).
What should I do? Should I run mongodb on docker? I tried it also, Mongo runs successfully but still spring container couldnt run and connect to mongo. I tried to run another spring boot app without mongodb, it is working successfully and i made request from browser by ip&port, i got response from application as i expected.
*** MONGO URL **** mongodb://127.0.0.1:27017/db-localhost **** Authentication server .yml file **** server: port: 9080 contextPath: /auth-service tomcat: access_log_enabled: true basedir: target/tomcat security: basic: enabled: false spring: profiles: active: development thymeleaf: cache: false mongo: db: server: 127.0.0.1 port: 27017 logging: level: org.springframework.security: DEBUG --- spring: profiles: development data: mongodb: database: db-localhost --- spring: profiles: production data: mongodb: database: db-prod --- ***** DOCKER FILE ******* FROM java:8 VOLUME /tmp ADD auth-server-1.0-SNAPSHOT.jar app.jar EXPOSE 9080 RUN bash -c 'touch /app.jar' ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] **** DOCKER COMMAND ******* docker run -it -P --name authserver authserver
Upvotes: 0
Views: 5432
Reputation: 538
Question: What should I do?
At least for developing purposes I would recommend to also use docker for your mongodb instance. I had a similar setup with RabbitMQ in addition and it solved a lot of problems when I used docker for those as well. Using docker-compose
to set everything up makes it even easier. Later you can still specify which mongodb instance you want to use through your spring properties.
Problem: I tried it also, Mongo runs successfully but still spring container couldnt run and connect to mongo
The problem is probably because you have not set up any networks or hostnames for you services. Your spring application can not resolve the hostname of your mongo server, since you specified 127.0.0.1
for your mongodb server in your properties.
I would recommend using docker for your mongodb and use a docker-compose.yml
file like this to set everything up:
version: '3.7'
services:
resource-server:
image: demo/resource-server:latest
container_name: resource-server
depends_on:
- mongodb-example
networks:
- your-network
ports:
- 8080:8080
auth-server:
image: demo/auth-server:latest
container_name: auth-server
depends_on:
- mongodb-example
networks:
- your-network
ports:
- 8081:8080
mongodb-example:
image: mongo:latest
container_name: mongo-example
hostname: mongo-example
networks:
- your-network
ports:
- 27017:27017
networks:
your-network:
name: network-name
Of course you then need to adapt your property file or specify environment variables through your docker-compose.yml
file.
Upvotes: 0
Reputation: 12270
The issue with your configuration is referencing the mongodb from inside of the authservice on 127.0.0.1
which is the loopback adapter inside of the authservice container. So you tell your spring application that mongodb is running in the same container as the authservice spring application, which is not the case.
Either you are running your database as an own container (which requires to handle the data volumes correctly) and referencing it using the container name as hostname (via link) or you need to reference the externally running mongodb instance with the correct address. This would be the ip address of the machine running the docker daemon (I assume for your local environment something like 192.168.0.xxx
).
Upvotes: 0