简体   繁体   English

使用 docker 时 Spring 无法连接到 postgres

[英]Spring cannot connect to postgres when using docker

I am trying to containerise a Spring Boot project that uses another containerised Postgres database, but it seems the Spring project cannot connect to the database because it cannot resolve the hostname.我正在尝试将使用另一个容器化 Postgres 数据库的 Spring 引导项目容器化,但似乎 Spring 项目无法连接到数据库,因为它无法解析主机名。

Dockerfile: Dockerfile:

FROM maven:3.6.0-jdk-11
WORKDIR /app
COPY . /app
RUN mvn clean install spring-boot:run -q

Docker compose: Docker 组成:

version: "3.7"

services:
  postgres:
    image: postgres
    container_name: db
    expose:
      - "5432"
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: nanobox
    networks:
      - app-network
  
  backend:
    restart: always
    build:
      context: nanobox
    volumes:
      - ./nanobox:/app
    environment:
      - SECRET=bigsecret
    ports:
      - 8081:8081
    networks:
      - app-network
    depends_on:
      - postgres


networks:
  app-network:
    driver: bridge

application.properties:应用程序属性:

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
#debug=false

spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://postgres:5432/nanobox
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.generate-ddl=true
spring.jpa.database=postgresql
spring.jpa.hibernate.ddl-auto=update
#spring.jpa.properties.hibernate.jdbc.lob.not_contextual_creation=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
debug=true
spring.mvc.log-request-details=true
auth.secret={SECRET}
storage.prefix = /tmp/uploads/

docker compose up fails with the following exceptions: docker 组合失败,但有以下例外:

#8 44.67 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
    [..]
#8 44.67 Caused by: org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
    [..]
#8 44.68 Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
    [..]
#8 44.68 Caused by: java.net.UnknownHostException: postgres

It seems that for some reason docker compose refuses to start the postgres container before the Spring application, even the command:似乎由于某种原因 docker compose 拒绝在 Spring 应用程序之前启动 postgres 容器,甚至命令:

docker compose up postgres backend

results in:结果是:

[+] Building 8.3s (8/8) FINISHED                                                                                                                                                                                                                                                                                            
 => [internal] load build definition from Dockerfile                                                                                                                                                                                                                                                                   0.0s
 => => transferring dockerfile: 31B                                                                                                                                                                                                                                                                                    0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                                                                                      0.0s
 => => transferring context: 2B                                                                                                                                                                                                                                                                                        0.0s
 => [internal] load metadata for docker.io/library/maven:3.6.0-jdk-11                                                                                                                                                                                                                                                  1.5s
 => [1/4] FROM docker.io/library/maven:3.6.0-jdk-11@sha256:6a0430ded2cfaba7e16080f4cc097c9c65d1406b3b235d0fcfcfd84c354c4177                                                                                                                                                                                            0.0s
 => [internal] load build context                                                                                                                                                                                                                                                                                      0.0s
 => => transferring context: 18.55kB                                                                                                                                                                                                                                                                                   0.0s
 => CACHED [2/4] WORKDIR /app                                                                                                                                                                                                                                                                                          0.0s
 => CACHED [3/4] COPY . /app    

You're trying to run mvn spring-boot:run from a Dockerfile RUN instruction;您正在尝试运行mvn spring-boot:run from a Dockerfile RUN指令; that happens while you're trying to build the image, not when you go to run the container later.这发生在您尝试构建映像时,而不是当您 go 稍后运行容器时。 (It's similar to the difference between using javac to compile a source file and java to run the built class file.) For reasons beyond the scope of this answer, the build phase can't connect to other containers, even if they're declared in the same docker-compose.yml file. (这类似于使用javac编译源文件和java运行构建的 class 文件之间的区别。)由于 scope 的其他容器之外的原因,即使他们声明的构建阶段无法连接到其他容器,在同一个docker-compose.yml文件中。

Split the Dockerfile into two separate lines to build the application, and then to run it as the main container command:将 Dockerfile 拆分为两个单独的行以构建应用程序,然后将其作为主容器命令运行:

FROM maven:3.6.0-jdk-11
WORKDIR /app
COPY . .      # (don't repeat /app directory name)

# At build time, only compile the application but do not run it
RUN mvn clean install

# When you launch the container, this will be the main command
CMD mvn spring-boot:run

@david-maze 's solution was almost good for my case. @david-maze 的解决方案对我来说几乎是好的。 However, I solved the problem with this following Dockerfile:但是,我通过以下 Dockerfile 解决了这个问题:

FROM maven:3.6.0-jdk-11
WORKDIR /app
COPY . .

# At build time, only compile the application but do not run it
RUN mvn clean package -DskipTests -q

# When you launch the container, this will be the main command
CMD mvn spring-boot:run

also had to add an alias for the database container:还必须为数据库容器添加别名:

postgres:
    image: postgres
    container_name: db
    expose:
      - "5432"
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: nanobox
      networks:
        some-net:
          aliases:
            - auth-module.dev

Maven was running tests that resulted in container launch failure because there was no database. Maven 正在运行导致容器启动失败的测试,因为没有数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker 中的 Postgres 服务器无法连接到 spring 启动应用程序 - Postgres Server in Docker cannot connect to spring boot application 如何使用 docker spring 引导应用程序连接到 heroku postgres? - How to connect to heroku postgres using docker spring boot app? 无法连接到 Docker postgres 容器 - Scala - Cannot Connect to Docker postgres container - Scala 在 Docker 容器内时,Spring 微服务无法通过 Eureka 连接 - Spring Microservices Cannot Connect Via Eureka When Inside Docker Container 无法使用 Netbeans 连接到 heroku postgres 数据库 - Cannot connect to heroku postgres database using Netbeans Spring Boot无法连接到MySQL并在Docker / Docker Compose中退出 - Spring Boot cannot connect to MySQLand exits in Docker/Docker compose Spring 启动,docker 中的 postgres 无法访问本地主机 - Spring boot, postgres inside docker compose cannot reach localhost 如何使用java.sql包连接到Docker上的Postgres? - How to connect to a Postgres on Docker using java.sql package? 无法通过IDE连接到Docker上的Jhipster Spring注册表 - Cannot connect to jhipster spring registry on docker via IDE Spring Boot docker无法连接到mysql(连接被拒绝/ createCommunicationsException) - Spring boot docker Cannot connect to mysql (Connection refused / createCommunicationsException)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM