运行 Spring Boot 的 Docker 实例与 Postgres 的 Docker 实例。

huangapple go评论69阅读模式
英文:

Running Spring Boot docker instance with Postgres docker instance

问题

我正在尝试运行一个使用Spring Boot的应用程序,该应用程序连接到一个Postgres数据库,方法如下:

docker-compose.yml(用于Postgres):

version: '3'
services:
  postgres-db:
    container_name: postgres-db
    image: postgres:latest
    restart: always
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: my_user
      POSTGRES_PASSWORD: my_password
      POSTGRES_DB: shorten-db

要运行Postgres数据库:

docker-compose up

Dockerfile(用于Spring Boot应用程序):

FROM openjdk:12-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

为了使用Docker运行Spring应用程序,我使用以下步骤:

mvn package

docker build -t url-shorten/url-shorten-docker .

docker run -p 8080:8080 url-shorten/url-shorten-docker

但是,当我运行上述Docker命令时,我收到以下错误:

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.

在Spring的application.properties文件中,我使用以下方式连接到数据库:

spring.datasource.url=jdbc:postgresql://localhost:5432/shorten-db

我认为这个错误是因为Spring Boot应用程序在与数据库不同的容器中运行,因此它无法在localhost上找到数据库。有没有一种将Spring Boot Docker容器连接到DB容器的惯用方法?或者我是否需要访问我的机器的IP地址,并使用该地址来连接在Docker上运行的Postgres数据库?

英文:

I'm attempting to run a Spring Boot app that connects a Postgres DB using:

docker-compose.yml (for Postgres) :

  version: '3'
  services:
  postgres-db:
  container_name: postgres-db
  image: postgres:latest
  restart: always
  ports:
  - "5432:5432"
  environment:
  POSTGRES_USER: my_user
  POSTGRES_PASSWORD: my_password
  POSTGRES_DB: shorten-db

To run the Postgres DB:

docker-compose up

.Dockerfile (for the Spring Boot app) :

  FROM openjdk:12-jdk-alpine
  RUN addgroup -S spring && adduser -S spring -G spring
  USER spring:spring
  ARG JAR_FILE=target/*.jar
  COPY ${JAR_FILE} app.jar
  ENTRYPOINT ["java","-jar","/app.jar"]

In order to run the Spring app using Docker I use:

  mvn package
  
  docker build -t url-shorten/url-shorten-docker .
  
  docker run -p 8080:8080 url-shorten/url-shorten-docker

But I receive the error when starting when running above docker command:

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.

In Spring application.properties I connect to the DB using:

spring.datasource.url=jdbc:postgresql://localhost:5432/shorten-db

I think this error is due to the Spring Boot app is running in a different container to DB so it cannot find the DB on localhost. Is there an idiomatic way of connecting the Spring Book docker container to the DB container. Or do I have do access the IP address of my machine and use this address to connect to the Postgres DB running on Docker?

答案1

得分: 1

在Spring的application.properties中,尝试将DB配置更改为:

spring.datasource.url=jdbc:postgresql://postgres-db:5432/shorten-db

在容器网络中,您需要使用容器名称作为主机。

英文:

In Spring application.properties, try to change DB config to:

spring.datasource.url=jdbc:postgresql://postgres-db:5432/shorten-db

In container networks, You need to use the container name as a host.

答案2

得分: 1

是的,在这种情况下,您不能使用localhost。

英文:

Yes, you can't use localhost in this situation

spring.datasource.url=jdbc:postgresql://postgres-db:5432/shorten-db

答案3

得分: 0

你可以将数据库(DB)容器和应用程序(app)容器都添加到一个Docker网络中,并且将数据源URL中的PostgreSQL主机更改为postgres-db。然后Spring应用程序将与您的数据库(DB)一起工作。

英文:

You can add both DB and app containers to one Docker network and change PostgreSQL host in datasource URL to postgres-db. Then Spring app will work with your DB.

huangapple
  • 本文由 发表于 2020年9月2日 20:51:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63705927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定