Docker Compose容器无法相互通信

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

Docker Compose containers can't communicate with each other

问题

如何在使用Docker和Docker Compose部署时,解决Spring Boot应用程序、RabbitMQ和MySQL之间的通信问题以及后端应用程序持续重启且无法与RabbitMQ和MySQL建立通信的故障?

Dockerfile:

FROM openjdk:17-alpine
ADD target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

Docker Compose:

version: "3.9"
networks:
  net:
    driver: bridge

services:
  thermostat-app:
    image: myapp
    build: .
    restart: always
    ports:
      - "8080:8080"
    environment:
      MYSQL_USER: root
      MYSQL_PASSWORD: password
      MYSQL_PORT: 3306
    networks:
      - net
  rabbitmq:
    image: rabbitmq:3.9.11-management-alpine
    container_name: rabbitmq
    networks:
      - net
  db:
    image: mysql:latest
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: mysqldb
    networks:
      - net

application.properties:

spring.datasource.url=jdbc:mysql://db:${MYSQL_PORT:3306}/thermostat
spring.datasource.username=${MYSQL_USER:root}
spring.datasource.password=${MYSQL_PASSWORD:password}
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
英文:

How can I troubleshoot communication issues between my Spring Boot application, RabbitMQ, and MySQL when using Docker and Docker Compose for deployment, as my back-end application keeps restarting and fails to establish communication with RabbitMQ and MySQL?

Dockerfile:

<!-- language: lang-docker -->

FROM openjdk:17-alpine
ADD target/*.jar app.jar
ENTRYPOINT [&quot;java&quot;,&quot;-jar&quot;,&quot;/app.jar&quot;]

Docker compose:

<!-- language: lang-yaml -->

version: &quot;3.9&quot;
networks:
  net:
    driver: bridge

services:
  thermostat-app:
    image: myapp
    build: .
    restart: always
    ports:
      - &quot;8080:8080&quot;
    environment:
      MYSQL_USER: root
      MYSQL_PASSWORD: password
      MYSQL_PORT: 3306
    networks:
      - net
  rabbitmq:
    image: rabbitmq:3.9.11-management-alpine
    container_name: rabbitmq
    networks:
      - net
  db:
    image: mysql:latest
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: mysqldb
    networks:
      - net

application.properties:

<!-- language: lang-properties -->

spring.datasource.url=jdbc:mysql://db:${MYSQL_PORT:3306}/thermostat
spring.datasource.username=${MYSQL_USER:root}
spring.datasource.password=${MYSQL_PASSWORD:password}
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

答案1

得分: 1

在容器中,localhost 意味着容器本身。同一Docker网络上的容器可以使用其服务名称作为主机名相互访问。

您已经在数据源URL中完成了这一操作,使用了 db 作为主机名。

您还需要对RabbitMQ进行同样的操作,并将

spring.rabbitmq.host=localhost

更改为

spring.rabbitmq.host=rabbitmq
英文:

In a container, localhost means the container itself. Containers on the same docker network can address each other using their service names as host names.

You've done that with the datasource URL, where you've used db as the host name.

You need to do it for RabbitMQ as well and change

spring.rabbitmq.host=localhost

to

spring.rabbitmq.host=rabbitmq

huangapple
  • 本文由 发表于 2023年4月11日 15:09:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983266.html
匿名

发表评论

匿名网友

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

确定