kaptan
kaptan

Reputation: 23

Spring Boot App + H2 (in Memory) not working with Docker Container

I have a question about a spring boot app + H2 deploying with Docker.

I have build a simple spring boot project with a connection to a H2 in memory DB and I want to deploy the app in a Docker Container. My problem is, that my container is running well, but I can't reach my API via the define localhost and port. When I open in the browser I get the message that there is no connection.

When I start the jar locally everything works fine, but no in my container. I suspect that I'm doing something wrong with my Dockerfile.

Can somebody please help me.

Here are some details what I have done so far:

My app prop.:

server.port=8580
logging.level.org.springframework.web=DEBUG
#spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.url=${DATABASE_SERVER:jdbc:h2:file:/Users/test/Documents/MyProjects/h2-Db/todoAppDb}
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false

My Dockerfile:

FROM amazoncorretto:11.0.8-alpine
VOLUME /app
ADD /target/TodoApp.jar TodoApp.jar
EXPOSE 8580
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/TodoApp.jar"]

My CMD to build the Docker image:

docker build -t todoapi .   

My CMD to run the container:

docker run -e DATABASE_SERVER=jdbc:h2:mem:tmpdb -dp 8580:8080 todoapi

After that my container run and this is the output from container console:

container console

When open the link localhost:8580 my Browser tells me that no connection could be established.

What I'm doing wrong?

Upvotes: 1

Views: 5361

Answers (1)

Roberto Manfreda
Roberto Manfreda

Reputation: 2623

-p 8080:80 Map TCP port 80 in the container to port 8080 on the Docker host.

You are publishing the wrong port.

You should run:

docker run -e DATABASE_SERVER=jdbc:h2:mem:tmpdb -dp 8080:8580 todoapi

Then access it in the host machine as follows

http://localhost:8080

Upvotes: 1

Related Questions