春季启动到mysql docker通讯 [英] spring boot to mysql docker communcation

查看:84
本文介绍了春季启动到mysql docker通讯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 spring boot 应用程序。当我在Eclipse中运行该应用程序时,可以从浏览器中访问我的服务并获得所需的响应。

I have created a spring boot app. When I run the app inside Eclipse, I can hit my service from a browser and get the response that I want.

我正在构建此容器:

FROM java:8-jre
VOLUME /tmp
RUN mkdir -p /conf
COPY application.properties /conf/application.properties
ADD target/chrisbolton_service-1.0.0.jar app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar","--spring.config.location=file:/conf/application.properties"]

我正在通过以下方式启动 spring boot 应用程序

I am starting my spring boot app by using this

docker run -it --name chrisbolton_service --link chrisbolton:mysql -p 8080:8080 chrisbolton_service

另外,我正在运行 mysql 。从 docker ps 您可以看到,

Also, I have mysql running. From docker ps you can see,

bcb0a900b693 mysql:latest docker-entrypoint。 .. 6小时前上3小时0.0.0.0:3306->3306/tcp chrisbolton

我有阅读,我需要链接我的容器。但是,当我命中端点 localhost:8080 / blogs 时,我会得到

I have read that I need to link my containers. However, when I hit my endpoint localhost:8080/blogs I am getting

2017-08-29 20:20:37.516 ERROR 1 --- [nio-8080-exec-1] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

更新:

根据评论中的建议,我创建了一个 docker网络并且我同时在网络中运行 mysql 和我的 spring boot 应用程序。

From the suggestion in the comments, I created a docker network and I am running both mysql and my spring boot app in the network. Still getting the same error as above.

这是我的 docker网络上的检查

Here is my inspect on my docker network

$泊坞窗网络检查chrisbolton
[
{
Name : chrisbolton,
Id: 394e6e207ab53967f6f15e9498c95e5b25f3827ad865cc3c21873c7de2e25465,
Created: 2017-08-29T20:37:11.136108745Z,
Scope: local,
驱动程序:桥,
启用IPv6:false,
IPAM:{
驱动程序:默认,
选项 :{},
Config:[
{
Subnet: 172.18.0.0/16,
Gateway: 172.18.0.1
}
]
},
内部:false,
可附加:false,
入口:false,
ConfigFrom :{{
Network:
},
ConfigOnly:false,
容器:{
1556dae62334250b20220b05dc18365d65e498c66a47d0db4b8e7e61de8d3496:{
名称: chrisbolton_service,
EndpointID: 4ce1a129277a0c27b07be796b42b42b2da24fa2b4e4b4e4b2e54b2e0b0b0b0b0e0b0e0b0b0b0b0e0b0b0b0b0e0c0b0b美国总统国:ac:12:00:03,
IPv4Address: 172.18.0.3/16,
IPv6Address:
},
6d7faf5f43722667bdd9080a41f56d9934574900aec1be96118d9683b99f846e: {
Name: chrisbolton,
EndpointID: 312b00c24979fdf5bad50377cf64eeec29bbdcbbf5221b981693886886ae53ef490,
MacAddress: 02:42:ac:12:00:02,
IPv4Address: 172.18.0.2/16,
IPv6Address:
}
},
Options:{},
标签:{}
}
]

推荐答案

每个容器运行在自己的n网络名称空间。容器中的Localhost与主机上的localhost不同。要将容器连接到容器:

Each container runs in its own networking namespace. Localhost in a container is different from localhost on your host. To connect container to container:


  1. 创建公共网络,例如 docker网络创建myapp

  2. 使用名称创建容器并连接到网络,例如: docker run --network myapp-数据库名称mysql mysql 。请注意,不必使用主机上的端口或将Dockerfile中的端口公开即可使容器到容器网络正常工作。

  3. 服务(数据库,Web服务器等) )必须在所有接口上监听,而不是localhost。语法为 0.0.0.0 表示要在所有接口上监听。

  4. 从应用程序中,使用docker的在DNS中指向其他容器的名称,例如 mysql:3306 。不要连接到本地主机(它将不起作用)或已发布的端口(您正在直接与容器通信)。

  1. Create a common network, e.g. docker network create myapp
  2. Create your containers with a name and attached to the network, e.g.: docker run --network myapp --name mysql mysql for the database. Note that it is not necessary to publish the port on the host or expose the port in your Dockerfile for container-to-container networking to work.
  3. The service (the database, webserver, etc) must be listening on all interfaces, not localhost. The syntax for this is 0.0.0.0 to indicate you want to listen on all interfaces.
  4. From your application, connect using docker's built in DNS to the name of your other container, e.g. mysql:3306. Do not connect to localhost (it won't work) or the published port (you're talking directly to the container).

这篇关于春季启动到mysql docker通讯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