我为我的Spring项目创建了一个Docker Compose和Dockerfile,但在Maven构建阶段失败,因为它正在尝试与Postgres服务建立连接,我不知道如何解决。
这是我的docker-compose文件:
我的PostgreSQL Dockerfile:
我的JDK Dockerfile,我在其中构建了项目(但是失败了),并运行了jar包:
我该怎么修复它?
这是我的docker-compose文件:
version: '3'
services:
postgres:
build:
context: .
dockerfile: docker/postgres/Dockerfile
restart: always
environment:
POSTGRES_DB: ${postgres_database}
POSTGRES_USER: ${postgres_username}
POSTGRES_PASSWORD: ${postgres_password}
ports:
- '5432:5432'
volumes:
- ./pgdata:/var/lib/postgresql/data
api:
build:
context: .
dockerfile: docker/jdk/Dockerfile
restart: always
environment:
postgres_database: ${postgres_database}
postgres_username: ${postgres_username}
postgres_password: ${postgres_password}
ports:
- '8100:8080'
depends_on:
- postgres
我的PostgreSQL Dockerfile:
FROM postgres:11-alpine
ENV POSTGRES_DB: ${POSTGRES_DB}
ENV POSTGRES_USER: ${POSTGRES_USER}
ENV POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
COPY ./docker/postgres/init.sql /docker-entrypoint-initdb.d
EXPOSE 5432
我的JDK Dockerfile,我在其中构建了项目(但是失败了),并运行了jar包:
FROM maven:3.6.0-jdk-11-slim AS build
COPY ./src /usr/src/app/src
COPY ./pom.xml /usr/src/app
RUN mvn -f /usr/src/app/pom.xml clean package
FROM adoptopenjdk/openjdk11:jre11u-alpine-nightly
COPY --from=build /src/usr/src/app/target/logger_service-0.0.1-SNAPSHOT.jar /usr/src/app/app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/usr/app/app.jar"]
错误日志:
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 14.844 s <<< FAILURE! - in cl.getcolors.loggerservice.LoggerServiceApplicationTests
[ERROR] contextLoads(cl.getcolors.loggerservice.LoggerServiceApplicationTests) Time elapsed: 0.014 s <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
Caused by: org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
Caused by: java.net.UnknownHostException: postgres
我该怎么修复它?