I'm trying to run my app on Docker. Problem is that when I'm running image of mongo
, it works perfectly. But then I'm running myapp image which using mongo
as a db, it gives com.mongodb.MongoSocketOpenException: Exception opening socket
. By the way I'm creating both of them under the same network.
Exception
Note: when I run myapp from my local it works perfectly. Problem is just about image of my app. I can`t understand what is difference? Why dos it give this error?
First command, that run mongo image:
docker run --name=mongo --rm --network=mynet -p 27017:27017 mongo
Second command, that run myapp image:
docker run --name=myapp --rm --network=mynet -p 8088:8080 -e MONGO_URL=mongodb://mongo:27017 myapp
Inspection of mynet network:
"Name":"enote",
"Id":"95c6704778676fded0f856b2f905d60544bf67a83910b96193f00ef6a23730be",
"Created":"2023-08-21T07:59:34.479354749Z",
"Scope":"local",
"Driver":"bridge",
"EnableIPv6":false,
"IPAM":{
"Driver":"default",
"Options":{
},
"Config":[
{
"Subnet":"172.22.0.0/16",
"Gateway":"172.22.0.1"
}
]
},
"Internal":false,
"Attachable":false,
"Ingress":false,
"ConfigFrom":{
"Network":""
},
"ConfigOnly":false,
"Containers":{
"bf1bd274fcfdd61f93afc8be87d3542f1cefe81d62587a3d669b09b06cef2e84":{
"Name":"enote-java",
"EndpointID":"af36329326a5ce2c0ac3b24d1dc60c63d27a22e2df2b50c5469094bae21b8a05",
"MacAddress":"02:42:ac:16:00:03",
"IPv4Address":"172.22.0.3/16",
"IPv6Address":""
},
"fa44ba0e009744610ec7f93ff7c04078692d853714193e2dc542cfe2ed815845":{
"Name":"mongo",
"EndpointID":"89be69458e5614976622731e89351f93cd406f8f1dfdc474480310039b8c4a74",
"MacAddress":"02:42:ac:16:00:02",
"IPv4Address":"172.22.0.2/16",
"IPv6Address":""
}
},
"Options":{
},
"Labels":{
}
Dockerfile of myapp:
FROM adoptopenjdk/openjdk11:jdk-11.0.2.9-slim
WORKDIR /opt
ENV PORT 8080
EXPOSE 8080
COPY target/*.jar /opt/app.jar
ENTRYPOINT exec java -jar app.jar
This configurations solved my issue -
docker-compose:
version: '3.1' services: mongodb: image: mongo container_name: "mongodb" ports: - '27017:27017' networks: - myapp myapp: image: myuser/myapp:1.0.0 environment: - MONGO_URL=mongodb://mongodb:27017/dev container_name: "myapp" ports: - "8080:8080" depends_on: - mongodb networks: - myapp networks: myapp:
.properties:
spring.data.mongodb.uri=${MONGO_URL:mongodb://mongodb:27017/dev}