Docker-compose, spring app + mongoDB on non-default port - spring

I have a problem with connecting to mongodb from my app.
Here is the docker-compose file:
version: "3"
services:
olx-crawler:
container_name: olx-crawler
image: myimage:v1
ports:
- "8099:8099"
depends_on:
- olx-mongo
environment:
SPRING_DATA_MONGODB_HOST: olx-mongo
olx-mongo:
container_name: olx-mongo
image: mongo
ports:
- "27777:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: biafra
MONGO_INITDB_ROOT_PASSWORD: password
And here is my application.yaml:
spring:
data:
mongodb:
host: localhost
port: 27777
username: biafra
password: password
authentication-database: admin
logging:
level:
org.springframework.data.mongodb.core.MongoTemplate: DEBUG
server:
port: 8099
Now i have done a similar project to this (docker-compose -> spring app + mongodb) and it worked correctly, but it was with the default mongo port 27017.
And i know you have to use mongo container name instead of localhost, this is what this:
SPRING_DATA_MONGODB_HOST: olx-mongo
is for it replaces "localhost" in application.yml with olx-mongo, as you can see in app logs:
Exception in monitor thread while connecting to server olx-mongo:27777
Here are some logs:
olx-mongo | 2020-04-15T18:00:15.170+0000 I SHARDING [LogicalSessionCacheRefresh] Marking collection config.system.sessions as collection version: <unsharded>
olx-mongo | 2020-04-15T18:00:15.174+0000 I SHARDING [LogicalSessionCacheReap] Marking collection config.transactions as collection version: <unsharded>
olx-mongo | 2020-04-15T18:00:15.175+0000 I NETWORK [listener] Listening on /tmp/mongodb-27017.sock
olx-mongo | 2020-04-15T18:00:15.175+0000 I NETWORK [listener] Listening on 0.0.0.0
olx-mongo | 2020-04-15T18:00:15.175+0000 I NETWORK [listener] waiting for connections on port 27017
olx-crawler | 2020-04-15 18:00:15.436 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
olx-crawler | 2020-04-15 18:00:15.486 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 45ms. Found 1 MongoDB repository interfaces.
olx-mongo | 2020-04-15T18:00:16.000+0000 I SHARDING [ftdc] Marking collection local.oplog.rs as collection version: <unsharded>
olx-crawler | 2020-04-15 18:00:16.037 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8099 (http)
olx-crawler | 2020-04-15 18:00:16.050 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
olx-crawler | 2020-04-15 18:00:16.052 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.33]
olx-crawler | 2020-04-15 18:00:16.116 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
olx-crawler | 2020-04-15 18:00:16.117 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1487 ms
olx-crawler | 2020-04-15 18:00:16.468 INFO 1 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[olx-mongo:27777], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize
=500}
olx-crawler | 2020-04-15 18:00:16.469 INFO 1 --- [ main] org.mongodb.driver.cluster : Adding discovered server olx-mongo:27777 to client view of cluster
olx-crawler | 2020-04-15 18:00:16.550 INFO 1 --- [olx-mongo:27777] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server olx-mongo:27777
olx-crawler |
olx-crawler | com.mongodb.MongoSocketOpenException: Exception opening socket
olx-crawler | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-3.11.2.jar!/:na]
olx-crawler | at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:128) ~[mongodb-driver-core-3.11.2.jar!/:na]
olx-crawler | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.11.2.jar!/:na]
olx-crawler | at java.base/java.lang.Thread.run(Thread.java:844) [na:na]
olx-crawler | Caused by: java.net.ConnectException: Connection refused (Connection refused)
olx-crawler | at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
olx-crawler | at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:400) ~[na:na]
olx-crawler | at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:243) ~[na:na]
olx-crawler | at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:225) ~[na:na]
olx-crawler | at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:402) ~[na:na]
olx-crawler | at java.base/java.net.Socket.connect(Socket.java:591) ~[na:na]
olx-crawler | at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongodb-driver-core-3.11.2.jar!/:na]
olx-crawler | at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-3.11.2.jar!/:na]
olx-crawler | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-3.11.2.jar!/:na]
olx-crawler | ... 3 common frames omitted
olx-crawler |
olx-crawler | 2020-04-15 18:00:17.096 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
olx-crawler | 2020-04-15 18:00:17.229 INFO 1 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler'
olx-crawler | 2020-04-15 18:00:17.306 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8099 (http) with context path ''
olx-crawler | 2020-04-15 18:00:17.315 INFO 1 --- [ main] c.e.olxcrawler.OlxCrawlerApplication : Started OlxCrawlerApplication in 3.944 seconds (JVM running for 4.977)
Any help?

Well you wrote
And i know you have to use mongo container name instead of localhost, but it still does not work.
but you have
spring:
data:
mongodb:
host: localhost
port: 27777
Problem is that with this config you are not able to connect to mongo from within spring boot container. It's just configuration for "outside world" of container. For example you can connect to it from your locally running spring boot application which doesn't run inside docker.
To connect to mongo from within dockerized spring boot app, change host to olx-mongo and port to 27017.

Related

Inside docker container Eureka doesn't see the defaultZone parameter value in properties.yml

