Not able to access springboot application running inside a container on MacOs - spring-boot

I have a springboot application running inside a container on macOS Catalina. Logs show that the application is up and running inside the container however I cannot reach it from browser.
I have tried accessing it using :
localhost:8081/
0.0.0.0:8081/ and
127.0.0.1/8081/
But nothing helped. I have also added server.address=0.0.0.0 to my application.properties file but that didn't help either.
This is how I run my container:
docker run --name spring_boot_docker -it --link mysql-dev-server:db -p 8081:8081 demo_docker_kubernetes
Logs show application is running fine:
2020-04-12 00:21:40.149 INFO 1 --- [ main]
o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService
'applicationTaskExecutor'
2020-04-12 00:21:40.632 INFO 1 --- [ main]
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with
context path ''
2020-04-12 00:21:40.637 INFO 1 --- [ main] com.microservice.DemoApplication
: Started DemoApplication in 8.519 seconds (JVM running for 9.391)
2020-04-12 00:21:52.209 INFO 1 --- [nio-8081-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]
: Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-04-12 00:21:52.210 INFO 1 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet
: Initializing Servlet 'dispatcherServlet'
2020-04-12 00:21:52.227 INFO 1 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet
: Completed initialization in 17 ms
When I do docker container ls I get below output under "ports":
0.0.0.0:8081->8081/tcp if that helps anyway?
I have referred almost all other posts that suggest to include server.address=0.0.0.0 in application.properties but as I mentioned that has not helped me.
Any idea what I could be doing wrong here? Thanks

If you run your application Production mode then by default it uses HTTPS and you might be using localhost(HTTP) to access the host.

You need to remove the --link in your docker run to access it.
--link is a deprecated Docker feature, so there may be a docker network being created between those two containers. Rendering your -p 8081:8081 useless.

Related

Could not reach the docker container web application even it run successfully

I wrote a simple backend application by spring boot and kotlin, here you can see the full settings https://github.com/lifeodyssey/demo
this bug performed as
I can start and access the application by ./gradlew bootRun
I can start and access the application by java -jar demo.jar
But I could not access the application when I try to start it in a container, even I can see a successful log by docker logs containerID. The log is given below
2022-11-12 15:50:33.017 INFO 1 --- [ main] com.example.demo.DemoApplicationKt : Starting DemoApplicationKt using Java 11.0.16 on eeb1dfe09e6a with PID 1 (/Demo-0.0.1.jar started by root in /)
2022-11-12 15:50:33.029 INFO 1 --- [ main] com.example.demo.DemoApplicationKt : No active profile set, falling back to 1 default profile: "default"
2022-11-12 15:50:34.315 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2022-11-12 15:50:34.320 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
2022-11-12 15:50:34.346 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 15 ms. Found 0 MongoDB repository interfaces.
2022-11-12 15:50:35.564 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8000 (http)
2022-11-12 15:50:35.595 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-11-12 15:50:35.596 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.68]
2022-11-12 15:50:35.787 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-11-12 15:50:35.788 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2628 ms
2022-11-12 15:50:38.155 WARN 1 --- [ main] o.s.b.a.m.MustacheAutoConfiguration : Cannot find template location: classpath:/templates/ (please add some templates, check your Mustache configuration, or set spring.mustache.check-template-location=false)
2022-11-12 15:50:38.346 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8000 (http) with context path ''
2022-11-12 15:50:38.412 INFO 1 --- [ main] com.example.demo.DemoApplicationKt : Started DemoApplicationKt in 6.588 seconds (JVM running for 7.745)
And here is the Dockerfile
FROM openjdk:11
COPY /build/libs/demo-0.0.1-SNAPSHOT.jar Demo-0.0.1.jar
EXPOSE 8000
ENTRYPOINT ["java","-jar","/Demo-0.0.1.jar"]
Here is the commmand I used to build image and run container
docker build -t demo .
docker run -dp 8000:8000 demo:latest
I could not find where is the problem. Can you help me with it ?
Update
Thanks for the comments below, here is what showed when I access localhost
This site can’t be reached localhost refused to connect.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED
I have tried change -dp 8000 to -d -p 8000, but nothing changed.
Try to connect to it using 127.0.0.1:8000 instead of localhost:8000, maybe localhost is not resolving to 127.0.0.1.
If not try a different port and/or browser. Finally your firewall might be blocking your server so check that.
I finally found the issue.
I installed two application two run my docker. One is colima, another is Docker Desktop.
I guess the problem is that docker do not know which one is the "localhost".
This problem fixed after I uninstall docker desktop and link docker to colima.

How to connect docker db from dockerized spring boot app?

