两个Docker容器之间的通讯出现问题 [英] Trouble communicating between two docker containers
问题描述
我是docker的新手,正在尝试将运行在 boot-example docker容器中的spring boot应用程序连接到运行在 mymysql 中的mysql服务器上端口6603上的docker容器,两者都在同一台物理计算机上运行. 事实是:如果我将spring-boot应用程序连接到 mymysql Docker容器以便与数据库进行通信,则我没有任何错误,并且一切正常. >
当我将自己的spring boot应用程序移到我的 boot-example 容器中并尝试(通过Hibernate)与我的 mymysql 容器通信时,然后我得到这个错误:
2018-02-05 09:58:38.912 ERROR 1 --- [ main] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_111]
我的spring boot application.properties是:
server.port=8083
spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:mysql://localhost:6603/mydockerdb
spring.datasource.username=root
spring.datasource.password=mypassword
在我的春季启动应用在端口8082上的Docker容器中运行之前,它工作正常(在正确构建Docker映像之后):
docker run -it -p 8082:8083 boot-example
按照上面的建议,Docker-compose是一种方法,但是如果您不想使用组合/组合模式.
- 使用
docker network create myNet
轻松创建自己的网络
- 部署容器以侦听已创建的网络
--network myNet
- 将您的spring.datasource.url更改为
jdbc:mysql://mymysql:6603/mydockerdb
通过使用docker demon的DNS解析,容器可以彼此发现并因此可以通信. [默认网桥不支持DNS.使用网桥的用户定义网络.]
有关更多信息: https://docs.docker.com/engine/userguide/联网/
I’m new to docker and I’m trying to connect my spring boot app running into my boot-example docker container to a mysql server running into my mymysql docker container on port 6603, both running on the same phisical machine. The fact is: if I connect my spring-boot app to my mymysql docker container in order to communicate with the database, I get no errors and everything works fine.
When I move my spring boot application into my boot-example container and try to communicate (through Hibernate) to my mymysql container, then I get this error:
2018-02-05 09:58:38.912 ERROR 1 --- [ main] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_111]
My spring boot application.properties are:
server.port=8083
spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:mysql://localhost:6603/mydockerdb
spring.datasource.username=root
spring.datasource.password=mypassword
It works fine until my spring boot app runs in a docker container on port 8082, (after the docker image is correctly built):
docker run -it -p 8082:8083 boot-example
As per above suggestion, Docker-compose is a way but if you don't want to go with compose/swarm mode.
- Simply create your own network using
docker network create myNet
- Deploy your containers listening on a created network
--network myNet
- Change your spring.datasource.url to
jdbc:mysql://mymysql:6603/mydockerdb
By using DNS resolution of docker demon, containers can discover each other and hence can communicate. [DNS is not supported by default bridge. A user-defined network using bridge does.]
For more information: https://docs.docker.com/engine/userguide/networking/
这篇关于两个Docker容器之间的通讯出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!