[英]Docker compose connection between java and mysql container not possible
I have an java spring / hibernate application and I cant create a connection beween the mysql and java containers.我有一个 java spring/hibernate 应用程序,我无法在 mysql 和 java 容器之间创建连接。 When I start just the mysql Container and run the java application manualy in CMD it works fine.
当我只启动 mysql 容器并在 CMD 中手动运行 java 应用程序时,它工作正常。 But once the java application runs in a container I get the following problem:
但是一旦 java 应用程序在容器中运行,我就会遇到以下问题:
bookAPI | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
bookAPI |
bookAPI | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
bookAPI | at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
bookAPI | at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
If I understand the error correctly the java container can send to the mysql container but not the other way around.如果我正确理解错误,java 容器可以发送到 mysql 容器,但不能反过来。 I have linked the java container to the mysql container.
我已将 java 容器链接到 mysql 容器。 Anyone knows how to connect these containers correctly?
有人知道如何正确连接这些容器吗?
My docker-compose.yml looks like this:我的 docker-compose.yml 看起来像这样:
version: '3'
services:
db:
image: mysql:5.7
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: aaaaaa
MYSQL_DATABASE: bookAPI
MYSQL_USER: aaaaaa
MYSQL_PASSWORD: aaaaaa
ports:
- 3306:3306
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
links:
- db
environment:
PMA_HOST: db
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
ports:
- 8081:80
java:
container_name: bookAPI
image: openjdk:8
depends_on:
- db
volumes:
- ./:/app
working_dir: /app
command: bash -c "cd /app && java -jar bookAPI.jar"
stdin_open: true
tty: true
ports:
- 8080:8080
links:
- db
You can also directly override the applications.properties with docker-compose environment variables like this :您还可以使用 docker-compose 环境变量直接覆盖 applications.properties,如下所示:
java:
container_name: bookAPI
image: openjdk:8
volumes:
- ./:/app
working_dir: /app
command: bash -c "cd /app && java -jar bookAPI.jar"
stdin_open: true
tty: true
environment:
spring.datasource.url="jdbc:mysql://db:3306/bookAPI""
links:
- db
ports:
- 8080:8080
I would recomment you tu use depends_on db instead of links我建议你使用depends_on db而不是链接
depends_on:
- db
Okay, I solved the problem.好的,我解决了这个问题。 To connect the spring application with another docker container you need to change the application.properties url value from localhost or ip to the docker maysql container name.
要将 spring 应用程序与另一个 docker 容器连接,您需要将 application.properties url 值从 localhost 或 ip 更改为 docker maysql 容器名称。 Thats tricky while running it localy or run maven install so the best pracice is to overwrite it while starting the application in container.
在本地运行它或运行 maven install 时这很棘手,因此最好的做法是在容器中启动应用程序时覆盖它。
java:
container_name: bookAPI
image: openjdk:8
volumes:
- ./:/app
working_dir: /app
command: bash -c "cd /app && java -jar bookAPI.jar --spring.datasource.url="jdbc:mysql://db:3306/bookAPI""
stdin_open: true
tty: true
links:
- db
ports:
- 8080:8080
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.