简体   繁体   English

众所周知的“Docker容器看不到彼此”的问题

[英]The well-known 'Docker containers don't see each other' problem

I checked many forum entries (eg in stackoverflow too) but I still cannot figure out what the problem is with my docker-compose file. 我检查了很多论坛条目(例如在stackoverflow中)但我仍然无法弄清楚我的docker-compose文件有什么问题。

So when I start my application ( content-app ) I got the following exception: 因此,当我启动我的应用程序( 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)

My application is a Spring boot app that tries to connect to the database, the JDBC URL is 我的应用程序是一个Spring启动应用程序,尝试连接到数据库,JDBC URL是

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

The Spring Boot app works fine as locally (when no docker is used) can connect to the local mariadb. Spring Boot应用程序可以正常工作(当没有使用docker时)可以连接到本地mariadb。
So the content-app container don't see the content-database container. 因此content-app容器看不到content-database容器。 I read that if I specify a network and I assign the containers to the network then they should be able to connect to each other. 我读到如果我指定一个网络并将容器分配给网络,那么它们应该能够相互连接。

When I connect to the running content-app container then I can telnet to content-database 当我连接到正在运行的content-app容器时,我可以telnet到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.

My docker-compose yaml file: 我的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"

Any hint please? 有什么提示吗?
Thanks! 谢谢!

您必须在./database/Dockerfile的Dockerfile中./database/Dockerfile内容数据库正在侦听的./database/Dockerfile

I guess MariaDB is listening on default port 3307 , this means your application has to connect to this port as well. 我猜MariaDB正在监听默认端口3307 ,这意味着你的应用程序也必须连接到这个端口。 I guess this is the case as you are mapping the port 3307 of your container to "the outside". 我想这就是你正在将容器的端口3307映射到“外部”的情况。

Change the port in your connection string: 更改连接字符串中的端口:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 docker 同一网络上的容器(来自撰写)看不到对方 - docker containers on the same net (from compose) don't see each other Docker 容器在 docker compose 中无法看到彼此,主机也无法看到 - Docker containers are not able to see each other in docker compose, nor is host docker撰写网络版本3 –除非使用公共IP,否则容器无法互相看到? - docker compose network version 3 – containers can't see each other unless using public IP? 让docker容器按名称相互查看(ping)的问题 - Problems getting docker containers to see (ping) each other by name IdentityServer4:无法从以下位置获取配置:&#39;{servicename} .well-known / openid-configuration&#39;。 码头工人问题 - IdentityServer4: Unable to obtain configuration from: '{servicename}.well-known/openid-configuration'. docker issue 码头工人组成 | 为什么容器不能互相到达? - Docker-Compose | why the containers can't reach each other? 链接的docker容器无法相互通信 - Linked docker containers can't communicate with each other 为什么Docker容器无法相互通信? - Why Docker containers can't communicate with each other? 如何设置两个 docker 容器,以便它们可以相互看到? - How do I setup two docker containers, such that they can see each other? 为什么我的Docker容器不互相通信 - Why will my Docker containers not talk to each other
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM