Spring Boot App + H2(在内存中)不能与 Docker Container 一起工作

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

我对使用 Docker 部署的 spring 启动应用程序 + H2 有疑问。

我已经构建了一个简单的 spring 引导项目,它连接到内存数据库中的 H2,我想在 Docker 容器中部署该应用程序。 我的问题是,我的容器 运行ning 很好,但我无法通过定义的本地主机和端口访问我的 API。当我在浏览器中打开时,我收到没有连接的消息。

当我在本地启动 jar 时,一切正常,但在我的容器中没有。我怀疑我的 Docker 文件有问题。

有人可以帮帮我吗。

以下是我到目前为止所做的一些细节:

我的应用道具:

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

我的Docker文件:

enter code here
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"]

我的 CMD 构建 Docker 图像:

docker build -t todoapi .   

我的 CMD 到 运行 容器:

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

之后我的容器 运行 这是容器控制台的输出:

container console

当打开 link localhost:8580 我的浏览器告诉我无法建立连接。

我做错了什么?

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

你是publishing错误的端口。

你应该运行:

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

然后在宿主机中访问如下

http://localhost:8080