gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:46:21 25 4
gpt4 key购买 nike

我有一个带有 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(在网络浏览器中 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 端口):

# 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"]

我是不是漏掉了什么?

编辑:这是 docker inspect:

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 run -p 8080:8080 web-app 

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

docker run web-app -p 8080:8080

关于端口绑定(bind)8080时Docker无法访问主机中的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60368818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com