I have a spring boot application which connects to a Mongo database. I have created a docker-compose file. The spring boot application has two instances. First instance runs on 8080 and 27017, which works perfectly fine. Now the second instance runs on 8083 and 27018. I can easily connect to 27017 and 27018 through Mongo GUI. However, when I run docker-compose up for the second instance, the spring boot gives the exception.
Following are my docker-compose files:
First Instance(docker-compose.yml):
version: '3'
services:
app:
container_name: HR-BACKEND
restart: always
build: .
ports:
- "8081:8080" #VF Webservice
links:
- mongo
mongo:
container_name: MONGOHR
image: mongo:4.0.2
ports:
- "27017:27017"
volumes:
- /data/hrdb:/data/db
First Instance (application.properties)
spring.data.mongodb.uri=mongodb://mongo:27017/tsp
Second Instance(docker-compose.yml):
version: '3'
services:
app:
container_name: VF-BACKEND
restart: always
build: .
ports:
- "8083:8080" #VF Webservice
links:
- mongovf
mongovf:
container_name: MONGOVF
image: mongo:4.0.2
ports:
- "27018:27017"
volumes:
- /data/hrdb:/data/db
Second Instance (application.properties)
spring.data.mongodb.uri=mongodb://mongovf:27018/tsp
Docker version:
Docker version 18.09.1, build 4c52b90
. I could not really find a solution in SO. Please let me know if more details are needed
In your second instance you are trying to connect to mongo on the wrong port. application.property should be:
spring.data.mongodb.uri=mongodb://mongovf:27017/tsp
In a docker compose context, you connect directly to containers without going through the docker host. This is why you have to connect directly to the container port instead of trying to connect through the port published on the docker host.
Related
I built a REST API using spring boot frame work. In the application I am trying to connect to mongo servers which I am successfully connected to while running in an IDE.
But when I try to run it in a docker I am facing the below issue.
Please help me solve it.
com.mongodb.MongoSocketException: qa-***mongo02.aws.*****.local: Name or service not known
Sharing my docker-compose.yml
Hope this helps.
version: "3.7"
services:
playground_spring_test1_java:
container_name: playground_spring_test1_java
build:
context: ./demo
dockerfile: Dockerfile
stdin_open: true
# env_file:
# - ./dev.env
tty: true
volumes:
- "./demo:/app"
ports:
- 80:8080
playground_spring_test1_mongo:
container_name: playground_spring_test1_mongo
image: mongo
volumes:
- playground_spring_test1_mongo_data:/data/db
ports:
- 27017:27017
volumes:
playground_spring_test1_mongo_data:
Im using MongoTemplate and my application.properties is-
spring.data.mongodb.uri=mongodb://playground_spring_test1_mongo:27017/?authSource=admin
I've a following docker-compose.yaml file:
version: "3"
services:
# Database
database:
image: mongo
container_name: database
restart: always
volumes: ["dbdata:/data/db"]
environment:
- MONGO_INITDB_ROOT_USERNAME=myusername
- MONGO_INITDB_ROOT_PASSWORD=mypassword
ports:
- 7000:27017
command: mongod
networks:
- webapp
# Server
server:
container_name: server
restart: always
build: ./server
volumes: ["./server:/var/www", "/var/www/node_modules"]
ports:
- "9000:3000"
depends_on:
- database
networks:
- webapp
networks:
webapp:
driver: bridge
volumes:
dbdata:
Now, I'm able to connect with my mongo database from my local windows machine through mongodb client but my server container is not able connect with database container.
I'm using following connection URI on my server to establish a connection:
mongodb://myusername:mypassword#database:7000/mydatabase
It must be noted that on my local machine I'm using the same URI to connect successfully with the database, the only difference is that I'm using localhost instead of database (container name) on my local machine.
Can you please tell me what I'm doing wrong here?
I have the following docker compose file that I use to spin up a mongo docker container which works as intended.
However, I cannot seem to connect to the container from compass on my local machine.
I executed docker inspect on my mongo container and got its IP and tried to connect using compass, but it didn't work - connection timed out.
Is it the IP of my docker network or the IP of the mongo container I need?
docker compose file:
version: "3"
services:
pokerStats:
image: pokerStats
container_name: pokerStats
ports:
- 8080:8080
depends_on:
- db
db:
image: mongo
container_name: mongo
volumes:
- ./database:/data
ports:
- "27017:27017"
I was able to get this working using the top answer on this question.
I had to change my docker-compose file to expose port 27018 on my local:
db:
image: mongo
container_name: mongo
volumes:
- ./database:/data
ports:
- "27018:27017"
And my connection details on Compass to be:
You are mapping port 27017 from your host machine to port 27017 on the container. So the following connection string should work mongodb://localhost:27017
i got a problem.
I made a docker-compose that runs mongo and node.
The problem is there is no way i use mongo from the container, i cannot start my node server.
Here there is my docker-compose :
version: '3'
services:
database:
build: ./Database
container_name: "dashboard_database"
ports:
- "27017:27017"
backend:
build: ./Backend
container_name: "dashboard_backend"
ports:
- "8080:8080"
depends_on:
- database
links:
- database
But when i start mongo without the container my node can reach it, i don't know why ...
Any idea ?
Thanks !
Dont define ports in the DB service. But afterwards only application will be able to access DB. Most probably it will work then. If you still want to access it from your PC then you should define a network. Try this
version: '3'
services:
database:
build: ./Database
container_name: "dashboard_database"
backend:
build: ./Backend
container_name: "dashboard_backend"
ports:
- "8080:8080"
depends_on:
- database
links:
- database
And for creating network
version: '3'
networks:
back-tier:
services:
database:
build: ./Database
container_name: "dashboard_database"
networks:
- back-tier
ports:
- "27017:27017"
backend:
build: ./Backend
container_name: "dashboard_backend"
networks:
- back-tier
ports:
- "8080:8080"
depends_on:
- database
All services in docker-compose are within the docker-compose created network, and can be addressed by their service names from other services. In your case the service names are database and backend, so for instance the database can be accessed by the backend with something like tcp://database:27017. You don't need to link them anymore.
https://runnable.com/docker/docker-compose-networking
Be aware depends_on only waits until the process has been started and does not wait for the process to be ready to accept connections.
https://docs.docker.com/compose/compose-file/#depends_on
https://docs.docker.com/compose/startup-order
The port mappings are only necessary if you want to make a service accessible from the local machine. In your examplte the backend service is accessible via localhost:8080.
If you want an external container to access a docker-compose service tne localhost:8080 wont work because localhost in the container isn't the same localhost as on your local machine where docker containers are running. You can create manually a docker network and connect the container and docker-compose services to it. See docker-compose-networking link and take a look at section Pre-existing Networks.
Does that help you?
I'm having trouble accessing a database created from a docker-compose file.
Given the following compose file, I should be able to connect to it from java using something like:
jdbc:postgresql://eprase:eprase#database:7000/eprase
However, the connection is rejected. I can't even use PGAdmin to connect it using the same details to create a new server.
I've entered the database container and ran psql commands to verify that the eprase user and database have been created according to postgres Docker documentation, everything seems fine. I can't tell if the problem is within the database container or something I need to change in the compose network.
The client & server services can largely be ignored, the server is a java based web API and the client is an Angular app.
Compose file:
version: "3"
services:
client:
image: eprase/client:latest
build: ./client/eprase-app
networks:
api:
ports:
- "5000:80"
tty: true
depends_on:
- server
server:
image: eprase/server:latest
build: ./server
networks:
api:
ports:
- "6000:8080"
depends_on:
- database
database:
image: postgres:9
volumes:
- "./database/data:/var/lib/postgresql/data"
environment:
- "POSTGRES_USER=eprase"
- "POSTGRES_PASSWORD=eprase"
- "POSTGRES_DB=eprase"
networks:
api:
ports:
- "7000:5432"
restart: unless-stopped
pgadmin:
image: dpage/pgadmin4:latest
environment:
- "PGADMIN_DEFAULT_EMAIL=admin#eprase.com"
- "PGADMIN_DEFAULT_PASSWORD=eprase"
networks:
api:
ports:
- "8000:80"
depends_on:
- database
networks:
api:
The PostgreSQL database is listening on container port 5432. The 7000:5432 line is mapping host port 7000 to container port 5432. That allows you to connect to the database on port 7000. But, your services on a common network (api) should communicate with each other via the container ports.
So, from the perspective of the containers for the client and server services, the connection string should be:
jdbc:postgresql://eprase:eprase#database:5432/eprase