首页 > 解决方案 > 如何从 docker 和本地连接到 docker-compose 端口?

问题描述

我正在使用 spring boot 2.5.3 和 postgreSQL 作为后端。

我希望这个应用程序可以在 docker-compose 上运行,localhost:5000/api/v1/customers也可以从 docker-compose 上运行。

应用程序属性:

server.port=5000
#Database setup
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://localhost:5432/customers
spring.datasource.username=postgres
spring.datasource.password=postgres

这适用于本地主机,与mvn spring-boot:run

但是,当我使用以下 application.properties 设置localhostdb

server.port=5000
#Database setup
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://db:5432/customers
spring.datasource.username=postgres
spring.datasource.password=postgres

Dockerfile

FROM maven:3.8.1-openjdk-11-slim
COPY ./target/customer-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

码头工人-compose.yml

version: '3.1'
services:
  app:
    container_name: customer-container
    image: customer-image:v1
    build: ./
    ports:
      - "5000:5000"
    depends_on:
      - db
  db:
    image: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_DB=customers

localhost:5000/api/v1/customers错误后我无法连接mvn spring-boot:run

org.postgresql.util.PSQLException: Connection to db:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:303) ~[postgresql-42.2.23.jar:42.2.23]
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51) ~[postgresql-42.2.23.jar:42.2.23]
        at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223) ~[postgresql-42.2.23.jar:42.2.23]
        at org.postgresql.Driver.makeConnection(Driver.java:465) ~[postgresql-42.2.23.jar:42.2.23]
        at org.postgresql.Driver.connect(Driver.java:264) ~[postgresql-42.2.23.jar:42.2.23]

尝试创建一个 docker 镜像 docker build -t customer-image:v1 . ,运行docker-compose build docker-compose up没有错误。日志说它在端口 5000 上运行。

customer-container | 2021-08-29 12:43:42.053  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 5000 (http)
customer-container | 2021-08-29 12:43:42.068  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
customer-container | 2021-08-29 12:43:42.069  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.50]
customer-container | 2021-08-29 12:43:42.189  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
customer-container | 2021-08-29 12:43:42.190  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1876 ms
customer-container | 2021-08-29 12:43:42.487  INFO 1 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
customer-container | 2021-08-29 12:43:42.567  INFO 1 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.32.Final
customer-container | 2021-08-29 12:43:42.702  INFO 1 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
customer-container | 2021-08-29 12:43:42.827  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
customer-container | 2021-08-29 12:43:42.970  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
customer-container | 2021-08-29 12:43:42.995  INFO 1 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
customer-container | 2021-08-29 12:43:43.631  WARN 1 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Warning Code: 0, SQLState: 00000
customer-container | 2021-08-29 12:43:43.631  WARN 1 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : table "customer" does not exist, skipping
customer-container | 2021-08-29 12:43:43.642  INFO 1 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
customer-container | 2021-08-29 12:43:43.656  INFO 1 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
customer-container | 2021-08-29 12:43:44.252  WARN 1 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
customer-container | 2021-08-29 12:43:44.991  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 5000 (http) with context path ''
customer-container | 2021-08-29 12:43:45.290  INFO 1 --- [           main] c.p.customer.CustomerApplication         : Started CustomerApplication in 5.654 seconds (JVM running for 6.531)

localhost:5000/api/v1/customers在 docker-compose 启动时有效。

有没有办法我们可以在这一行中为主机设置条件名称?还有其他方法吗?

spring.datasource.url=jdbc:postgresql://db:5432/customers

标签: javaspring-bootdockerdocker-compose

解决方案


那么,您希望能够在本地运行和从 docker 运行吗?

如果是这种情况,您需要将数据库连接字符串提取到您在运行时设置的环境变量

spring.datasource.url = ${DB_HOST}:${DB_PORT}/
spring.datasource.username = ${DB_USERNAME}
spring.datasource.password = ${DB_PASSWD}

或者您可以引入两个从 Spring 配置文件中设置的不同属性文件,您也可以在运行时设置它们;参考SPRING_PROFILES_ACTIVE环境变量


推荐阅读