I have a spring framework web application and I’m trying to deploy it into production using tomcat with docker image.
I searched about this error and found out there are two possible errors. The first one is incorrect database url format and I check mine and it’s correct. The database url is like this
jdbc:postgresql://localhost:5432/postgres?currentSchema=volga&stringtype=unspecified
The second possible problem is the Postgres db library file is not included in war file. So I run bash command inside the running tomcat container and check for Postgres library file and it exists in the war file. I also tried to copy the Postgres library into tomcat lib folder and it’s not working.
I create a repo with minimum code to reproduce the error.
https://github.com/ap30422/spring-boot.git
Please clone the repo and change directory to compete
directory.
Run mvn clean install package
to build the app.
Then run Docker build .
to build the image.
Run Docker image with docker run #insert_image_id. -p 8080:8080
If you check http://localhost:8080, you will see the Down
text and the error log in the docker container log.
2
Answers
The current issue you’re encountering may be attributed to either the absence of JDBC drivers or misconfigurations in your Spring Boot application or Tomcat Docker image. Here are a few steps you can follow to address and potentially resolve the problem:
1. Verify Dependency Management.
Confirm that the PostgreSQL JDBC driver is included in your pom.xml file as a dependency. If it’s not already there, add the following lines
2. Check the Classpath in Spring Boot.
While Spring Boot should automatically load the JDBC driver into the classpath, you can explicitly specify the driver class in either the application.properties or application.yml file
3. Verify Database Connection URL
Ensure that the database connection URL is correct. If the database is on a different server, use the appropriate hostname or IP address. Also, confirm that Tomcat can resolve "localhost" if that’s part of the URL.
4. Docker Image Build
When creating the Docker image, make sure to copy the PostgreSQL JDBC driver to the correct location within the image. For instance, you can copy it to a "lib" folder. Modify your Dockerfile as follows:
By following these steps, you should be able to address potential issues related to JDBC drivers and configurations in your Spring Boot application and Tomcat Docker image.
The problem is that you are trying very hard to work around all the things that Spring Boot offers.
DataSource
but try to do things yourself.Use the frameworks and tools you have to there advantage instead of working around things.
Use the Spring Boot
DataSource
In your
application.properties
(should be insrc/main/resources
) add the followingNext modify your
HelloController
to use aDataSource
or even better aJdbcTemplate
.Now you are properly using the Spring Boot configured
DataSource
and Spring Boot will load the correct driver (if it is on the classpath).The application will probably fail to start as I doubt that in the container you are building for this application there is a PostgreSQL database available.
Use embedded tomcat
Spring Boot already provides Tomcat as the default provider and should be used to run it. Change your applicatation to a
jar
and not awar
. And modify yourDockerfile
to launch the application instead of using Tomcat and deploy the application.This will just launch the application you have created and utilize the embedded tomcat instance.
NOTE: This is not the best Dockerfile for a Spring Boot application, you want to use more layers so you can enable reuse. See the Spring Boot Docker Guide.