There are no problems happens if I'm launching apps without containers. My temporary solution is to send the parameter through environment variable, but I can't understand why does it work like this. Could somebody please explain what am I doing wrong?
application-docker.yml (api-gateway)
server:
port: 8080
spring:
application:
name: api-gateway
cloud:
gateway:
routes:
- id: customer
uri: lb://CUSTOMER
predicates:
- Path=/api/v1/customers/**
zipkin:
base-url: http://zipkin:9411/
eureka:
client:
serviceUrl:
defaultZone: http://eureka-server:8761/eureka/
fetch-registry: true
register-with-eureka: true
application-docker.yml (eureka-server)
server:
port: 8761
spring:
application:
name: eureka-server
zipkin:
base-url: http://zipkin:9411/
eureka:
client:
fetch-registry: false
register-with-eureka: false
docker-compose.yaml
services:
postgres:
container_name: postgres
image: postgres
environment:
POSTGRES_USER: *
POSTGRES_PASSWORD: *
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
ports:
- "5432:5432"
networks:
- postgres
restart: unless-stopped
zipkin:
image: openzipkin/zipkin
container_name: zipkin
ports:
- "9411:9411"
networks:
- spring
rabbitmq:
image: rabbitmq:3.9.11-management-alpine
container_name: rabbitmq
ports:
- "5672:5672"
- "15672:15672"
networks:
- spring
eureka-server:
image: ftmpt/eureka-server:latest
container_name: eureka-server
environment:
- SPRING_PROFILES_ACTIVE=docker
ports:
- "8761:8761"
networks:
- spring
depends_on:
- zipkin
api-gateway:
image: ftmpt/api-gateway:latest
container_name: api-gateway
environment:
- SPRING_PROFILES_ACTIVE=docker
# - eureka.client.service-url.defaultZone=http://eureka-server:8761/eureka
ports:
- "8080:8080"
networks:
- spring
- postgres
depends_on:
- eureka-server
- zipkin
networks:
postgres:
driver: bridge
spring:
driver: bridge
volumes:
postgres:
logs from api-gateway container:
Powered by Spring Boot 2.5.7
2022-10-03 16:19:28.632 INFO [api-gateway,,] 1 --- [ main] e.fatumepta.apigw.ApiGatewayApplication : Starting ApiGatewayApplication using Java 17.0.1 on f492a34f31a2 with PID 1 (/app/classes started by root in /)
2022-10-03 16:19:28.640 INFO [api-gateway,,] 1 --- [ main] e.fatumepta.apigw.ApiGatewayApplication : The following profiles are active: docker
2022-10-03 16:19:31.883 INFO [api-gateway,,] 1 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=7282d2ab-5b80-3882-a881-0bf42ac5f5e5
2022-10-03 16:19:32.624 INFO [api-gateway,,] 1 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration' of type [org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-10-03 16:19:32.638 INFO [api-gateway,,] 1 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration$ReactorDeferringLoadBalancerFilterConfig' of type [org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration$ReactorDeferringLoadBalancerFilterConfig] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-10-03 16:19:32.647 INFO [api-gateway,,] 1 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'reactorDeferringLoadBalancerExchangeFilterFunction' of type [org.springframework.cloud.client.loadbalancer.reactive.DeferringLoadBalancerExchangeFilterFunction] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-10-03 16:19:34.880 INFO [api-gateway,,] 1 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [After]
2022-10-03 16:19:34.880 INFO [api-gateway,,] 1 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Before]
2022-10-03 16:19:34.880 INFO [api-gateway,,] 1 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Between]
2022-10-03 16:19:34.880 INFO [api-gateway,,] 1 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Cookie]
2022-10-03 16:19:34.880 INFO [api-gateway,,] 1 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Header]
2022-10-03 16:19:34.880 INFO [api-gateway,,] 1 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Host]
2022-10-03 16:19:34.880 INFO [api-gateway,,] 1 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Method]
2022-10-03 16:19:34.880 INFO [api-gateway,,] 1 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Path]
2022-10-03 16:19:34.880 INFO [api-gateway,,] 1 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Query]
2022-10-03 16:19:34.880 INFO [api-gateway,,] 1 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [ReadBody]
2022-10-03 16:19:34.881 INFO [api-gateway,,] 1 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [RemoteAddr]
2022-10-03 16:19:34.881 INFO [api-gateway,,] 1 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Weight]
2022-10-03 16:19:34.881 INFO [api-gateway,,] 1 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [CloudFoundryRouteService]
2022-10-03 16:19:36.003 INFO [api-gateway,,] 1 --- [ main] DiscoveryClientOptionalArgsConfiguration : Eureka HTTP Client uses RestTemplate.
2022-10-03 16:19:36.316 WARN [api-gateway,,] 1 --- [ main] iguration$LoadBalancerCaffeineWarnLogger : Spring Cloud LoadBalancer is currently working with the default cache. You can switch to using Caffeine cache, by adding it and org.springframework.cache.caffeine.CaffeineCacheManager to the classpath.
2022-10-03 16:19:36.509 INFO [api-gateway,,] 1 --- [ main] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING
2022-10-03 16:19:36.782 INFO [api-gateway,,] 1 --- [ main] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1
2022-10-03 16:19:36.800 INFO [api-gateway,,] 1 --- [ main] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
2022-10-03 16:19:36.843 INFO [api-gateway,,] 1 --- [ main] com.netflix.discovery.DiscoveryClient : Disable delta property : false
2022-10-03 16:19:36.843 INFO [api-gateway,,] 1 --- [ main] com.netflix.discovery.DiscoveryClient : Single vip registry refresh property : null
2022-10-03 16:19:36.843 INFO [api-gateway,,] 1 --- [ main] com.netflix.discovery.DiscoveryClient : Force full registry fetch : false
2022-10-03 16:19:36.843 INFO [api-gateway,,] 1 --- [ main] com.netflix.discovery.DiscoveryClient : Application is null : false
2022-10-03 16:19:36.843 INFO [api-gateway,,] 1 --- [ main] com.netflix.discovery.DiscoveryClient : Registered Applications size is zero : true
2022-10-03 16:19:36.843 INFO [api-gateway,,] 1 --- [ main] com.netflix.discovery.DiscoveryClient : Application version is -1: true
2022-10-03 16:19:36.843 INFO [api-gateway,,] 1 --- [ main] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
2022-10-03 16:19:37.584 INFO [api-gateway,,] 1 --- [ main] c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8761/eureka/}, exception=I/O error on GET request for "http://localhost:8761/eureka/apps/": Connect to localhost:8761 [localhost/127.0.0.1] failed: Connection refused; nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8761 [localhost/127.0.0.1] failed: Connection refused stacktrace=org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8761/eureka/apps/": Connect to localhost:8761 [localhost/127.0.0.1] failed: Connection refused; nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8761 [localhost/127.0.0.1] failed: Connection refused
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:785)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:711)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:602)
at org.springframework.cloud.netflix.eureka.http.RestTemplateEurekaHttpClient.getApplicationsInternal(RestTemplateEurekaHttpClient.java:145)
at org.springframework.cloud.netflix.eureka.http.RestTemplateEurekaHttpClient.getApplications(RestTemplateEurekaHttpClient.java:135)
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.java:137)
at com.netflix.discovery.shared.transport.decorator.RedirectingEurekaHttpClient.executeOnNewServer(RedirectingEurekaHttpClient.java:121)
at com.netflix.discovery.shared.transport.decorator.RedirectingEurekaHttpClient.execute(RedirectingEurekaHttpClient.java:80)
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134)
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.java:137)
at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:120)
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134)
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.java:137)
at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77)
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134)
at com.netflix.discovery.DiscoveryClient.getAndStoreFullRegistry(DiscoveryClient.java:1101)
at com.netflix.discovery.DiscoveryClient.fetchRegistry(DiscoveryClient.java:1014)
at com.netflix.discovery.DiscoveryClient.<init>(DiscoveryClient.java:441)
at com.netflix.discovery.DiscoveryClient.<init>(DiscoveryClient.java:283)
at com.netflix.discovery.DiscoveryClient.<init>(DiscoveryClient.java:279)
at org.springframework.cloud.netflix.eureka.CloudEurekaClient.<init>(CloudEurekaClient.java:66)
at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration.eurekaClient(EurekaClientAutoConfiguration.java:295)
eureka-server logs from container
2022-10-06 11:46:06.693 INFO [eureka-server,,] 1 --- [ main] e.f.eureka.EurekaServerApplication : Starting EurekaServerApplication using Java 17.0.1 on bf39ca283c65 with PID 1 (/app/classes started by root in /)
2022-10-06 11:46:06.727 INFO [eureka-server,,] 1 --- [ main] e.f.eureka.EurekaServerApplication : The following profiles are active: docker
2022-10-06 11:46:10.568 INFO [eureka-server,,] 1 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=c87a0e94-d419-31a3-a4bc-1879ab705f9c
2022-10-06 11:46:12.271 INFO [eureka-server,,] 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8761 (http)
2022-10-06 11:46:12.311 INFO [eureka-server,,] 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-10-06 11:46:12.311 INFO [eureka-server,,] 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.55]
2022-10-06 11:46:12.604 INFO [eureka-server,,] 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-10-06 11:46:12.604 INFO [eureka-server,,] 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 5728 ms
2022-10-06 11:46:15.226 INFO [eureka-server,,] 1 --- [ main] c.s.j.s.i.a.WebApplicationImpl : Initiating Jersey application, version 'Jersey: 1.19.4 05/24/2017 03:20 PM'
2022-10-06 11:46:15.401 INFO [eureka-server,,] 1 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson
2022-10-06 11:46:15.402 INFO [eureka-server,,] 1 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson
2022-10-06 11:46:15.755 INFO [eureka-server,,] 1 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml
2022-10-06 11:46:15.755 INFO [eureka-server,,] 1 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml
2022-10-06 11:46:18.003 INFO [eureka-server,,] 1 --- [ main] DiscoveryClientOptionalArgsConfiguration : Eureka HTTP Client uses Jersey
2022-10-06 11:46:18.261 WARN [eureka-server,,] 1 --- [ main] iguration$LoadBalancerCaffeineWarnLogger : Spring Cloud LoadBalancer is currently working with the default cache. You can switch to using Caffeine cache, by adding it and org.springframework.cache.caffeine.CaffeineCacheManager to the classpath.
2022-10-06 11:46:18.274 INFO [eureka-server,,] 1 --- [ main] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING
2022-10-06 11:46:18.293 INFO [eureka-server,,] 1 --- [ main] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1
2022-10-06 11:46:18.295 INFO [eureka-server,,] 1 --- [ main] com.netflix.discovery.DiscoveryClient : Client configured to neither register nor query for data.
2022-10-06 11:46:18.300 INFO [eureka-server,,] 1 --- [ main] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1665056778299 with initial instances count: 0
2022-10-06 11:46:18.396 INFO [eureka-server,,] 1 --- [ main] c.n.eureka.DefaultEurekaServerContext : Initializing ...
2022-10-06 11:46:18.405 INFO [eureka-server,,] 1 --- [ main] c.n.eureka.cluster.PeerEurekaNodes : Adding new peer nodes [http://localhost:8761/eureka/]
2022-10-06 11:46:18.857 INFO [eureka-server,,] 1 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson
2022-10-06 11:46:18.857 INFO [eureka-server,,] 1 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson
2022-10-06 11:46:18.857 INFO [eureka-server,,] 1 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml
2022-10-06 11:46:18.857 INFO [eureka-server,,] 1 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml
2022-10-06 11:46:19.589 INFO [eureka-server,,] 1 --- [ main] c.n.eureka.cluster.PeerEurekaNodes : Replica node URL: http://localhost:8761/eureka/
2022-10-06 11:46:19.608 INFO [eureka-server,,] 1 --- [ main] c.n.e.registry.AbstractInstanceRegistry : Finished initializing remote region registries. All known remote regions: []
2022-10-06 11:46:19.609 INFO [eureka-server,,] 1 --- [ main] c.n.eureka.DefaultEurekaServerContext : Initialized
2022-10-06 11:46:19.855 INFO [eureka-server,,] 1 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator'
2022-10-06 11:46:19.953 INFO [eureka-server,,] 1 --- [ main] o.s.c.n.e.s.EurekaServiceRegistry : Registering application EUREKA-SERVER with eureka with status UP
2022-10-06 11:46:19.978 INFO [eureka-server,,] 1 --- [ Thread-9] o.s.c.n.e.server.EurekaServerBootstrap : Setting the eureka configuration..
2022-10-06 11:46:19.991 INFO [eureka-server,,] 1 --- [ Thread-9] o.s.c.n.e.server.EurekaServerBootstrap : isAws returned false
2022-10-06 11:46:20.008 INFO [eureka-server,,] 1 --- [ Thread-9] o.s.c.n.e.server.EurekaServerBootstrap : Initialized server context
2022-10-06 11:46:20.008 INFO [eureka-server,,] 1 --- [ Thread-9] c.n.e.r.PeerAwareInstanceRegistryImpl : Got 1 instances from neighboring DS node
2022-10-06 11:46:20.008 INFO [eureka-server,,] 1 --- [ Thread-9] c.n.e.r.PeerAwareInstanceRegistryImpl : Renew threshold is: 1
2022-10-06 11:46:20.008 INFO [eureka-server,,] 1 --- [ Thread-9] c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP
2022-10-06 11:46:20.034 INFO [eureka-server,,] 1 --- [ Thread-9] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2022-10-06 11:46:20.034 INFO [eureka-server,,] 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8761 (http) with context path ''
2022-10-06 11:46:20.035 INFO [eureka-server,,] 1 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8761
2022-10-06 11:46:20.089 INFO [eureka-server,,] 1 --- [ main] e.f.eureka.EurekaServerApplication : Started EurekaServerApplication in 14.267 seconds (JVM running for 14.702)

Running Spring Boot Native Application on Azure Functions: Permission denied with server.port: 80

I have created a simple Spring Boot Application and compiled it with Spring Native gradle bootBuildImage. I uploaded the docker container to an private registry and deployed it to Microsoft Azure Function. Accessing the function will correctly start the Spring Boot Application inside the docker container. But it crashes with following exception:
2021-11-09T15:46:55.996267267Z 2021-11-09 15:46:55.996 INFO 1 --- [ main] o.s.nativex.NativeListener : This application is bootstrapped with code generated with Spring AOT
2021-11-09T15:46:55.997230973Z
2021-11-09T15:46:55.997396574Z . ____ _ __ _ _
2021-11-09T15:46:55.997539875Z /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
2021-11-09T15:46:55.997679976Z ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
2021-11-09T15:46:55.997816777Z \\/ ___)| |_)| | | | | || (_| | ) ) ) )
2021-11-09T15:46:55.997979078Z ' |____| .__|_| |_|_| |_\__, | / / / /
2021-11-09T15:46:55.998114779Z =========|_|==============|___/=/_/_/_/
2021-11-09T15:46:55.999546489Z :: Spring Boot :: (v2.5.6)
2021-11-09T15:46:55.999559889Z
2021-11-09T15:46:56.065075944Z 2021-11-09 15:46:56.054 INFO 1 --- [ main] o.s.boot.SpringApplication : Starting application using Java 11.0.13 on 421026966d15 with PID 1 (started by cnb in /workspace)
2021-11-09T15:46:56.065102544Z 2021-11-09 15:46:56.056 INFO 1 --- [ main] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default
2021-11-09T15:46:56.095177553Z 2021-11-09 15:46:56.094 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-11-09T15:46:56.097067866Z 2021-11-09 15:46:56.096 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 1 ms. Found 1 JPA repository interfaces.
2021-11-09T15:46:56.147529016Z 2021-11-09 15:46:56.147 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 80 (http)
2021-11-09T15:46:56.148811625Z 2021-11-09 15:46:56.148 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-11-09T15:46:56.149445829Z 2021-11-09 15:46:56.148 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.54]
2021-11-09T15:46:56.154613565Z 2021-11-09 15:46:56.154 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-11-09T15:46:56.155352670Z 2021-11-09 15:46:56.154 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 95 ms
2021-11-09T15:46:56.177839326Z 2021-11-09 15:46:56.177 INFO 1 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-11-09T15:46:56.179695539Z 2021-11-09 15:46:56.179 INFO 1 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.32.Final
2021-11-09T15:46:56.181115849Z 2021-11-09 15:46:56.180 INFO 1 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-11-09T15:46:56.183164263Z 2021-11-09 15:46:56.182 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-11-09T15:46:56.533544294Z 2021-11-09 15:46:56.533 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-11-09T15:46:56.534665902Z 2021-11-09 15:46:56.533 INFO 1 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
2021-11-09T15:46:56.675326478Z 2021-11-09 15:46:56.675 INFO 1 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-11-09T15:46:56.676515986Z 2021-11-09 15:46:56.676 INFO 1 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-11-09T15:46:56.722262803Z 2021-11-09 15:46:56.722 WARN 1 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-11-09T15:46:56.746931074Z 2021-11-09 15:46:56.746 WARN 1 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat server
2021-11-09T15:46:56.749313091Z 2021-11-09 15:46:56.749 INFO 1 --- [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2021-11-09T15:46:56.750124797Z 2021-11-09 15:46:56.749 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2021-11-09T15:46:56.877499580Z 2021-11-09 15:46:56.877 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
2021-11-09T15:46:56.878593388Z 2021-11-09 15:46:56.877 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2021-11-09T15:46:56.888123954Z 2021-11-09 15:46:56.880 INFO 1 --- [ main] ConditionEvaluationReportLoggingListener :
2021-11-09T15:46:56.888151454Z
2021-11-09T15:46:56.888156354Z Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-11-09T15:46:56.888160554Z 2021-11-09 15:46:56.880 ERROR 1 --- [ main] o.s.boot.SpringApplication : Application run failed
2021-11-09T15:46:56.888164954Z
2021-11-09T15:46:56.888168654Z org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat server
2021-11-09T15:46:56.888172954Z at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[de.aditu.buchschrank.BuchschrankApplicationKt:5.3.12]
2021-11-09T15:46:56.888177354Z at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[de.aditu.buchschrank.BuchschrankApplicationKt:5.3.12]
2021-11-09T15:46:56.888181254Z at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[na:na]
2021-11-09T15:46:56.888196454Z at java.lang.Iterable.forEach(Iterable.java:75) ~[na:na]
2021-11-09T15:46:56.888200454Z at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[de.aditu.buchschrank.BuchschrankApplicationKt:5.3.12]
2021-11-09T15:46:56.888204354Z at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[de.aditu.buchschrank.BuchschrankApplicationKt:5.3.12]
2021-11-09T15:46:56.888208354Z at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[na:na]
2021-11-09T15:46:56.888212155Z at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[na:na]
2021-11-09T15:46:56.888216155Z at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[na:na]
2021-11-09T15:46:56.888220055Z at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[de.aditu.buchschrank.BuchschrankApplicationKt:2.5.6]
2021-11-09T15:46:56.888224455Z at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[de.aditu.buchschrank.BuchschrankApplicationKt:2.5.6]
2021-11-09T15:46:56.888228555Z at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[de.aditu.buchschrank.BuchschrankApplicationKt:2.5.6]
2021-11-09T15:46:56.888232355Z at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[de.aditu.buchschrank.BuchschrankApplicationKt:2.5.6]
2021-11-09T15:46:56.888236255Z at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[de.aditu.buchschrank.BuchschrankApplicationKt:2.5.6]
2021-11-09T15:46:56.888240055Z at de.aditu.buchschrank.BuchschrankApplicationKt.main(BuchschrankApplication.kt:17) ~[na:na]
2021-11-09T15:46:56.888243955Z Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat server
2021-11-09T15:46:56.888247755Z at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:229) ~[na:na]
2021-11-09T15:46:56.888251555Z at org.springframework.boot.web.servlet.context.WebServerStartStopLifecycle.start(WebServerStartStopLifecycle.java:43) ~[na:na]
2021-11-09T15:46:56.888255455Z at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~[de.aditu.buchschrank.BuchschrankApplicationKt:5.3.12]
2021-11-09T15:46:56.888259455Z ... 14 common frames omitted
2021-11-09T15:46:56.888263155Z Caused by: java.lang.IllegalArgumentException: standardService.connector.startFailed
2021-11-09T15:46:56.888266855Z at org.apache.catalina.core.StandardService.addConnector(StandardService.java:238) ~[na:na]
2021-11-09T15:46:56.888270755Z at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.addPreviouslyRemovedConnectors(TomcatWebServer.java:282) ~[na:na]
2021-11-09T15:46:56.888275055Z at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:213) ~[na:na]
2021-11-09T15:46:56.888281755Z ... 16 common frames omitted
2021-11-09T15:46:56.888285555Z Caused by: org.apache.catalina.LifecycleException: Protocol handler start failed
2021-11-09T15:46:56.888289355Z at org.apache.catalina.connector.Connector.startInternal(Connector.java:1075) ~[na:na]
2021-11-09T15:46:56.888293055Z at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[na:na]
2021-11-09T15:46:56.888296855Z at org.apache.catalina.core.StandardService.addConnector(StandardService.java:234) ~[na:na]
2021-11-09T15:46:56.888300655Z ... 18 common frames omitted
2021-11-09T15:46:56.888304255Z Caused by: java.net.SocketException: Permission denied
2021-11-09T15:46:56.888308255Z at com.oracle.svm.jni.JNIJavaCallWrappers.jniInvoke_VA_LIST_SocketException_constructor_df417f52c09624a386e25e5af85993f853394076(JNIJavaCallWrappers.java:0) ~[na:na]
2021-11-09T15:46:56.888312255Z at sun.nio.ch.Net.bind0(Net.java) ~[na:na]
2021-11-09T15:46:56.888315955Z at sun.nio.ch.Net.bind(Net.java:455) ~[na:na]
2021-11-09T15:46:56.888319655Z at sun.nio.ch.Net.bind(Net.java:447) ~[na:na]
2021-11-09T15:46:56.888323255Z at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:227) ~[na:na]
2021-11-09T15:46:56.888327055Z at org.apache.tomcat.util.net.NioEndpoint.initServerSocket(NioEndpoint.java:271) ~[de.aditu.buchschrank.BuchschrankApplicationKt:9.0.54]
2021-11-09T15:46:56.888330955Z at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:226) ~[de.aditu.buchschrank.BuchschrankApplicationKt:9.0.54]
2021-11-09T15:46:56.888334755Z at org.apache.tomcat.util.net.AbstractEndpoint.bindWithCleanup(AbstractEndpoint.java:1208) ~[de.aditu.buchschrank.BuchschrankApplicationKt:9.0.54]
2021-11-09T15:46:56.888338655Z at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:1294) ~[de.aditu.buchschrank.BuchschrankApplicationKt:9.0.54]
2021-11-09T15:46:56.888342555Z at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:614) ~[de.aditu.buchschrank.BuchschrankApplicationKt:9.0.54]
2021-11-09T15:46:56.888346355Z at org.apache.catalina.connector.Connector.startInternal(Connector.java:1072) ~[na:na]
2021-11-09T15:46:56.888350255Z ... 20 common frames omitted
2021-11-09T15:46:56.888353856Z
Regarding the logs, Azure starts docker with
docker run -d -p 7502:80...
The first port seems to be random.
The full source code can be found here: https://github.com/lesestunden/buchschrank-backend
Does anybody have an idea whats going wrong?
Thanks a lot!
Your application should listen on a non-privileged port (> 1024).
The created container is unable to use a privileged port. The bootBuildImage task creates and runs images utilizing buildpacks (for runtime paketobuildpacks/run:tiny-cnb), the application in the container doesn't run as root. Therefore you can not listen on a port below 1024.
Spring boot uses paketo-buildpacks, which have some security-hardenings in them
The stack images are run as a dedicated non-root user when building
and running applications.
see paketo documentation
Solution is using a port bigger than 1024 (e.g. 8080) and configure the port like in this answer: Azure Functions with docker: How change port?

Exception opening socket - MongoDB, Docker

I have Maven Multi-Module application(Spring Boot + MySql + MongoDB) with using docker image, but i can't get connection to MongoDB.
Thing is when local mongo instance "MongoDB Server on Windows Services" is turned on and use spring.data.mongodb.host=localhost all is working fine.
But when I turn off mongo instance and try to go with:
spring.data.mongodb.host=$(MONGO_HOST) to use it with docker I'm getting error "Exception opening socket"
I start my application with execute command:
mvn install and tryed with docker-compose up --build
console
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.1)
2021-09-25 12:19:22.635 INFO 8588 --- [ main] c.o.o.n.controller.NoteControllerTest : Starting NoteControllerTest using Java 16.0.1 on AntonioPC with PID 8588 (started by ajago in D:\Programming\Eclipse-workspace\OC_Project10\Notes)
2021-09-25 12:19:22.642 INFO 8588 --- [ main] c.o.o.n.controller.NoteControllerTest : No active profile set, falling back to default profiles: default
2021-09-25 12:19:23.776 INFO 8588 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-09-25 12:19:23.777 INFO 8588 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-09-25 12:19:23.824 INFO 8588 --- [ main] .RepositoryConfigurationExtensionSupport : Spring Data JPA - Could not safely identify store assignment for repository candidate interface com.openclassrooms.ocproject10.notes.repository.NoteRepository. If you want this repository to be a JPA repository, consider annotating your entities with one of these annotations: javax.persistence.Entity, javax.persistence.MappedSuperclass (preferred), or consider extending one of the following types with your repository: org.springframework.data.jpa.repository.JpaRepository.
2021-09-25 12:19:23.826 INFO 8588 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 36 ms. Found 0 JPA repository interfaces.
2021-09-25 12:19:24.093 INFO 8588 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-09-25 12:19:24.094 INFO 8588 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
2021-09-25 12:19:24.126 INFO 8588 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 31 ms. Found 1 MongoDB repository interfaces.
2021-09-25 12:19:24.876 INFO 8588 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-09-25 12:19:25.458 INFO 8588 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-09-25 12:19:25.596 INFO 8588 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-09-25 12:19:25.685 INFO 8588 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.32.Final
2021-09-25 12:19:25.882 INFO 8588 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-09-25 12:19:26.112 INFO 8588 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2021-09-25 12:19:27.051 INFO 8588 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-09-25 12:19:27.065 INFO 8588 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-09-25 12:19:27.486 INFO 8588 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2021-09-25 12:19:27.586 INFO 8588 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:143) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144) ~[mongodb-driver-core-4.2.3.jar:na]
at java.base/java.lang.Thread.run(Thread.java:831) ~[na:na]
Caused by: java.net.ConnectException: Connection refused: no further information
at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:669) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333) ~[na:na]
at java.base/java.net.Socket.connect(Socket.java:645) ~[na:na]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:107) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.2.3.jar:na]
... 4 common frames omitted
2021-09-25 12:19:28.302 WARN 8588 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-09-25 12:19:28.988 INFO 8588 --- [ main] o.s.b.t.m.w.SpringBootMockServletContext : Initializing Spring TestDispatcherServlet ''
2021-09-25 12:19:28.988 INFO 8588 --- [ main] o.s.t.web.servlet.TestDispatcherServlet : Initializing Servlet ''
2021-09-25 12:19:28.990 INFO 8588 --- [ main] o.s.t.web.servlet.TestDispatcherServlet : Completed initialization in 2 ms
2021-09-25 12:19:29.018 INFO 8588 --- [ main] c.o.o.n.controller.NoteControllerTest : Started NoteControllerTest in 7.33 seconds (JVM running for 9.77)
2021-09-25 12:19:29.380 INFO 8588 --- [ main] org.mongodb.driver.cluster : Cluster description not yet available. Waiting for 30000 ms before timing out
docker-compose.yml
version: '2'
services:
reports:
image: 'reports:latest'
build:
context: ./reports
container_name: reports
ports:
- 8080:8080
environment:
- PATIENTS_URL=http://gps:8081
- NOTES_URL=http://rewards:8082
patients:
image: 'patients:latest'
build:
context: ./patients
container_name: patients
ports:
- 8081:8081
environment:
- MYSQL_HOST=db-mysql
depends_on:
- db-mysql
notes:
image: 'notes:latest'
build:
context: ./notes
container_name: notes
ports:
- 8082:8082
environment:
- MONGO_HOST=mongodb
depends_on:
- mongodb
db-mysql:
image: mysql:latest
container_name: mysql
ports:
- 3306:3306
command: --innodb --init-file /data/application/init.sql
volumes:
- ./init.sql:/data/application/init.sql
environment:
MYSQL_DATABASE: project_10
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: Musapa1990..
mongodb:
image: mongo:latest
container_name: mongodb
network_mode: host
restart: always
ports:
- 27017:27017
environment:
MONGO_INITDB_DATABASE: project_10
application.properties
# App config
logging.level.org.springframework=INFO
spring.application.name=P10_notes
server.port=8082
################### MongoDB Configuration ##########################
spring.data.mongodb.database=project_10
spring.data.mongodb.port=27017
#spring.data.mongodb.host=localhost
spring.data.mongodb.host=$(MONGO_HOST)
spring.data.mongodb.authentication-database=admin
# thymeleaf configurations for Auto reload Thymeleaf templates without restart
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=file:src/main/resources/templates/
I read similar articles here and tried different things, but nothing helped me.
In the end I messed up something when installing mongodb. When I reinstalled the mongo it worked. I did not make any crucial changes in the code.
Only what I added was:
################### MongoDB Configuration ##########################
spring.data.mongodb.database=project_10
spring.data.mongodb.port=27017
#spring.data.mongodb.host=localhost
spring.data.mongodb.host=${MONGO_HOST}
spring.data.mongodb.authentication-database=admin
I added that on all 3 module on application.properties because each of modules are using MongoDB.
If that not work then you can try that
change localhost and 127.0.0.1 it worked for me
like this
#spring.data.mongodb.host=localhost
spring.data.mongodb.host=127.0.0.1

Spring Docker Container stops because it tries to connect to localhost MongoDB even though it already connects correctly to MongoDB Container

Edit: The solution is as described by Valijon. But in addition, the following line was missing before my first insert in the DemoApplication.java:
MongoOperations mongoOps = new MongoTemplate(new SimpleMongoClientDbFactory(new ConnectionString("mongodb://docker_mongo:27017/db")));
so I wrote a very basic Spring application that is supposed to connect to a MongoDB, store some data, retrieve it and print some stuff for testing purposes.
For that purpose, I use docker-compose up to create a container "docker_mongo" and one called "docker_spring".
Now the connection to the Mongo container is actually successfull (see logs), but after that, the Spring container tries to establish another connection to a standard MongoDB on localhost:27017. And that then fails which leads to the container to exit with code 1.
I also edited my application.properties (see below).
I guess it has something to do with Spring Mongo autoconfiguration and I already tried to deactivate it as described in this post: Mongo tries to connect automatically to port 27017(localhost).
However, deactivating the autoconfiguration leads to other errors that I have no idea how to solve right now (I guess I have to refactor the way I connect to the MongoDB in my Spring application and I did not find simple enough tutorials on the internet that tells me how to do that without using the autoconfig features).
I am really a beginner when it comes to coding, so sorry when the question is of poor quality or a duplicate <3 (I googled quite some hours until now though...)
I feel like the solution is really simple, but I just found no article that covers exactly my issue :'(
application.properties:
spring.data.mongodb.host=mongo
spring.data.mongodb.port=27017
spring.data.mongodb.database=db
spring.data.mongodb.uri=mongodb://docker_mongo:27017/db
Dockerfile:
FROM openjdk:11
VOLUME /tmp
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} /tmp/app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/tmp/app.jar"]
RUN echo "hello world"
docker-compose.yaml:
version: '3.1'
services:
mongo:
container_name: docker_mongo
networks:
- gateway
ports:
- "27017:27017"
hostname: mongo
image: mongo:latest
command: mongod --port 27017
volumes:
- ./data/db:/data/db
spring:
build: .
container_name: docker_spring
networks:
- gateway
ports:
- "8080:8080"
hostname: spring
depends_on:
- mongo
networks:
gateway:
driver: "bridge"
DemoApplication.java that is being called by the main (I outsourced that into a separate class):
package com.example.demo;
import com.example.model.Expenditure;
import com.mongodb.client.MongoClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import java.time.LocalDate;
/*
#SpringBootApplication(exclude={
MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class
})
*/
#SpringBootApplication
#ComponentScan({"com.example.client","com.example.demo","com.example.model"})
public class DemoApplication implements CommandLineRunner {
#Autowired
private ExpenditureRepository repository;
#Override
public void run(String... args) throws Exception {
MongoOperations mongoOps = new MongoTemplate(new SimpleMongoClientDbFactory(MongoClients.create(), "db"));
mongoOps.insert(new Expenditure("Aldi",10.01, LocalDate.parse("2019-10-05")));
System.out.println(mongoOps.findOne(new Query(Criteria.where("name").is("Aldi")),Expenditure.class));
mongoOps.dropCollection("expenditure");
PopulateExpenditureDB.populateExpenditures(mongoOps);
}
}
MongoDbFactory.java:
package com.example.demo;
import com.mongodb.client.MongoDatabase;
import org.springframework.dao.DataAccessException;
public interface MongoDbFactory {
MongoDatabase getDb() throws DataAccessException;
MongoDatabase getDb(String dbName) throws DataAccessException;
}
ExpenditureRepository.java:
package com.example.demo;
import com.example.model.Expenditure;
import java.time.LocalDate;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
//mongodb Host & Port defined in application.properties
public interface ExpenditureRepository extends MongoRepository<Expenditure, String> {
/*
public void create(Expenditure expenditure) {
if(expenditure.)
}
public Expenditure findById(int id) {
return data.get(id)
}
*/
public Expenditure findByName (String name);
public List<Expenditure> findByExDate (LocalDate exDate);
}
Docker logs:
~/new_Webapp$ sudo docker-compose up
Creating network "newwebapp_gateway" with driver "bridge"
Building spring
Step 1/6 : FROM openjdk:11
---> 612d4d483eee
Step 2/6 : VOLUME /tmp
---> Running in 3aff2f893162
Removing intermediate container 3aff2f893162
---> 2bf19d4c67b1
Step 3/6 : ARG JAR_FILE=target/*.jar
---> Running in b7c3782bbc1a
Removing intermediate container b7c3782bbc1a
---> b029607db12b
Step 4/6 : COPY ${JAR_FILE} /tmp/app.jar
---> f3168bc17ae3
Step 5/6 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/tmp/app.jar"]
---> Running in f729f7204cdd
Removing intermediate container f729f7204cdd
---> aceb4c1a0bda
Step 6/6 : RUN echo "hello world"
---> Running in d4ab9bb0f498
hello world
Removing intermediate container d4ab9bb0f498
---> 45ae63ef2bd1
Successfully built 45ae63ef2bd1
Successfully tagged newwebapp_spring:latest
WARNING: Image for service spring was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating docker_mongo ...
Creating docker_mongo ... done
Creating docker_spring ...
Creating docker_spring ... done
Attaching to docker_mongo, docker_spring
docker_mongo | 2020-02-07T14:45:26.462+0000 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=mongo
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] db version v4.2.2
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] git version: a0bbbff6ada159e19298d37946ac8dc4b497eadf
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.1.1 11 Sep 2018
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] allocator: tcmalloc
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] modules: none
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] build environment:
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] distmod: ubuntu1804
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] distarch: x86_64
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] target_arch: x86_64
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] options: { net: { bindIp: "*", port: 27017 } }
docker_mongo | 2020-02-07T14:45:26.464+0000 I STORAGE [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
docker_mongo | 2020-02-07T14:45:26.464+0000 I STORAGE [initandlisten]
docker_mongo | 2020-02-07T14:45:26.464+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
docker_mongo | 2020-02-07T14:45:26.464+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
docker_mongo | 2020-02-07T14:45:26.464+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=7471M,cache_overflow=(file_max=0M),session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress],
docker_mongo | 2020-02-07T14:45:26.877+0000 I STORAGE [initandlisten] WiredTiger message [1581086726:877548][1:0x7ff0c5201b00], txn-recover: Recovering log 36 through 37
docker_mongo | 2020-02-07T14:45:26.904+0000 I STORAGE [initandlisten] WiredTiger message [1581086726:904522][1:0x7ff0c5201b00], txn-recover: Recovering log 37 through 37
docker_mongo | 2020-02-07T14:45:26.950+0000 I STORAGE [initandlisten] WiredTiger message [1581086726:950147][1:0x7ff0c5201b00], txn-recover: Main recovery loop: starting at 36/5888 to 37/256
docker_mongo | 2020-02-07T14:45:27.008+0000 I STORAGE [initandlisten] WiredTiger message [1581086727:8723][1:0x7ff0c5201b00], txn-recover: Recovering log 36 through 37
docker_mongo | 2020-02-07T14:45:27.044+0000 I STORAGE [initandlisten] WiredTiger message [1581086727:44798][1:0x7ff0c5201b00], txn-recover: Recovering log 37 through 37
docker_mongo | 2020-02-07T14:45:27.075+0000 I STORAGE [initandlisten] WiredTiger message [1581086727:75209][1:0x7ff0c5201b00], txn-recover: Set global recovery timestamp: (0,0)
docker_mongo | 2020-02-07T14:45:27.102+0000 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
docker_mongo | 2020-02-07T14:45:27.106+0000 I STORAGE [initandlisten] Timestamp monitor starting
docker_mongo | 2020-02-07T14:45:27.109+0000 I CONTROL [initandlisten]
docker_mongo | 2020-02-07T14:45:27.110+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
docker_mongo | 2020-02-07T14:45:27.110+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
docker_mongo | 2020-02-07T14:45:27.110+0000 I CONTROL [initandlisten]
docker_mongo | 2020-02-07T14:45:27.112+0000 I SHARDING [initandlisten] Marking collection local.system.replset as collection version: <unsharded>
docker_mongo | 2020-02-07T14:45:27.114+0000 I STORAGE [initandlisten] Flow Control is enabled on this deployment.
docker_mongo | 2020-02-07T14:45:27.114+0000 I SHARDING [initandlisten] Marking collection admin.system.roles as collection version: <unsharded>
docker_mongo | 2020-02-07T14:45:27.114+0000 I SHARDING [initandlisten] Marking collection admin.system.version as collection version: <unsharded>
docker_mongo | 2020-02-07T14:45:27.116+0000 I SHARDING [initandlisten] Marking collection local.startup_log as collection version: <unsharded>
docker_mongo | 2020-02-07T14:45:27.116+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
docker_mongo | 2020-02-07T14:45:27.118+0000 I SHARDING [LogicalSessionCacheRefresh] Marking collection config.system.sessions as collection version: <unsharded>
docker_mongo | 2020-02-07T14:45:27.118+0000 I NETWORK [initandlisten] Listening on /tmp/mongodb-27017.sock
docker_mongo | 2020-02-07T14:45:27.118+0000 I NETWORK [initandlisten] Listening on 0.0.0.0
docker_mongo | 2020-02-07T14:45:27.118+0000 I SHARDING [LogicalSessionCacheReap] Marking collection config.transactions as collection version: <unsharded>
docker_mongo | 2020-02-07T14:45:27.118+0000 I NETWORK [initandlisten] waiting for connections on port 27017
docker_spring |
docker_spring | . ____ _ __ _ _
docker_spring | /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
docker_spring | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
docker_spring | \\/ ___)| |_)| | | | | || (_| | ) ) ) )
docker_spring | ' |____| .__|_| |_|_| |_\__, | / / / /
docker_spring | =========|_|==============|___/=/_/_/_/
docker_spring | :: Spring Boot :: (v2.2.1.RELEASE)
docker_spring |
docker_spring | 2020-02-07 14:45:27.552 INFO 1 --- [ main] com.example.demo.MainClass : Starting MainClass v0.0.1-SNAPSHOT on spring with PID 1 (/tmp/app.jar started by root in /)
docker_spring | 2020-02-07 14:45:27.556 INFO 1 --- [ main] com.example.demo.MainClass : No active profile set, falling back to default profiles: default
docker_spring | 2020-02-07 14:45:27.950 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
docker_spring | 2020-02-07 14:45:27.992 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 38ms. Found 1 repository interfaces.
docker_mongo | 2020-02-07T14:45:28.000+0000 I SHARDING [ftdc] Marking collection local.oplog.rs as collection version: <unsharded>
docker_spring | 2020-02-07 14:45:28.364 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
docker_spring | 2020-02-07 14:45:28.372 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
docker_spring | 2020-02-07 14:45:28.372 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.27]
docker_spring | 2020-02-07 14:45:28.425 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
docker_spring | 2020-02-07 14:45:28.426 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 822 ms
docker_spring | 2020-02-07 14:45:28.616 INFO 1 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[docker_mongo:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
docker_mongo | 2020-02-07T14:45:28.643+0000 I NETWORK [listener] connection accepted from 192.168.0.3:53260 #1 (1 connection now open)
docker_mongo | 2020-02-07T14:45:28.649+0000 I NETWORK [conn1] received client metadata from 192.168.0.3:53260 conn1: { driver: { name: "mongo-java-driver|legacy", version: "3.11.2" }, os: { type: "Linux", name: "Linux", architecture: "amd64", version: "5.3.0-28-generic" }, platform: "Java/Oracle Corporation/11.0.6+10" }
docker_spring | 2020-02-07 14:45:28.659 INFO 1 --- [ker_mongo:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:1}] to docker_mongo:27017
docker_spring | 2020-02-07 14:45:28.663 INFO 1 --- [ker_mongo:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=docker_mongo:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 2, 2]}, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=2669943}
docker_spring | 2020-02-07 14:45:28.703 WARN 1 --- [ main] o.s.data.convert.CustomConversions : Registering converter from class java.time.LocalDateTime to class java.time.Instant as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
docker_spring | 2020-02-07 14:45:28.703 WARN 1 --- [ main] o.s.data.convert.CustomConversions : Registering converter from class java.time.Instant to class java.time.LocalDateTime as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
docker_spring | 2020-02-07 14:45:28.717 WARN 1 --- [ main] o.s.data.convert.CustomConversions : Registering converter from class java.time.LocalDateTime to class java.time.Instant as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
docker_spring | 2020-02-07 14:45:28.717 WARN 1 --- [ main] o.s.data.convert.CustomConversions : Registering converter from class java.time.Instant to class java.time.LocalDateTime as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
docker_spring | 2020-02-07 14:45:28.951 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
docker_spring | 2020-02-07 14:45:29.076 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
docker_spring | 2020-02-07 14:45:29.078 INFO 1 --- [ main] com.example.demo.MainClass : Started MainClass in 1.787 seconds (JVM running for 2.142)
docker_spring | 2020-02-07 14:45:29.081 INFO 1 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
docker_spring | 2020-02-07 14:45:29.083 WARN 1 --- [ main] o.s.data.convert.CustomConversions : Registering converter from class java.time.LocalDateTime to class java.time.Instant as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
docker_spring | 2020-02-07 14:45:29.083 WARN 1 --- [ main] o.s.data.convert.CustomConversions : Registering converter from class java.time.Instant to class java.time.LocalDateTime as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
docker_spring | 2020-02-07 14:45:29.085 WARN 1 --- [ main] o.s.data.convert.CustomConversions : Registering converter from class java.time.LocalDateTime to class java.time.Instant as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
docker_spring | 2020-02-07 14:45:29.085 WARN 1 --- [ main] o.s.data.convert.CustomConversions : Registering converter from class java.time.Instant to class java.time.LocalDateTime as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
docker_spring | 2020-02-07 14:45:29.086 INFO 1 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017
docker_spring |
docker_spring | com.mongodb.MongoSocketOpenException: Exception opening socket
docker_spring | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-3.11.2.jar!/:na]
docker_spring | at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:128) ~[mongodb-driver-core-3.11.2.jar!/:na]
docker_spring | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.11.2.jar!/:na]
docker_spring | at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
docker_spring | Caused by: java.net.ConnectException: Connection refused (Connection refused)
docker_spring | at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
docker_spring | at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399) ~[na:na]
docker_spring | at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242) ~[na:na]
docker_spring | at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224) ~[na:na]
docker_spring | at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403) ~[na:na]
docker_spring | at java.base/java.net.Socket.connect(Socket.java:609) ~[na:na]
docker_spring | at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongodb-driver-core-3.11.2.jar!/:na]
docker_spring | at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-3.11.2.jar!/:na]
docker_spring | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-3.11.2.jar!/:na]
docker_spring | ... 3 common frames omitted
docker_spring |
docker_spring | 2020-02-07 14:45:29.119 INFO 1 --- [ main] org.mongodb.driver.cluster : Cluster description not yet available. Waiting for 30000 ms before timing out
docker_spring | 2020-02-07 14:45:59.125 INFO 1 --- [ main] ConditionEvaluationReportLoggingListener :
docker_spring |
docker_spring | Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
docker_spring | 2020-02-07 14:45:59.131 ERROR 1 --- [ main] o.s.boot.SpringApplication : Application run failed
docker_spring |
docker_spring | java.lang.IllegalStateException: Failed to execute CommandLineRunner
docker_spring | at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:787) ~[spring-boot-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:768) ~[spring-boot-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) ~[spring-boot-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at com.example.demo.MainClass.main(MainClass.java:8) ~[classes!/:0.0.1-SNAPSHOT]
docker_spring | at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
docker_spring | at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
docker_spring | at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
docker_spring | at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
docker_spring | at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) ~[app.jar:0.0.1-SNAPSHOT]
docker_spring | at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) ~[app.jar:0.0.1-SNAPSHOT]
docker_spring | at org.springframework.boot.loader.Launcher.launch(Launcher.java:51) ~[app.jar:0.0.1-SNAPSHOT]
docker_spring | at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52) ~[app.jar:0.0.1-SNAPSHOT]
docker_spring | Caused by: org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]
docker_spring | at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:90) ~[spring-data-mongodb-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2902) ~[spring-data-mongodb-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:587) ~[spring-data-mongodb-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.data.mongodb.core.MongoTemplate.insertDocument(MongoTemplate.java:1494) ~[spring-data-mongodb-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:1294) ~[spring-data-mongodb-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:1226) ~[spring-data-mongodb-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:1211) ~[spring-data-mongodb-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at com.example.demo.DemoApplication.run(DemoApplication.java:40) ~[classes!/:0.0.1-SNAPSHOT]
docker_spring | at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:784) ~[spring-boot-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | ... 13 common frames omitted
docker_spring | Caused by: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]
docker_spring | at com.mongodb.internal.connection.BaseCluster.getDescription(BaseCluster.java:182) ~[mongodb-driver-core-3.11.2.jar!/:na]
docker_spring | at com.mongodb.internal.connection.SingleServerCluster.getDescription(SingleServerCluster.java:41) ~[mongodb-driver-core-3.11.2.jar!/:na]
docker_spring | at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:145) ~[mongodb-driver-3.11.2.jar!/:na]
docker_spring | at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:100) ~[mongodb-driver-3.11.2.jar!/:na]
docker_spring | at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:277) ~[mongodb-driver-3.11.2.jar!/:na]
docker_spring | at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:201) ~[mongodb-driver-3.11.2.jar!/:na]
docker_spring | at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1048) ~[mongodb-driver-3.11.2.jar!/:na]
docker_spring | at com.mongodb.client.internal.MongoCollectionImpl.executeInsertOne(MongoCollectionImpl.java:498) ~[mongodb-driver-3.11.2.jar!/:na]
docker_spring | at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:482) ~[mongodb-driver-3.11.2.jar!/:na]
docker_spring | at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:476) ~[mongodb-driver-3.11.2.jar!/:na]
docker_spring | at org.springframework.data.mongodb.core.MongoTemplate.lambda$insertDocument$16(MongoTemplate.java:1500) ~[spring-data-mongodb-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:585) ~[spring-data-mongodb-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | ... 19 common frames omitted
docker_spring |
docker_spring | 2020-02-07 14:45:59.140 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
docker_mongo | 2020-02-07T14:45:59.146+0000 I NETWORK [conn1] end connection 192.168.0.3:53260 (0 connections now open)
docker_spring exited with code 1
It's very simple
MongoClients.create() without any parameters to connect to a MongoDB instance running on localhost on port 27017.
https://mongodb.github.io/mongo-java-driver/3.4/driver-async/tutorials/connect-to-mongodb/#connect-to-a-standalone-mongodb-instance
Spring Boot already connected to MongoDB
docker_spring | 2020-02-07 14:45:28.616 INFO 1 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[docker_mongo:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
docker_mongo | 2020-02-07T14:45:28.643+0000 I NETWORK [listener] connection accepted from 192.168.0.3:53260 #1 (1 connection now open)
docker_mongo | 2020-02-07T14:45:28.649+0000 I NETWORK [conn1] received client metadata from 192.168.0.3:53260 conn1: { driver: { name: "mongo-java-driver|legacy", version: "3.11.2" }, os: { type: "Linux", name: "Linux", architecture: "amd64", version: "5.3.0-28-generic" }, platform: "Java/Oracle Corporation/11.0.6+10" }
docker_spring | 2020-02-07 14:45:28.659 INFO 1 --- [ker_mongo:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:1}] to docker_mongo:27017
docker_spring | 2020-02-07 14:45:28.663 INFO 1 --- [ker_mongo:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=docker_mongo:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 2, 2]}, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=2669943}
Change to this:
#SpringBootApplication
#ComponentScan({"com.example.client","com.example.demo","com.example.model"})
public class DemoApplication implements CommandLineRunner {
#Autowired
private MongoOperations mongoOps;
#Override
public void run(String... args) throws Exception {
mongoOps.insert(new Expenditure("Aldi",10.01, LocalDate.parse("2019-10-05")));
System.out.println(mongoOps.findOne(new Query(Criteria.where("name").is("Aldi")),Expenditure.class));
mongoOps.dropCollection("expenditure");
PopulateExpenditureDB.populateExpenditures(mongoOps);
}
}

Spring Cloud Gateway Failed to start: Unable to find GatewayFilterFactory with name <blank>

Spring Cloud Gateway failed to start, and gives the following exception:
service-gateway | 2020-01-02 13:44:55.910 INFO 22 --- [ main] c.n.discovery.InstanceInfoReplicator : InstanceInfoReplicator onDemand update allowed rate per min is 4
service-gateway | 2020-01-02 13:44:55.915 INFO 22 --- [ main] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1577943895914 with initial instances count: 11
service-gateway | 2020-01-02 13:44:55.916 INFO 22 --- [ main] o.s.c.n.e.s.EurekaServiceRegistry : Registering application SERVICE-GATEWAY with eureka with status UP
service-gateway | 2020-01-02 13:44:55.917 INFO 22 --- [ main] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1577943895917, current=UP, previous=STARTING]
service-gateway | 2020-01-02 13:44:55.923 INFO 22 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_SERVICE-GATEWAY/a5881e80ffd5:service-gateway:1790: registering service...
service-gateway | 2020-01-02 13:44:55.991 INFO 22 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_SERVICE-GATEWAY/a5881e80ffd5:service-gateway:1790 - registration status: 204
service-gateway | 2020-01-02 13:44:56.051 WARN 22 --- [ main] onfigReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'eurekaAutoServiceRegistration'; nested exception is reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.IllegalArgumentException: Unable to find GatewayFilterFactory with name
service-gateway | 2020-01-02 13:44:56.083 INFO 22 --- [ main] com.netflix.discovery.DiscoveryClient : Shutting down DiscoveryClient ...
service-gateway | 2020-01-02 13:44:59.085 INFO 22 --- [ main] com.netflix.discovery.DiscoveryClient : Unregistering ...
service-gateway | 2020-01-02 13:44:59.093 INFO 22 --- [ main] com.netflix.discovery.DiscoveryClient : DiscoveryClient_SERVICE-GATEWAY/a5881e80ffd5:service-gateway:1790 - deregister status: 200
service-gateway | 2020-01-02 13:44:59.104 INFO 22 --- [ main] com.netflix.discovery.DiscoveryClient : Completed shut down of DiscoveryClient
In spring cloud gateway code that used to create filter it says:
GatewayFilterFactory factory = this.gatewayFilterFactories.get(definition.getName());
if (factory == null) {
throw new IllegalArgumentException("Unable to find GatewayFilterFactory with name " + definition.getName());
}
so if it failed to to create the gatewayFilterFactory, the exception should gives a message:
"Unable to find GatewayFilterFactory with name <some name>", But there is no such thing in my log, just "Unable to find GatewayFilterFactory with name ". After 2 days of research and I finally found, there is an error in my config file:
- id: api_assets
uri: lb://BACKEND
predicates:
- Path=/api/assets/**
filters:
- name: AuditFilter
-
A LIST OF FILTER NAMES WITH A BLANK ONE

Resources