【问题标题】:Java application doesn't apply enviroment variable inside docker containerJava 应用程序不在 docker 容器内应用环境变量
【发布时间】:2021-08-21 10:38:22
【问题描述】:

我正在尝试在 docker(在 Windows 10 上)中部署我的应用程序,与 Postgres 容器一起编写。当我执行 docker-compose up 时,我看到以下日志:

Starting postgres ... done
Recreating application ... done
Attaching to postgres, application
        postgres |
        postgres | PostgreSQL Database directory appears to contain a database; Skipping initialization
        postgres |
        postgres | 2021-08-20 14:51:49.721 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
        postgres | 2021-08-20 14:51:49.721 UTC [1] LOG:  listening on IPv6 address "::", port 5432
        postgres | 2021-08-20 14:51:49.741 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
        postgres | 2021-08-20 14:51:49.858 UTC [21] LOG:  database system was interrupted; last known up at 2021-08-20 14:50:34 UTC
        postgres | 2021-08-20 14:51:51.363 UTC [21] LOG:  database system was not properly shut down; automatic recovery in progress
        postgres | 2021-08-20 14:51:51.377 UTC [21] LOG:  redo starts at 0/1661A88
        postgres | 2021-08-20 14:51:51.377 UTC [21] LOG:  invalid record length at 0/1661AC0: wanted 24, got 0
        postgres | 2021-08-20 14:51:51.377 UTC [21] LOG:  redo done at 0/1661A88
        postgres | 2021-08-20 14:51:51.471 UTC [1] LOG:  database system is ready to accept connections

然后我的应用程序的容器尝试启动并在横幅“Spring Boot”等之后出现错误:

application | 2021-08-20 14:52:23.440 ERROR 1 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.
application |
application | 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.
application |   at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:303) ~[postgresql-42.2.20.jar!/:42.2.20] 

这是我的 docker-compose.yml

version: "3"

services:
  db:
    image: postgres:11.13-alpine
    container_name: postgres
    ports:
    - 5432:5432
    volumes:
      - ./pgdata:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=my_db
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=root
      - PGDATA=/var/lib/postgresql/data/mnt
    restart: always
  app:
    build: .
    container_name: application
    ports:
      - 8085:8085
    environment:
      - POSTGRES_HOST=db
    restart: always
    links:
      - db

我的 Dockerfile:

FROM openjdk:11
ADD target/my-app.jar my-app.jar
EXPOSE 8085
ENTRYPOINT ["java" , "-jar", "my-app.jar"]

application.properties:

spring.datasource.url=jdbc:postgresql://${POSTGRES_HOST}:5432/my_db
spring.datasource.username=postgres
spring.datasource.password=root
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=none
spring.liquibase.change-log=classpath:liquibase/changelog.xml
logging.level.org.springframework.jdbc.core = TRACE

有什么问题?为什么我的应用程序在 localhost 上寻找 Postgres 而没有应用环境变量?在 docker 容器内,postgres 的主机应该不同,不是吗?我什至尝试将 application.properties 中的 postgres 主机硬编码为 jdbc:postgresql://db:5432/my_db ,但它继续使用 localhost。我该如何解决?

【问题讨论】:

  • 将您的Dockerfile 也添加到问题中
  • @Tarun 已更新。
  • 也许在你的应用程序上你看到的不是它所做的......尝试在连接之前和之后添加日志以显示真实的连接字符串。
  • 为什么不在 docker 本身中构建 jar?您应该使用多阶段构建来构建 jar 文件,然后将其复制到最终图像中
  • 如果您在本地构建 jar,可能会将 POSTGRES_HOST 设置为 localhost 而不是 db。对于初学者,请尝试在本地构建它,但定义 export POSTGRES_HOST=db 并重试。此外,您似乎正在通过卷与容器共享 Postgres 的本地存储。这是故意的吗?

标签: java postgresql docker docker-compose


【解决方案1】:

尝试这样使用

environment:
        POSTGRES_HOST: db 

【讨论】:

    猜你喜欢
    • 2016-03-09
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    • 2015-02-26
    相关资源
    最近更新 更多