I have a little web servlet application and I would like to initialize and reach my h2 database with tcp and docker-compose but somehow it doesn’t work.
I have this Dockerfile:
FROM maven:3.9.7-eclipse-temurin-11 AS builder
WORKDIR /app
COPY . /app
RUN mvn clean package
FROM tomcat:9.0.89-jre11-temurin-jammy
COPY --from=builder /app/target/*.war $CATALINA_HOME/webapps/ROOT.war
COPY --from=builder /app/target/classes/h2init_ddl_dml.sql $CATALINA_HOME
EXPOSE 8080
CMD ["catalina.sh", "run"]
And this is docker-compose.yml:
services:
db:
image: oscarfonts/h2:2.2.224
container_name: data-database
hostname: data-database
restart: no
environment:
H2_OPTIONS: '-tcp'
ports:
- 1521:1521
volumes:
- ./h2init_ddl_dml.sql:/docker-entrypoint-initdb.d/h2init_ddl_dml.sql
app:
build: .
ports:
- 8080:8080
container_name: data-application
depends_on:
- db
In my maven java web application I would try to use this connection url in my application:
jdbc:h2:tcp://data-database:1521/servlets;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1;
If I use only Dockerfile with in-memory database, with this url, then it works:
jdbc:h2:mem:18_servlets;DATABASE_TO_UPPER=FALSE;INIT=RUNSCRIPT FROM './h2init_ddl_dml.sql';
I tried to check related questions here but I wasn’t able to find the solution in them.
I don’t know whether it is enough information, please let me know if something else could be useful for finding what causes the problem.
Thank you very much for your help in advance.