[英]Tomcat app deployed on Docker: can connect to Postgres, but new data doesn't show up in database
I have deployed a Tomcat app to Docker (using Windows) and in my terminal it shows that it connected to the database:我已经将 Tomcat 应用程序部署到 Docker(使用 Windows),并在我的终端中显示它已连接到数据库:
PostgreSQL init process complete; ready for start up.
app-db_1 |
app-db_1 | LOG: database system was shut down at 2021-04-25 19:15:05 UTC
app-db_1 | LOG: MultiXact member wraparound protections are now enabled
app-db_1 | LOG: autovacuum launcher started
app-db_1 | LOG: database system is ready to accept connections
The app uses JPA with Hibernate.该应用程序使用 JPA 和 Hibernate。 I already have a database on my host and we interact nicely when we are not on Docker.
我的主机上已经有一个数据库,当我们不在 Docker 上时,我们可以很好地交互。 I call persist on an entity and there is no exception thrown.
我在一个实体上调用persist并且没有抛出异常。 And otherwise behavior is expected.
否则行为是预期的。 But when I run a
SELECT * FROM users;
但是当我运行
SELECT * FROM users;
in pgAdmin, the new entity does not appear in the database.在 pgAdmin 中,新实体不会出现在数据库中。
Here is docker-compose:这是 docker-compose:
app-web:
build: .
ports:
- "8080:8080"
links:
- app-db
app-db:
build: ./db
expose:
- "5432"
app-db-data:
image: cogniteev/echo
command: echo 'Data Container for PostgreSQL'
volumes:
- /var/lib/postgresql/data
I have also tried having as volumes postgres_data:/var/lib/postgresql/data/
without having app-db-data but I was getting the same result.我也尝试过在没有 app-db-data 的情况下将
postgres_data:/var/lib/postgresql/data/
作为卷,但我得到了相同的结果。
This is Dockerfile for web app:这是 web 应用程序的 Dockerfile:
FROM tomcat:10.0.5-jdk8-openjdk
MAINTAINER test
EXPOSE 8080
RUN rm -rf /usr/local/tomcat/webapps/*
COPY ./target/timetraveling-microservice-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/ROOT.war
CMD ["catalina.sh","run"]
And in persistence.xml I have:在 persistence.xml 我有:
...
<property name="hibernate.connection.url" value="jdbc:postgresql://app-db:5432/my-database" />
...
<property name="hibernate.hbm2ddl.auto" value="update" />
...
And the Dockerfile for the database is而数据库的 Dockerfile 是
FROM postgres:latest
MAINTAINER test
ENV POSTGRES_USER user
ENV POSTGRES_PASSWORD password
ENV POSTGRES_DB my-database
I didn't find many resources online to help me because they use Spring and I am not allowed to use Spring.我没有在网上找到很多资源来帮助我,因为他们使用 Spring 而我不允许使用 Spring。 I am obviously new to Docker:( So why does the persisting work fine without throwing exceptions, but the actual data doesn't show up in the database I kindly created for my web app?
我显然是 Docker 的新手 :( 那么为什么持久化工作正常而不抛出异常,但实际数据没有出现在我为我的 web 应用程序创建的数据库中?
EDIT:编辑:
When I run docker ps:当我运行 docker ps 时:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4a67709429b6 timetraveling-microservice_app-web "catalina.sh run" 32 minutes ago Up 32 minutes 0.0.0.0:8080->8080/tcp timetraveling-microservice_app-web_1
74f1c1d1cc40 timetraveling-microservice_app-db "docker-entrypoint.s…" 32 minutes ago Up 32 minutes 5432/tcp timetraveling-microservice_app-db_1
pg-admin doesnt have any access to the docker private network in which containers run and communicate. pg-admin 无权访问容器在其中运行和通信的 docker 专用网络。 If you want to be able to access app-db you should use
ports: -5432:5432
in ur docker-compose for that ( expose
doesnt publish ports in latest versions of docker and is used for informative purposes about the image).如果您希望能够访问 app-db,您应该在您的 docker-compose 中使用
ports: -5432:5432
(在 docker 的最新版本中expose
不发布端口,用于提供有关图像的信息)。
app-db:
build: ./db
ports:
- 5432:5432
for reference https://docs.docker.com/compose/networking/供参考https://docs.docker.com/compose/networking/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.