Docker: 无法访问容器 host/port
Docker: can not access container host/port
我有一个容器
ps -a
de3ff650fbef tasklist-img "/bin/sh -c 'java -ja" 2 minutes ago Up 2 minutes 8080/tcp, 0.0.0.0:8080->8081/tcp tasklist-img-01
并且无法访问 localhost:8080/task-list
当我检查时发现 json 的 IP 地址为 172.17.0.2,它也是无法访问的
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "82398f58a56f6685223df13f0e053c1ad388505ff595ec270eb4e9a445109641",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"8080/tcp": null,
"8081/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "8080"
}
]
},
"SandboxKey": "/var/run/docker/netns/82398f58a56f",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "e8d9d3e209e858d6e881295899949df457fd8ca28cb059105bebceb6015e1e7d",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "24ef0b8e2aea470b5a0c4e48b96e5b203ea35b291689fe6d73b71b870c86f659",
"EndpointID": "e8d9d3e209e858d6e881295899949df457fd8ca28cb059105bebceb6015e1e7d",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02"
}
}
我的docker机器运行下ip 192.168.99.100
i 运行 带有命令的容器
docker run -d -p 8080:8081 --name tasklist-img-01 tasklist-img
我可以访问http://192.168.99.100:8080/ and it is return only dropwizard metrics, http://192.168.99.100:8080/task-list/无法访问
我也登录了
INFO [2016-11-14 19:32:29,324] io.dropwizard.server.DefaultServerFactory: Registering admin handler with root path prefix: /
INFO [2016-11-14 19:32:29,402] org.eclipse.jetty.setuid.SetUIDListener: Opened application@522a32b1{HTTP/1.1}{0.0.0.0:8080}
INFO [2016-11-14 19:32:29,402] org.eclipse.jetty.setuid.SetUIDListener: Opened admin@35390ee3{HTTP/1.1}{0.0.0.0:8081}
INFO [2016-11-14 19:32:29,406] org.eclipse.jetty.server.Server: jetty-9.2.z-SNAPSHOT
INFO [2016-11-14 19:32:30,428] io.dropwizard.jersey.DropwizardResourceConfig: The following paths were found for the configured resources:
GET /task-list (com.bjedrzejewski.tasklistservice.TaskListResource)
GET /task-list/big (com.bjedrzejewski.tasklistservice.TaskListResource)
INFO [2016-11-14 19:32:30,435] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler@378bd86d{/,null,AVAILABLE}
INFO [2016-11-14 19:32:30,450] io.dropwizard.setup.AdminEnvironment: tasks =
POST /tasks/log-level (io.dropwizard.servlets.tasks.LogConfigurationTask)
POST /tasks/gc (io.dropwizard.servlets.tasks.GarbageCollectionTask)
WARN [2016-11-14 19:32:30,451] io.dropwizard.setup.AdminEnvironment:
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! THIS APPLICATION HAS NO HEALTHCHECKS. THIS MEANS YOU WILL NEVER KNOW !
! IF IT DIES IN PRODUCTION, WHICH MEANS YOU WILL NEVER KNOW IF YOU'RE !
! LETTING YOUR USERS DOWN. YOU SHOULD ADD A HEALTHCHECK FOR EACH OF YOUR !
! APPLICATION'S DEPENDENCIES WHICH FULLY (BUT LIGHTLY) TESTS IT. !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
INFO [2016-11-14 19:32:30,456] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler@7b60c3e{/,null,AVAILABLE}
INFO [2016-11-14 19:32:30,486] org.eclipse.jetty.server.ServerConnector: Started application@522a32b1{HTTP/1.1}{0.0.0.0:8080}
INFO [2016-11-14 19:32:30,487] org.eclipse.jetty.server.ServerConnector: Started admin@35390ee3{HTTP/1.1}{0.0.0.0:8081}
所以我不明白我的微服务在哪里?:)))
看起来您将容器的 8081
端口映射到主机
的 8080
docker run -d -p 8080:8081
并且有端口在 8080
和 8081
上侦听
INFO [2016-11-14 19:32:30,486] org.eclipse.jetty.server.ServerConnector: Started application@522a32b1{HTTP/1.1}{0.0.0.0:8080}
INFO [2016-11-14 19:32:30,487] org.eclipse.jetty.server.ServerConnector: Started admin@35390ee3{HTTP/1.1}{0.0.0.0:8081}
您当然可以访问它,但这不是容器的真实 8080
。
您可能需要发布 端口范围
docker run -d -p 8080-8081:8080-8081
我有一个容器
ps -a
de3ff650fbef tasklist-img "/bin/sh -c 'java -ja" 2 minutes ago Up 2 minutes 8080/tcp, 0.0.0.0:8080->8081/tcp tasklist-img-01
并且无法访问 localhost:8080/task-list
当我检查时发现 json 的 IP 地址为 172.17.0.2,它也是无法访问的
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "82398f58a56f6685223df13f0e053c1ad388505ff595ec270eb4e9a445109641",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"8080/tcp": null,
"8081/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "8080"
}
]
},
"SandboxKey": "/var/run/docker/netns/82398f58a56f",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "e8d9d3e209e858d6e881295899949df457fd8ca28cb059105bebceb6015e1e7d",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "24ef0b8e2aea470b5a0c4e48b96e5b203ea35b291689fe6d73b71b870c86f659",
"EndpointID": "e8d9d3e209e858d6e881295899949df457fd8ca28cb059105bebceb6015e1e7d",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02"
}
}
我的docker机器运行下ip 192.168.99.100 i 运行 带有命令的容器
docker run -d -p 8080:8081 --name tasklist-img-01 tasklist-img
我可以访问http://192.168.99.100:8080/ and it is return only dropwizard metrics, http://192.168.99.100:8080/task-list/无法访问
我也登录了
INFO [2016-11-14 19:32:29,324] io.dropwizard.server.DefaultServerFactory: Registering admin handler with root path prefix: /
INFO [2016-11-14 19:32:29,402] org.eclipse.jetty.setuid.SetUIDListener: Opened application@522a32b1{HTTP/1.1}{0.0.0.0:8080}
INFO [2016-11-14 19:32:29,402] org.eclipse.jetty.setuid.SetUIDListener: Opened admin@35390ee3{HTTP/1.1}{0.0.0.0:8081}
INFO [2016-11-14 19:32:29,406] org.eclipse.jetty.server.Server: jetty-9.2.z-SNAPSHOT
INFO [2016-11-14 19:32:30,428] io.dropwizard.jersey.DropwizardResourceConfig: The following paths were found for the configured resources:
GET /task-list (com.bjedrzejewski.tasklistservice.TaskListResource)
GET /task-list/big (com.bjedrzejewski.tasklistservice.TaskListResource)
INFO [2016-11-14 19:32:30,435] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler@378bd86d{/,null,AVAILABLE}
INFO [2016-11-14 19:32:30,450] io.dropwizard.setup.AdminEnvironment: tasks =
POST /tasks/log-level (io.dropwizard.servlets.tasks.LogConfigurationTask)
POST /tasks/gc (io.dropwizard.servlets.tasks.GarbageCollectionTask)
WARN [2016-11-14 19:32:30,451] io.dropwizard.setup.AdminEnvironment:
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! THIS APPLICATION HAS NO HEALTHCHECKS. THIS MEANS YOU WILL NEVER KNOW !
! IF IT DIES IN PRODUCTION, WHICH MEANS YOU WILL NEVER KNOW IF YOU'RE !
! LETTING YOUR USERS DOWN. YOU SHOULD ADD A HEALTHCHECK FOR EACH OF YOUR !
! APPLICATION'S DEPENDENCIES WHICH FULLY (BUT LIGHTLY) TESTS IT. !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
INFO [2016-11-14 19:32:30,456] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler@7b60c3e{/,null,AVAILABLE}
INFO [2016-11-14 19:32:30,486] org.eclipse.jetty.server.ServerConnector: Started application@522a32b1{HTTP/1.1}{0.0.0.0:8080}
INFO [2016-11-14 19:32:30,487] org.eclipse.jetty.server.ServerConnector: Started admin@35390ee3{HTTP/1.1}{0.0.0.0:8081}
所以我不明白我的微服务在哪里?:)))
看起来您将容器的 8081
端口映射到主机
8080
docker run -d -p 8080:8081
并且有端口在 8080
和 8081
INFO [2016-11-14 19:32:30,486] org.eclipse.jetty.server.ServerConnector: Started application@522a32b1{HTTP/1.1}{0.0.0.0:8080}
INFO [2016-11-14 19:32:30,487] org.eclipse.jetty.server.ServerConnector: Started admin@35390ee3{HTTP/1.1}{0.0.0.0:8081}
您当然可以访问它,但这不是容器的真实 8080
。
您可能需要发布 端口范围
docker run -d -p 8080-8081:8080-8081