How to connect docker db from dockerized spring boot app? - spring-boot

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

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.

Not working :: spring.jpa.hibernate.ddl-auto=update

I am trying to create multitenant application with single db and seperate schemas for tenants. And I don't want to loose my data after every startup. This is my application.properties file
logging.level.root=info
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:multi-tenant
spring.jpa.hibernate.ddl-auto=update
spring.liquibase.change-log=classpath:db/changelog/changelog-master.xml
And here is my logs
Connected to the target VM, address: '127.0.0.1:53999', transport: 'socket'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.2)
2022-08-11 13:02:28.342 INFO 25732 --- [ main] c.l.multitenant.MultiTenantApplication : Starting MultiTenantApplication using Java 18.0.1.1 on Manu with PID 25732 (D:\JAVA\multi-tenant - updated\target\classes started by itsma in D:\JAVA\multi-tenant - updated)
2022-08-11 13:02:28.342 INFO 25732 --- [ main] c.l.multitenant.MultiTenantApplication : No active profile set, falling back to 1 default profile: "default"
2022-08-11 13:02:29.534 INFO 25732 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-08-11 13:02:29.613 INFO 25732 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 53 ms. Found 1 JPA repository interfaces.
2022-08-11 13:02:30.507 INFO 25732 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-08-11 13:02:30.522 INFO 25732 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-08-11 13:02:30.522 INFO 25732 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-08-11 13:02:30.742 INFO 25732 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-08-11 13:02:30.742 INFO 25732 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2321 ms
2022-08-11 13:02:30.993 INFO 25732 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-08-11 13:02:31.286 INFO 25732 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2022-08-11 13:02:31.302 INFO 25732 --- [ main] o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:multi-tenant'
2022-08-11 13:02:31.893 INFO 25732 --- [ main] liquibase.database : Set default schema name to PUBLIC
2022-08-11 13:02:32.253 INFO 25732 --- [ main] liquibase.lockservice : Successfully acquired change log lock
2022-08-11 13:02:32.929 INFO 25732 --- [ main] liquibase.changelog : Creating database history table with name: PUBLIC.DATABASECHANGELOG
2022-08-11 13:02:32.929 INFO 25732 --- [ main] liquibase.changelog : Reading from PUBLIC.DATABASECHANGELOG
Running Changeset: db/changelog/changelog-v1.0.xml::1::manu
2022-08-11 13:02:33.102 INFO 25732 --- [ main] liquibase.changelog : SQL in file db/changelog/data/data.sql executed
2022-08-11 13:02:33.102 INFO 25732 --- [ main] liquibase.changelog : ChangeSet db/changelog/changelog-v1.0.xml::1::manu ran successfully in 32ms
2022-08-11 13:02:33.117 INFO 25732 --- [ main] liquibase.lockservice : Successfully released change log lock
2022-08-11 13:02:33.321 INFO 25732 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-08-11 13:02:33.384 INFO 25732 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.10.Final
2022-08-11 13:02:33.525 INFO 25732 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-08-11 13:02:33.682 INFO 25732 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2022-08-11 13:02:34.138 INFO 25732 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-08-11 13:02:34.138 INFO 25732 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-08-11 13:02:34.530 WARN 25732 --- [ 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
2022-08-11 13:02:35.064 INFO 25732 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator'
2022-08-11 13:02:35.143 INFO 25732 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-08-11 13:02:35.174 INFO 25732 --- [ main] c.l.multitenant.MultiTenantApplication : Started MultiTenantApplication in 7.407 seconds (JVM running for 8.296)
2022-08-11 13:02:37.086 INFO 25732 --- [nio-8080-exec-7] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-08-11 13:02:37.086 INFO 25732 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-08-11 13:02:37.087 INFO 25732 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
Why is ddl-auto=update not working?
You use an in memory database configuration.
This means that when the application starts up an embedded database will be created not in permanent memory but in RAM.
This also means that when the application is shut down the memory it took in RAM is cleaned, therefore any information relative with this database is also lost.
H2 database offers another configuration in the url which will make sure that the data that database holds, is not stored in RAM but in permanent storage HDD. This way the application could be shut down and the data which the database contained, will not be lost.
The database however will stop functioning when the application is shut down, as the process that runs the database belongs to the main process of starting your application. But when the application starts up again and database process is started also, the database will see all the data that it contained before the application was shutdown.
The config is the following:
spring.datasource.url= jdbc:h2:/data/multi-tenant;AUTO_SERVER=TRUE
Multiple processes can access the same database without having to
start the server manually. To do that, append ;AUTO_SERVER=TRUE to the
database URL. You can use the same database URL independent of whether
the database is already open or not. This feature doesn't work with
in-memory databases.
Use the same URL for all connections to this database. Internally,
when using this mode, the first connection to the database is made in
embedded mode, and additionally a server is started internally (as a
daemon thread). If the database is already open in another process,
the server mode is used automatically. The IP address and port of the
server are stored in the file .lock.db, that's why in-memory databases
can't be supported.
The application that opens the first connection to the database uses
the embedded mode, which is faster than the server mode. Therefore the
main application should open the database first if possible. The first
connection automatically starts a server on a random port. This server
allows remote connections, however only to this database (to ensure
that, the client reads .lock.db file and sends the random key that is
stored there to the server). When the first connection is closed, the
server stops. If other (remote) connections are still open, one of
them will then start a server (auto-reconnect is enabled
automatically).
All processes need to have access to the database files. If the first
connection is closed (the connection that started the server), open
transactions of other connections will be rolled back (this may not be
a problem if you don't disable autocommit). Explicit client/server
connections (using jdbc:h2:tcp:// or ssl://) are not supported. This
mode is not supported for in-memory databases.
H2 Documentation

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

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.

Docker, Mongodb,Spring Boot on Windows getting Connection refused error

I am using default docker image of MongoDB and trying to connect using simple spring Boot application on windows box.
mongoDB image is getting stared correctly
IP in mongoDB image
I have spring-boot-starter-data-mongodb in my class path. i am using all spring boot default and only using below property in application.properties.
spring.data.mongodb.host=192.168.99.100
On application startup getting Connection refused: connect error
Logs
019-03-06 06:15:06.096 INFO 4664 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[192.168.99.100:27017], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2019-03-06 06:15:06.096 INFO 4664 --- [ main] org.mongodb.driver.cluster : Adding discovered server 192.168.99.100:27017 to client view of cluster
2019-03-06 06:15:06.356 WARN 4664 --- [ main] o.s.d.m.c.m.BasicMongoPersistentProperty : Customizing field name for id property not allowed! Custom name will not be considered!
2019-03-06 06:15:06.928 INFO 4664 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-03-06 06:15:07.164 INFO 4664 --- [68.99.100:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server 192.168.99.100:27017
What i am missing here ?
Not a MongoDB expert, but a moderate Docker user speaking: It looks like you are not publishing the port MongoDB runs by default: 27017. That's why your spring application could not get a connection and throws this exception:
Exception in monitor thread while connecting to server 192.168.99.100:27017
So you may try publishing the port by using -p 27017:27017when initiating mongo container.

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.

Resources