我创建了一个spring boot
应用。当我在Eclipse中运行应用程序时,我可以从浏览器中获取服务并获得我想要的响应。
我正在构建这个容器:
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
应用
docker run -it --name chrisbolton_service --link chrisbolton:mysql -p 8080:8080 chrisbolton_service
此外,我已mysql
正在运行。从docker ps
您可以看到,
bcb0a900b693 mysql:latest "docker-entrypoint..." 6 hours ago Up 3 hours 0.0.0.0:3306->3306/tcp chrisbolton
我需要read来链接我的容器。但是,当我点击我的端点localhost:8080/blogs
时,我正在
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 network
,我在网络中同时运行了mysql
和spring boot
个应用。仍然得到与上面相同的错误。
以下是inspect
docker network
$ docker network inspect chrisbolton
[
{
"Name": "chrisbolton",
"Id": "394e6e207ab53967f6f15e9498c95e5b25f3827ad865cc3c21873c7de2e25465",
"Created": "2017-08-29T20:37:11.136108745Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"1556dae62334250b20220b05dc18365d65e498c66a47d0db4b8e7e61de8d3496": {
"Name": "chrisbolton_service",
"EndpointID": "4ce1a129277a0c27b07be796c4b2fa18604f19764e28124729af2479da0cd58c",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
},
"6d7faf5f43722667bdd9080a41f56d9934574900aec1be96118d9683b99f846e": {
"Name": "chrisbolton",
"EndpointID": "312b00c24979fdf5bad50377cf64eeec29bbdcbbf5221b981693886ae53ef490",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
答案 0 :(得分:2)
每个容器都在自己的网络命名空间中运行。容器中的Localhost与主机上的localhost不同。要将容器连接到容器:
docker network create myapp
docker run --network myapp --name mysql mysql
用于数据库。请注意,没有必要在主机上发布端口或在Dockerfile中公开端口以使容器到容器网络正常工作。0.0.0.0
,表示你想要监听所有接口。mysql:3306
。不要连接到localhost(它不起作用)或发布的端口(你直接与容器对话)。