I am beginner on docker stuff and try to learn but I am stuck so hard.
When I only run db from docker-compose.yml and run the spring app called dockerdeneme directly from IDE then it connects to db, no problem. But when I try to run both from docker-compose.yml file then the app cannot connect to db. My docker-compose.yml file is:
version: '3.9'
services:
mysql:
platform: linux/amd64
image: mysql:8
container_name: mysqlcustomerdb
domainname: nishcustomer
hostname: localhost
ports:
- '3306:3306'
volumes:
- ~/apps/mysql:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=1234
- MYSQL_PASSWORD=1234
- MYSQL_DATABASE=mysqlcustomerdb
- DATABASE_HOST=mysqlcustomerdb
- MYSQL_ROOT_HOST=%
- lower_case_table_names=1
- init-connect='GRANT CREATE USER ON . TO 'root'#'%';FLUSH PRIVILEGES;'
dockerdeneme:
container_name: dockerdeneme
domainname: nishcustomer
hostname: localhost
image: dockerdeneme
build: ./
ports:
- '8080:8080'
environment:
- MYSQL_ROOT_PASSWORD=1234
- MYSQL_PASSWORD=1234
- MYSQL_USER=root
- MYSQL_DATABASE=mysqlcustomerdb
depends_on:
- mysql
volumes:
cache:
driver: local
Result of docker ps is:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fb0037e10702 mysql:8 "docker-entrypoint.s…" 22 seconds ago Up 21 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysqlcustomerdb
If I run the application from IDE with following application.properties then it is working good:
spring.datasource.url=jdbc:mysql://localhost:3306/mysqlcustomerdb
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
And it is the output of failing dockerized app:
2022-04-14 21:28:56.106 INFO 1 --- [ main] c.e.d.DockerdenemeApplication : Starting DockerdenemeApplication v0.0.1-SNAPSHOT using Java 11.0.14.1 on localhost with PID 1 (/app.jar started by root in /)
2022-04-14 21:28:56.107 INFO 1 --- [ main] c.e.d.DockerdenemeApplication : No active profile set, falling back to 1 default profile: "default"
2022-04-14 21:28:56.467 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-04-14 21:28:56.493 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 21 ms. Found 1 JPA repository interfaces.
2022-04-14 21:28:57.024 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-04-14 21:28:57.037 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-04-14 21:28:57.037 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.60]
2022-04-14 21:28:57.105 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-04-14 21:28:57.105 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 937 ms
2022-04-14 21:28:57.250 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-04-14 21:28:57.256 WARN 1 --- [ main] com.zaxxer.hikari.util.DriverDataSource : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
2022-04-14 21:28:58.334 ERROR 1 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]
I think I am doing something wrong with port mapping or hostname/domain name but I am stuck.
Can someone show me what am I doing wrong, ports or hostname etc?
localhost don't work for docker-compose apps. As a summary, use the ip of the host or a well configured docker network. Check:
https://stackoverflow.com/a/52213178/3957754
Pay special attention to : extra_hosts:
How to connect API with Web SPA through docker
Here you have a similar docker-compose with mysql and wordpress:
https://github.com/usil/wordpress-docker/blob/master/docker-compose.yml

Can't override server port in jenkins

in windows batch command
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8085
doesn't work
the console outputs:
main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8085 (https) with context path ''
2019-05-20 14:05:45.622 INFO 3144 --- [ main] g.s.i.InternalPortalApplication : Started InternalPortalApplication in 14.031 seconds (JVM running for 28.877)

Running Spring Boot Admin with a non-default port

I want to start the Spring Boot Admin server on a different port than 8080.
So I configured the server.port property in the bootstrap.yml file with 9000, but still the server listens on port 8080 according to the log file.
Here's my bootstrap.yml:
server:
port: 9000
spring:
application:
name: admin-server
cloud:
config:
uri: http://localhost:8888
And this is the final part of the console log:
2018-11-29 15:52:11.242 INFO 25999 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-11-29 15:52:11.271 INFO 25999 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-11-29 15:52:11.274 INFO 25999 --- [ main] n.d.d.c.a.AdminServerApplication : Started AdminServerApplication in 2.826 seconds (JVM running for 3.355)
2018-11-29 15:52:11.818 INFO 25999 --- [on(4)-127.0.0.1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-11-29 15:52:11.819 INFO 25999 --- [on(4)-127.0.0.1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-11-29 15:52:11.830 INFO 25999 --- [on(4)-127.0.0.1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 11 ms
In the application.properties file put this
server.port = 9000
A flashlight in my brain told me I might have forgotten to add the spring-cloud-config-client dependency, after checking, and then adding the appropriate dependency to the pom.xml, it worked like a charm:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
Thanks to all for providing feedback.
Many different configuration sources have precedence over application.properties (or YAML) as described here: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
You might need to verify if one of those sources are not interfering with the value of serve.port.

mvn spring-boot:run vs Run

I'm testing spring-boot-devtools with livereload on intelliJ.
I have simple SpringBootApplication that works fine.
When I start the app from maven command "mvn spring-boot:run" all works fine excepting that livereload server dont' start. No message on console appears and the chrome extensions show error that It's not possible to connect to livereload server.
If I start app with (right mousse button/Run Application.java), all works fine even livereload server. Message appears on console and browser is able to connect to livereload server.
Starting app from right mousse button/Run Application.java
Console message
2016-07-13 16:39:47.947 INFO 10440 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-07-13 16:39:48.026 INFO 10440 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2016-07-13 16:39:48.077 INFO 10440 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-07-13 16:39:48.145 INFO 10440 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8090 (http)
Starting app from mvn command "mvn spring-boot:run"
Chorme extension message:
Could not connect to LiveReload server. Please make sure that a compatible LiveReload server is running. (We recommend guard-livereload, until LiveReload2 comes to your platform.)
Console message. See that not show message LiveReload server is running on port....
2016-07-13 16:38:56.749 INFO 6924 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-07-13 16:38:56.852 INFO 6924 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-07-13 16:38:56.898 INFO 6924 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8090 (http)
Why maven command don't start livereload server?
You need to tell Maven to fork a separate JVM to run your application, rather than it running in the same JVM as Maven:
$ mvn spring-boot:run -Dfork=true

Resources