Spring Docker 容器无法访问 Postgres Docker 容器 [英] Spring Docker container cannot access Postgres Docker container

查看:36
本文介绍了Spring Docker 容器无法访问 Postgres Docker 容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 spring-boot 应用程序的 Dockerfile:

The Dockerfile of my spring-boot app:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY target/media-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

application.yml

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/media
    username: postgres
    password: postgres
    hikari:
      connectionTimeout: 30000

这里是docker-compose.yml:

version: '3'
services:
  db:
    image: postgres
    ports:
      - "5432:5432"
    environment:
      POSTGRES_DB: media
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres

  app:
    build:
      context: ./
      dockerfile: Dockerfile
    depends_on:
      - db
    ports:
      - "8080:8080"

运行 docker-compose up --build 结果:

app_1 |org.postgresql.util.PSQLException:连接到 0.0.0.0:5432拒绝了.检查主机名和端口是否正确,以及postmaster 正在接受 TCP/IP 连接.应用_1

app_1 | org.postgresql.util.PSQLException: Connection to 0.0.0.0:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. app_1

我的猜测是 spring 应用程序尝试在 postgres 准备好之前连接到 postgres,但我得到以下日志:

My guess is that the spring app tries to connect to postgres before postgres is ready, but I get the following log:

db_1 |2019-05-18 19:05:53.692 UTC [1] 日志:数据库系统是准备接受连接

db_1 | 2019-05-18 19:05:53.692 UTC [1] LOG: database system is ready to accept connections

推荐答案

Docker Compose 的主要目的是启动一组 Docker 容器,这些容器将作为独立的实体运行.默认情况下,所有容器都将与所有其他容器建立虚拟网络连接,但您可以根据需要进行更改;您将获得该功能,因为您尚未指定自定义配置.

The main purpose of Docker Compose is to spin up a set of Docker containers, which will then function as independent entities. By default, all containers will have a virtual network connection to all others, though you can change that if you wish; you will get that feature, since you have not specified a custom configuration.

每个容器都会在 Docker 设置的虚拟网络中获得一个虚拟 IP 地址.由于这些是动态的,Docker Compose 通过创建与每个服务对应的内部 DNS 条目使您更容易.因此,您将有两个容器,它们可以分别作为 appdb 寻址,可以是它们自己也可以是对方.如果您安装了 ping,您也可以通过 docker-compose exec 或通过手动创建的 shell 来 ping 这些名称.

Each of the containers will get a virtual IP address inside the virtual network set up by Docker. Since these are dynamic, Docker Compose makes it easier for you by creating internal DNS entries corresponding to each service. So, you will have two containers, which can be addressed as app and db respectively, either from themselves or the other. If you have ping installed, you can ping these names too, either via docker-compose exec, or via a manually-created shell.

因此,正如我们在评论中发现的,您可以从 app 连接到 jdbc:postgresql://db:5432/media,它应该可以工作.

Thus, as we discovered in the comments, you can connect from app to jdbc:postgresql://db:5432/media, and it should work.

这篇关于Spring Docker 容器无法访问 Postgres Docker 容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