英文:
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 ["java","-jar","/app.jar"]
Docker compose:
<!-- language: lang-yaml -->
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:
<!-- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论