Java Spring Boot Docker Deployment with Oracle Made Simple
Learn how to setup docker deployment of your Java Spring Boot application including Oracle Database dependency easily.
Introduction#
In today’s DevOps-driven world, containerization has become a crucial aspect of deploying applications efficiently. Docker enables developers to package applications along with their dependencies, ensuring seamless deployment across different environments.
In this blog, we will walk through the step-by-step process of deploying a Java Spring Boot application on Docker with Oracle Database, including how to tackle the challenge of downloading the OJDBC driver, which is not available through Maven.
Why Use Docker for Java Spring Boot Applications?#
- Consistency: Ensures the application runs the same way across different environments.
- Scalability: Containers can be easily scaled using tools like Kubernetes.
- Isolation: Each application runs in an isolated environment, avoiding dependency conflicts.
- Efficiency: Docker containers are lightweight and resource-efficient compared to VMs.
Prerequisites:#
Before getting started, ensure you have the following installed:
- Java JDK 17+
- Spring Boot Application (Maven/Gradle)
- Docker (Installed and running)
- Oracle Database (Running in Docker or accessible remotely)
- OJDBC Jar (Manually downloaded from Oracle website ↗)
- Basic knowledge of Docker and Spring Boot
Step 1: Create a Simple Spring Boot Application#
If you already have a Spring Boot project, skip this step. Otherwise, create a new one using Spring Initializr:
curl https://start.spring.io/starter.zip -d dependencies=web -o demo.zip
unzip demo.zip -d demo
cd demobashEnsure your pom.xml has the following dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-strarter-web<artifactId>
</dependency>xmlCreate a simple REST controller in /src/main/java/com/example/demo/HelloController.java
@RestController
@RequestMapping(“/api”)
Public class HelloController {
@GetMapping(“helloWorld”)
public String sayHello() {
return “”Hello, Dockerized Spring Boot Application!”;
}
}javaNow, build the application:
mvn clean package -DskipTestsbashStep 2: Download and Add the Oracle JDBC Driver#
Since Oracle’s JDBC driver is not available in public Maven repositories, you must download it manually from Oracle JDBC Downloads page ↗.
Once downloaded, add it to your local Maven repository:
mvn install:install \
-Dfile=ojdbc8.jar \
-DgroupId=com.oracle \
-DartifactId=ojdbc8 \
-Dversion=21.9.0.0 \
-Dpackaging=jarbashThen, include the dependency in your pom.xml:
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>21.9.0.0</version>
</dependency>xmlStep 3: Create a Dockerfile#
A Dockerfile defines the environment needed to run your application. Create a Dockerfile in the project root:
# Use an official OpenJDK runtime as base image
FROM openjdk:17-jdk-slim
# Set working directory in container
WORKDIR /app
# Copy JAR file into the container
COPY target/demo-0.0.1-SNAPSHOT.jar app.jar
# Expose the port the app runs on
EXPOSE 8080
# Run the application
CMD [“java”, “”-jar, “app.jar”]plaintextStep 4: Build and Run the Docker Container#
Now, build your Docker image:
Docker build -t my-spring-app .plaintextRun the container:
Docker run -d -p 8080:8080 —name spring-app my-spring-appbashVerify that the application is running by opening your browser and visiting:
http://localhost:8080/api/hello
Step 5: Run Oracle Database in a Docker Container#
To run an Oracle Database instance in Docker, use:
Docker run -d —name oracle-test \
-p 1521:1521 \
-e ORACLE_SID=ORCL \
-e ORACLE_PBD=ORCLPDB1 \
-e ORACLE_PWD=admin123 \
container-registry.oracle.com/database/enterprise:latestbashUpdate your application.properties file:
spring.datasource.url=jdbc:oracle:thin:@//oracle-db:1521/ORCLPDB1
spring.datasource.username=system
spring.datasource.password=admin123
spring.datasource.driver-class-name=oracle.jdbc.OracleDriverplaintextEnsure both services are running on the same Docker network:
docker network create mynetwork
docker network connect mynetwork spring-app
docker network connect mynetwork oracle-dbplaintextConclusion#
By following this guide, you have successfully Dockerized a Java Spring Boot application, connected it to an Oracle database using Docker, and resolved the OJDBC download challenge.
Key Takeaways#
- Docker ensures portability and scalability for Java applications.
- Using Docker Compose simplifies multi-container deployment.
- Overcoming OJDBC driver challenges is possible with manual integration.