简体   繁体   中英

Tomcat Docker Container With Network_Mode Host Can't Be Reached

I am working on Ubuntu 16.04 LTS with a docker version of

Docker version 18.03.1-ce, build 9ee9f40

When I start the Tomcat server with network mode host, I can not reach it.

$ sudo docker run --rm -d --network host -p 8888:8080 tomcat:8.0

$ curl localhost:8888
curl: (7) Failed to connect to localhost port 8888: Connection refused

When I start it in default bridged network mode everything seems fine.

$ sudo docker run --rm -d -p 8888:8080 tomcat:8.0
$ curl localhost:8888
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Apache Tomcat/8.0.53</title>
        <link href="favicon.ico" rel="icon" type="image/x-icon" />
        <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <link href="tomcat.css" rel="stylesheet" type="text/css" />

The problem seems to be related with the Tomcat image. The same principle is working for the nginx image. You can check it out: https://docs.docker.com/network/network-tutorial-host/#goal and I tried it. It is working for the nginx image.

I can not find any documentation related to this. Do you know the reason why Tomcat container does not work with host networking?

If you run in host network mode then port mapping doesn't do anything. What you're saying is "run this container with the host's networking stack". So you need to connect to tomcat on the port that tomcat is running on, no forwarding. In the example above this would be 8080 .

$ sudo docker run --rm -d --network host tomcat:8.0
$ curl localhost:8080
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Apache Tomcat/8.0.53</title>
        <link href="favicon.ico" rel="icon" type="image/x-icon" />
        <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <link href="tomcat.css" rel="stylesheet" type="text/css" />

The nginx example works fine because they're connecting to port 80 and that's the port nginx is running on. They're not doing any forwarding in that example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM