Я согласен, Дэвид ответ правильный.Но здесь есть вынос, ваша конфигурация тоже работает, я попробовал с моим примером, и вот результат.
Проблема в том, что ваше приложение не подключено к сети.
services:
company-repo-docker-app:
image: company-repo-image
build:
context: ./
dockerfile: Dockerfile
depends_on:
- mongo
ports:
- 8080:8080
networks:
- mongo_net
пример docker-compose.yml
version: '3.1'
services:
mongo-app:
image: barath-mongo
ports:
- "9000:9000"
depends_on:
- mongo
environment:
SPRING_DATA_MONGODB_PORT: 27017
SPRING_DATA_MONGODB_HOST: 172.28.0.2
SPRING_DATA_MONGODB_DATABASE: myDb
networks:
- mongo_net
mongo:
image: mongo
ports:
- '27017:27017'
networks:
mongo_net:
ipv4_address: 172.28.0.2
networks:
mongo_net:
driver: bridge
ipam:
driver: default
config:
-
subnet: 172.28.0.2/24
IP-адрес контейнера mongo :
$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ec2-user_mongo_1
172.28.0.2
сетевые настройки докера
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.28.0.2/24"
}
]
},
"Internal": false,
"Attachable": true,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"02b8b4148dd830eaddeb7e9ebd6305a6ae3c106bae5a495103c632aec7999274": {
"Name": "ec2-user_mongo-app_1",
"EndpointID": "885088d551888e1dec95b1c0e337c6a4b84f1bb2aebc4407963d3c0b4f6ec09d",
"MacAddress": "02:42:ac:1c:00:03",
"IPv4Address": "172.28.0.3/24",
"IPv6Address": ""
},
"3e4266fca3c8163412cc1c4733cad004aa0f366dc9fe30d03c7ecafe87b4e826": {
"Name": "ec2-user_mongo_1",
"EndpointID": "a6a72c7fc314a294c53d5c394ac1757702a0a95c588af2f730e971953dbbaa5f",
"MacAddress": "02:42:ac:1c:00:02",
"IPv4Address": "172.28.0.2/24",
"IPv6Address": ""
}
},
выход :
ongo-app_1 | 2019-01-28 17:07:29.710 INFO 1 --- [72.28.0.2:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:1}] to 172.28.0.2:27017
mongo-app_1 | 2019-01-28 17:07:29.735 INFO 1 --- [72.28.0.2:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=172.28.0.2:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 5]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=20314562}
mongo-app_1 | 2019-01-28 17:07:29.742 INFO 1 --- [72.28.0.2:27017] org.mongodb.driver.cluster : Discovered cluster type of STANDALONE
mongo-app_1 | 2019-01-28 17:07:30.387 INFO 1 --- [ main] com.barath.app.service.CustomerService : Saving the customer with customer details com.barath.app.document.Customer@37654521
mongo_1 | 2019-01-28T17:07:30.488+0000 I NETWORK [listener] connection accepted from 172.28.0.3:56250 #2 (2 connections now open)
mongo_1 | 2019-01-28T17:07:30.491+0000 I NETWORK [conn2] received client metadata from 172.28.0.3:56250 conn2: { driver: { name: "mongo-java-driver", version: "3.8.2" }, os: { type: "Linux", name: "Linux", architecture: "amd64", version: "4.14.77-81.59.amzn2.x86_64" }, platform: "Java/Oracle Corporation/1.8.0_191-b12" }
mongo-app_1 | 2019-01-28 17:07:30.497 INFO 1 --- [ main] org.mongodb.driver.connection : Opened connection [connectionId{localValue:2, serverValue:2}] to 172.28.0.2:27017
mongo_1 | 2019-01-28T17:07:30.576+0000 I STORAGE [conn2] createCollection: myDb.customer with generated UUID: 6bd5b022-16b6-4a04-96b0-d94988155a53
mongo-app_1 | 2019-01-28 17:07:31.514 INFO 1 --- [ main] pertySourcedRequestMappingHandlerMapping : Mapped URL path [/v2/api-docs] onto method [public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)]
mongo-app_1 | 2019-01-28 17:07:31.879 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
mongo-app_1 | 2019-01-28 17:07:32.372 INFO 1 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
mongo-app_1 | 2019-01-28 17:07:32.432 INFO 1 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
mongo-app_1 | 2019-01-28 17:07:32.522 INFO 1 --- [ main] s.d.s.w.s.ApiListingReferenceScanner : Scanning for api listing references
mongo-app_1 | 2019-01-28 17:07:32.977 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9000 (http) with context path ''
mongo-app_1 | 2019-01-28 17:07:32.984 INFO 1 --- [ main] com.barath.app.Application : Started Application in 11.151 seconds (JVM running for 13.149)