I am trying to create a container with 2 services: one Oracle db service and one Java service that connects to the database. to make the java service start after the db is completely configured i added the depend_on: service_healthy
clause in my compose.yaml
. Both services are defined in 2 different dockerfiles witch are in the same directory as the compose.yaml.
The problem i face is that the Oracle service starts unhealty and blocks the other java service. Also I wanted to know ho to configure the hibernate.cfg.xlm
to connect correctly the java application that is running inside the container.
My compose.yaml
services:
oracle-xe:
build:
context: .
dockerfile: DockerfileOracle.yaml
container_name: oracle-xe
ports:
- "1521:1521"
environment:
- ORACLE_PASSWORD=admin
- ORACLE_ALLOW_REMOTE=true
- ORACLE_DISABLE_ASYNCH_IO=true
- ORACLE_ENABLE_XDB=true
- ORACLE_ENABLE_GLOBAL_DBNAME=true
command: ADD requests.sql /docker-entrypoint-initdb.d/
volumes:
- ./requests.sql:/data/application/requests.sql
healthcheck:
test: ["CMD", "sh", "-c", "echo 'SELECT * FROM DUAL;' | sqlplus -S system/oracle@oracle-xe:1521/xe | grep -q 'DUMMY'"]
interval: 30s
timeout: 10s
retries: 3
java:
ports:
- "8080:8080"
build:
context: .
dockerfile: DockerfileJava.yaml
depends_on:
oracle-xe:
condition: service_healthy
My DockerfileJava.yaml
# Usa un'immagine di base che contiene JDK 21
FROM openjdk:21
# Imposta la directory di lavoro all'interno del container
WORKDIR /app
# Copia il resto del codice sorgente nell'immagine
COPY provaYaml-1.0-SNAPSHOT.jar .
# Esegue la compilazione dell'applicazione con Maven
#RUN mvn package
# Comando di default per avviare l'applicazione quando il container viene avviato
CMD ["java", "-jar", "provaYaml-1.0-SNAPSHOT.jar"]
My DockerfileOracle.yaml
# Usa un'immagine di Oracle XE già preparata
FROM gvenzl/oracle-xe
# Copia il file SQL nella directory del container
COPY requests.sql /docker-entrypoint-initdb.d/
# Avvia Oracle XE all'avvio del container
CMD ["sh", "-c", "/usr/sbin/startup.sh && sleep infinity"]
My hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"https://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@oracle-xe:1521:XE</property>
<property name="connection.username">METEOAPI</property>
<property name="connection.password">admin</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<mapping class="com.baeldung.api.model.Response" />
<mapping class="com.baeldung.api.model.Request" />
</session-factory>
</hibernate-configuration>