我正在运行一个项目,该项目使用 Docker 将 war 文件部署到 Wildfly 服务器。 我计划使用 Wildfly 27.0.1.Final 和 Postgres 9.4。
这是我的 docker-compose.yml 文件:
version: '2'
services:
database:
image: postgres:9.4
ports:
- "5433:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=123456
- POSTGRES_DB=postgres
networks:
- resolute
wildfly:
depends_on:
- database
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
- "9990:9990"
environment:
- DB_HOST=database
- DB_NAME=postgres
- DB_USER=postgres
- DB_PASS=123456
networks:
- resolute
command: /opt/jboss/wildfly/bin/standalone.sh -c standalone-full.xml -b 0.0.0.0 -bmanagement 0.0.0.0
networks:
resolute:
name: resolute
还有我的用于构建 Wildfly 容器的 Dockerfile:
FROM quay.io/wildfly/wildfly:27.0.1.Final-jdk17
COPY ./module.xml /opt/jboss/wildfly/modules/system/layers/base/org/eclipse/persistence/main
COPY ./eclipselink-4.0.0.jar /opt/jboss/wildfly/modules/system/layers/base/org/eclipse/persistence/main
COPY ./standalone-full.xml /opt/jboss/wildfly/standalone/configuration
ADD ./postgresql-42.6.0.jar /opt/jboss/wildfly/standalone/deployments/
ADD ./my-war-file.war .
RUN /opt/jboss/wildfly/bin/add-user.sh -u 'admin' -p 'admin'
我的数据源在standalone-full.xml中
<datasource jndi-name="java:/PostgresDS" pool-name="PostgresDS" enabled="true">
<connection-url>
jdbc:postgresql://host.docker.internal:5432/postgres
</connection-url>
<driver-class>
org.postgresql.Driver
</driver-class>
<datasource-class>
org.postgresql.ds.PGSimpleDataSource
</datasource-class>
<driver>
postgresql-42.6.0.jar
</driver>
<security>
<user-name>
postgres
</user-name>
<password>
123456
</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker" />
<background-validation>
true
</background-validation>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter" />
</validation>
</datasource>
然后我运行
docker-compose up --build
来构建。
容器启动正常,postgres42.6.0部署成功。
然后我进入刚刚创建的 Wildfly 容器,将我的 war 文件复制到
/opt/jboss/wildfly/standalone/deployments/
容器继续运行以部署我的 war 文件,但出现此错误。
2023-08-22 14:30:30 07:30:30,507 ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 2) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "my-war-file.war")]) - failure description: {"WFLYCTL0080: Failed services" => {
2023-08-22 14:30:30 "jboss.deployment.unit.\"my-war-file.war\".component.DataLoader.START" => "java.lang.IllegalStateException: WFLYEE0042: Failed to construct component instance
2023-08-22 14:30:30 Caused by: java.lang.IllegalStateException: WFLYEE0042: Failed to construct component instance
2023-08-22 14:30:30 Caused by: jakarta.ejb.EJBException: jakarta.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 4.0.0.v202210051929): org.eclipse.persistence.exceptions.DatabaseException
2023-08-22 14:30:30 Internal Exception: java.sql.SQLException: jakarta.resource.ResourceException: IJ000453: Unable to get managed connection for java:/PostgresDS
2023-08-22 14:30:30 Error Code: 0
2023-08-22 14:30:30 Caused by: jakarta.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 4.0.0.v202210051929): org.eclipse.persistence.exceptions.DatabaseException
2023-08-22 14:30:30 Internal Exception: java.sql.SQLException: jakarta.resource.ResourceException: IJ000453: Unable to get managed connection for java:/PostgresDS
2023-08-22 14:30:30 Error Code: 0
2023-08-22 14:30:30 Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 4.0.0.v202210051929): org.eclipse.persistence.exceptions.DatabaseException
2023-08-22 14:30:30 Internal Exception: java.sql.SQLException: jakarta.resource.ResourceException: IJ000453: Unable to get managed connection for java:/PostgresDS
2023-08-22 14:30:30 Error Code: 0
2023-08-22 14:30:30 Caused by: java.sql.SQLException: jakarta.resource.ResourceException: IJ000453: Unable to get managed connection for java:/PostgresDS
2023-08-22 14:30:30 Caused by: jakarta.resource.ResourceException: IJ000453: Unable to get managed connection for java:/PostgresDS
2023-08-22 14:30:30 Caused by: jakarta.resource.ResourceException: IJ031084: Unable to create connection
2023-08-22 14:30:30 Caused by: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
2023-08-22 14:30:30 Caused by: java.net.ConnectException: Connection refused",
2023-08-22 14:30:30 "jboss.deployment.unit.\"my-war-file.war\".component.AddrSearcherStartUp.START" => "java.lang.IllegalStateException: WFLYEE0042: Failed to construct component instance
2023-08-22 14:30:30 Caused by: java.lang.IllegalStateException: WFLYEE0042: Failed to construct component instance
2023-08-22 14:30:30 Caused by: jakarta.ejb.EJBException: javax.xml.ws.WebServiceException: Failed to access the WSDL at: http://localhost:9899/addr_searcher?wsdl. It failed with:
2023-08-22 14:30:30 Got Connection refused while opening stream from http://localhost:9899/addr_searcher?wsdl.
2023-08-22 14:30:30 Caused by: javax.xml.ws.WebServiceException: Failed to access the WSDL at: http://localhost:9899/addr_searcher?wsdl. It failed with:
2023-08-22 14:30:30 Got Connection refused while opening stream from http://localhost:9899/addr_searcher?wsdl.
2023-08-22 14:30:30 Caused by: java.io.IOException: Got Connection refused while opening stream from http://localhost:9899/addr_searcher?wsdl
2023-08-22 14:30:30 Caused by: java.net.ConnectException: Connection refused"
2023-08-22 14:30:30 }}
我应该如何修复这个错误?非常感谢。
host.docker.internal
指的是docker主机。因此,如果您在本地进行此操作,请以您的笔记本电脑为例。您正在尝试通过主机上的端口从另一个容器访问一个容器。这意味着您需要使用您在主机上实际公开的端口,您已经在此处完成了:
ports:
- "5433:5432"
因此您需要将配置调整为如下所示:
<datasource jndi-name="java:/PostgresDS" pool-name="PostgresDS" enabled="true">
<connection-url>
jdbc:postgresql://host.docker.internal:5433/postgres
</connection-url>
但是没有理由在 docker-compose 堆栈中执行此操作。您可以直接引用其他容器和容器端口,如下所示:
<datasource jndi-name="java:/PostgresDS" pool-name="PostgresDS" enabled="true">
<connection-url>
jdbc:postgresql://database:5432/
</connection-url>