【问题标题】:The well-known 'Docker containers don't see each other' problem众所周知的“Docker 容器看不到彼此”问题
【发布时间】:2019-03-22 18:47:43
【问题描述】:

我检查了许多论坛条目(例如在 stackoverflow 中),但我仍然无法弄清楚我的 docker-compose 文件有什么问题。

所以当我启动我的应用程序 (content-app) 时,我遇到了以下异常:

Failed to obtain JDBC Connection; nested exception is java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=content-database)(port=3306)(type=master) : Connection refused (Connection refused)

我的应用程序是一个尝试连接数据库的 Spring boot 应用程序,JDBC URL 是

url: jdbc:mariadb://content-database:3306/contentdb?autoReconnect=true

Spring Boot 应用程序运行良好,因为本地(不使用 docker 时)可以连接到本地 mariadb。
所以content-app 容器看不到content-database 容器。我读到如果我指定一个网络并将容器分配给网络,那么它们应该能够相互连接。

当我连接到正在运行的content-app 容器时,我可以远程登录到content-database

root@894628d7bdd9:/# telnet content-database 3306
Trying 172.28.0.3...
Connected to content-database.
Escape character is '^]'.
n
5.5.5-10.4.3-MariaDB-1:10.4.3+maria~bionip/4X@wW/�#_9<b[~)N.:ymysql_native_passwordConnection closed by foreign host.

我的 docker-compose yaml 文件:

version: '3.3'
networks: 
  net_content:
services:
  content-database:
    image: content-database:latest
    build:
      context: .
      dockerfile: ./database/Dockerfile 
    networks: 
    - net_content
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
  content-redis:
    image: content-redis:latest
    build:
      context: .
      dockerfile: ./redis/Dockerfile 
    networks: 
    - net_content
  content-app:
    image: content-app:latest
    build:
      context: .
      dockerfile: ./content/Dockerfile 
    networks: 
      - net_content
    depends_on:
    - "content-database"

有什么提示吗?
谢谢!

【问题讨论】:

    标签: docker docker-compose


    【解决方案1】:

    我猜 MariaDB 正在监听 默认端口 3307,这意味着您的应用程序也必须连接到该端口。我想这是因为您将容器的端口 3307 映射到“外部”。

    更改连接字符串中的端口:

    url: jdbc:mariadb://content-database:3307/contentdb?autoReconnect=true
    

    【讨论】:

    • 对不起,我的错我忘了删除ports声明,它不在yaml文件中。
    【解决方案2】:

    您必须在 ./database/Dockerfile 的 Dockerfile 中公开 content-database 正在侦听的端口

    【讨论】:

    • 对不起,我的错我忘了删除ports声明,它不在yaml文件中。
    • 那么content-database在哪个端口监听?你会在 Dockerfile 中公开那个端口吗?
    • 是标准的3306,暴露在那里
    • 好的,net_content 是哪种(驱动程序)?
    • 这是一座桥梁。
    猜你喜欢
    • 2019-12-08
    • 2023-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    相关资源
    最近更新 更多