Reputation: 605
I have a docker image with a Java spring boot application which starts and run with no problem in a docker container at the port 8080 as you can see in a portion of the logs:
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)
If I run a curl command inside the container (using the terminal) I get the right response back which indicates the server is app and running.
/# curl http://localhost:8080/web-app/cars/1
{"id":"1"}
Now the problem comes when I try to access the app on the host, I'm using Windows 10 btw (in a web browser http://localhost:8080/web-app/cars/1), here's how I run the docker image.
docker run web-app -p 8080:8080
however, I don't think this is a Windows issue since I can run a simple nginx server and I can access it on the host with no problem.
docker run -d -p 80:80 nginx
One thing I notice is when seeing the running containers, the port column
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
As you can see for the working container the PORT column looks like 0.0.0.0:80->80/tcp but for the non-working container the column looks like 8080/tcp
Here's the Dockerfile in case it helps (Notice I'm exposing the 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"]
Am I missing something here?
EDIT: Here's the 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
}
}
}
}
]
Upvotes: 5
Views: 15206
Reputation: 605
The port mapping was not working simply because I was setting it in the wrong order.
The correct way:
docker run -p 8080:8080 web-app
The way I was running the container and the port mapping was NOT working.
docker run web-app -p 8080:8080
Upvotes: 12
Reputation: 505
run docker-machine ls
. It will return an ip under URL column.
$ 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
Use that instead like
curl -i 192.168.99.100:8080/web-app/cars/1
Upvotes: 0