端口绑定8080时,Docker无法访问主机中的应用程序

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

我有一个带有Java Spring Boot应用程序的docker镜像,它可以在端口8080的docker容器中启动并正常运行,如您在部分日志中看到的那样:

   2020-02-24 02:09:07.906  INFO : Tomcat started on port(s): 8080 (http) with context path '/web-app'
   2020-02-24 02:09:07.909  INFO : Started web-app in 7.034 seconds (JVM running for 7.601)

如果在容器内(使用终端)运行curl命令,则会返回正确的响应,表明服务器已在运行并且正在运行。

/# curl http://localhost:8080/web-app/cars/1
{"id":"1"}

现在问题出在我尝试访问主机上的应用程序时,我正在使用Windows 10 btw(在Web浏览器http://localhost:8080/web-app/cars/1中,这是我运行docker映像的方式。

docker run web-app -p 8080:8080

但是,我认为这不是Windows问题,因为我可以运行简单的nginx服务器,并且可以在主机上毫无问题地访问它。

docker run -d -p 80:80 nginx

我注意到的一件事是看到正在运行的容器,端口列

C:\Users\luis>docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                NAMES
71c99d5a2c66        nginx               "nginx -g 'daemon of…"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp   loving_hypatia
86080fcce5d6        web-app            "java -jar /usr/app/…"    13 minutes ago       Up 13 minutes       8080/tcp             vigorous_swanso

如您所见,对于工作容器,PORT列看起来像0.0.0.0:80-> 80 / tcp,但是对于非工作容器,该列看起来像是8080 / tcp

这里是Dockerfile,以防万一(请注意我要公开8080 PORT):

# Building the APP from Maven image
FROM maven:3.6.0-jdk-11-slim AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package

# From previous stage run the WAR created
FROM openjdk:11-jre-slim
COPY --from=build /home/app/target/web-app-0.0.1-SNAPSHOT.war /usr/local/lib/web-app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/usr/local/lib/web-app.jar"]

我在这里想念东西吗?

编辑:这是码头工人检查:

C:\Users\luis>docker inspect a900f22848b2
[
    {
        "Id": "a900f22848b21193c966bdbf2804ac7f736c4802f969624392724717983269dc",
        "Created": "2020-02-24T02:51:55.407400538Z",
        "Path": "java",
        "Args": [
            "-jar",
            "/usr/app/web-app.jar",
            "-p",
            "8080:8080"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 4315,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-02-24T02:51:55.691065531Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:d5c9d14f8d550fab2028370bf8e5785f26f6c5bf423b77480076f191e3f551b2",
        "ResolvConfPath": "/var/lib/docker/containers/a900f22848b21193c966bdbf2804ac7f736c4802f969624392724717983269dc/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/a900f22848b21193c966bdbf2804ac7f736c4802f969624392724717983269dc/hostname",
        "HostsPath": "/var/lib/docker/containers/a900f22848b21193c966bdbf2804ac7f736c4802f969624392724717983269dc/hosts",
        "LogPath": "/var/lib/docker/containers/a900f22848b21193c966bdbf2804ac7f736c4802f969624392724717983269dc/a900f22848b21193c966bdbf2804ac7f736c4802f969624392724717983269dc-json.log",
        "Name": "/affectionate_ardinghelli",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Capabilities": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                50,
                120
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/6fb2eab78ca0d9b21e598d5d81cdb26e5039413c250801a8626cffd4a19eb17f-init/diff:/var/lib/docker/overlay2/25b7580ce520a6935c10b4acbedde883c0d0ebc676fb56525790883619ca4222/diff:/var/lib/docker/overlay2/94a2a6c5df74fb9ef6dc3dfb31cc75ed2c7f4d56f2f0e6b8bbad15d5032ad1b4/diff:/var/lib/docker/overlay2/6d09c5ef421eb410fdaf02da239d85b04154bf7b9b6a99c36addd8e2ff0b1479/diff:/var/lib/docker/overlay2/b45789110f7591e23d54469c6eb1479f3b2719b57cb31b344d09e3e057d372b0/diff:/var/lib/docker/overlay2/24daf4027a52517c1e6f166cbd61f19c713dcee101c95ec6bd0e0eb0b3590507/diff:/var/lib/docker/overlay2/340750e0a675235dd75be936c08b3768c5cb558ca4210ec8668949633e010040/diff:/var/lib/docker/overlay2/bf04da8ac060038ee92ea1d52cd46a261cae8a3a0b6428424a1d96de808f245a/diff:/var/lib/docker/overlay2/58b7232b6311cd78cd55f8d6153a9178cd93e3126997c4aa6aa0536206c58a31/diff",
                "MergedDir": "/var/lib/docker/overlay2/6fb2eab78ca0d9b21e598d5d81cdb26e5039413c250801a8626cffd4a19eb17f/merged",
                "UpperDir": "/var/lib/docker/overlay2/6fb2eab78ca0d9b21e598d5d81cdb26e5039413c250801a8626cffd4a19eb17f/diff",
                "WorkDir": "/var/lib/docker/overlay2/6fb2eab78ca0d9b21e598d5d81cdb26e5039413c250801a8626cffd4a19eb17f/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "a900f22848b2",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": true,
            "AttachStderr": true,
            "ExposedPorts": {
                "8080/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "LANG=C.UTF-8",
                "JAVA_HOME=/usr/local/openjdk-11",
                "JAVA_VERSION=11.0.6",
                "JAVA_BASE_URL=https://github.com/AdoptOpenJDK/openjdk11-upstream-binaries/releases/download/jdk-11.0.6%2B10/OpenJDK11U-jdk_",
                "JAVA_URL_VERSION=11.0.6_10"
            ],
            "Cmd": [
                "-p",
                "8080:8080"
            ],
            "Image": "web-app",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "java",
                "-jar",
                "/usr/app/web-app.jar"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "042d90c5fd29fcb620f3e021c7c4459a34caed47e50b3f183830543dd103826f",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "8080/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/042d90c5fd29",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "abcdee85d18be541a47c8c4a2f3aa8d4eca0b60bc11a635566b433ea0c59149b",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.4",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:04",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "82786fd4bcc39ea0c53918c2a61b5299dd51fe2239b0a02122bd8fda14a1ac2d",
                    "EndpointID": "abcdee85d18be541a47c8c4a2f3aa8d4eca0b60bc11a635566b433ea0c59149b",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.4",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:04",
                    "DriverOpts": null
                }
            }
        }
    }

]

docker docker-for-windows docker-networking docker-network
3个回答
0
投票

对于docker容器,它为容器创建了一个网络,因此您不能从外部使用localhost访问该容器。

应该是:172.17.0.1

OR 172.17.0.4

用于检查:http://172.17.0.1:8080/web-app/cars/1或:http://172.17.0.4:8080/web-app/cars/1

参考:https://docs.docker.com/v17.09/engine/userguide/networking/#the-docker_gwbridge-network


0
投票

运行docker-machine ls。它将在“ URL”列下返回一个IP。

$ docker-machine ls -t 12
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER   ERRORS
default   -        virtualbox   Running   tcp://192.168.99.100:2376           v1.9.1

使用类似curl -i 192.168.99.100:8080/web-app/cars/1


0
投票

端口映射无法正常工作,仅是因为我以错误的顺序设置了端口。

正确的方法:

docker run -p 8080:8080 web-app 

我运行容器的方式和端口映射不起作用。

docker run web-app -p 8080:8080
© www.soinside.com 2019 - 2024. All rights reserved.