无法访问在Docker容器中启动的Tomcat

问题描述 投票:0回答:5

我正在启动Tomcat作为Docker容器:

$ docker run -dP tomcat

并且该过程正确启动:

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS                     NAMES
48b87e545c18        tomcat              "catalina.sh run"   About a minute ago   Up About a minute   0.0.0.0:32769->8080/tcp   hopeful_ardinghelli

$ docker logs 48b87e545c18
16-Sep-2015 17:16:38.027 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.0.26
16-Sep-2015 17:16:38.037 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Aug 18 2015 11:38:37 UTC
16-Sep-2015 17:16:38.037 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         8.0.26.0
16-Sep-2015 17:16:38.038 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
16-Sep-2015 17:16:38.038 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            4.0.9-boot2docker
16-Sep-2015 17:16:38.038 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
16-Sep-2015 17:16:38.039 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/lib/jvm/java-7-openjdk-amd64/jre
16-Sep-2015 17:16:38.039 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.7.0_79-b14
16-Sep-2015 17:16:38.040 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
16-Sep-2015 17:16:38.040 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /usr/local/tomcat
16-Sep-2015 17:16:38.041 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/tomcat
16-Sep-2015 17:16:38.041 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
16-Sep-2015 17:16:38.042 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
16-Sep-2015 17:16:38.042 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.endorsed.dirs=/usr/local/tomcat/endorsed
16-Sep-2015 17:16:38.043 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
16-Sep-2015 17:16:38.043 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
16-Sep-2015 17:16:38.044 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
16-Sep-2015 17:16:38.044 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib
16-Sep-2015 17:16:38.226 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
16-Sep-2015 17:16:38.267 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
16-Sep-2015 17:16:38.272 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
16-Sep-2015 17:16:38.274 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
16-Sep-2015 17:16:38.282 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 1251 ms
16-Sep-2015 17:16:38.325 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
16-Sep-2015 17:16:38.341 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.26
16-Sep-2015 17:16:38.374 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/host-manager
16-Sep-2015 17:17:16.231 INFO [localhost-startStop-1] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [36,979] milliseconds.
16-Sep-2015 17:17:16.276 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/host-manager has finished in 37,901 ms
16-Sep-2015 17:17:16.276 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/docs
16-Sep-2015 17:17:16.361 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/docs has finished in 85 ms
16-Sep-2015 17:17:16.362 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/examples
16-Sep-2015 17:17:17.221 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/examples has finished in 859 ms
16-Sep-2015 17:17:17.242 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/ROOT
16-Sep-2015 17:17:17.343 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/ROOT has finished in 101 ms
16-Sep-2015 17:17:17.354 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/manager
16-Sep-2015 17:17:17.452 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/manager has finished in 98 ms
16-Sep-2015 17:17:17.460 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
16-Sep-2015 17:17:17.484 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
16-Sep-2015 17:17:17.487 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 39204 ms

但是当我在URL http://localhost:32769的浏览器中点击服务器时,我收到连接拒绝错误。我还尝试明确发布端口:

$ docker run -d -p 8888:8080 tomcat

或者以交互式shell模式启动容器:

$ docker run -it -p 8888:8080 tomcat

但没有帮助:http://localhost:8888拒绝连接。我该如何解决此问题?

tomcat docker portforwarding
5个回答
2
投票

我刚刚在DigitalOcean Docker VM上测试了这个:

root@33b54c81-d980-41a4-982a-f0449663b68e:~# docker run -dP tomcat
Unable to find image 'tomcat:latest' locally
latest: Pulling from library/tomcat
843e2bded498: Pull complete 
...
Status: Downloaded newer image for tomcat:latest
41cea0a1fede58d066af0f6ce5fc17aef66d4b4912244cf4966b5f459b881e1b

集装箱运行:

root@33b54c81-d980-41a4-982a-f0449663b68e:~# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
41cea0a1fede        tomcat              "catalina.sh run"        2 minutes ago       Up 2 minutes        0.0.0.0:32768->8080/tcp   mad_panini

通过docker-proxy主机端口映射进行测试:

root@33b54c81-d980-41a4-982a-f0449663b68e:~# telnet localhost 32768
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET / HTTP/1.1
host: localhost

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
...

请注意,Docker还在iptables中打了一个洞,允许与容器直接通信(没有主机端口映射):

root@33b54c81-d980-41a4-982a-f0449663b68e:~# iptables -L
...
Chain DOCKER (1 references)
target     prot opt source               destination         
ALLOW      all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             172.17.0.1           tcp dpt:http-alt

直接测试:

root@33b54c81-d980-41a4-982a-f0449663b68e:~# telnet 172.17.0.1 8080
Trying 172.17.0.1...
Connected to 172.17.0.1.
Escape character is '^]'.
GET / HTTP/1.1
host: localhost

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
...

您还可以尝试将--net=host参数传递给docker run,它将主机的网络直接暴露给容器。

希望这可以帮助..

- ab1


1
投票

端口映射是在默认机器的地址192.168.99.100上执行的。这些网址分别是http://192.168.99.100:32769http://192.168.99.100:8888。希望这有助于其他人遇到同样的问题。


1
投票

如果您使用boot2docker运行docker,则必须使用boot2docker VM ip地址启动tomcat。

您可以使用以下命令获取IP地址:

boot2docker ip

然后运行http://ip_address:8888


0
投票

这是因为您无法使用localhost访问您的docker地址。您必须使用与虚拟网络相关的IP。您必须检查docker正在运行的子网。

示例:我的笔记本的IP地址是192.168.1.13,但我的VirtualMachine的IP地址运行192.168.99.100。

问候


0
投票

请检查您的计算机中是否有任何代理设置也适用于localhost。

我如何检查:使用命令printenv,检查是否定义了任何http_proxy或https_proxy,并且未定义no_proxy

© www.soinside.com 2019 - 2024. All rights reserved.