Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac host doesn't like Docker container port forwarding

I am experimenting with Docker for the first time, and am trying to get a Spring Boot web app to run inside a Docker container. I am building the app (which packages up into a self-contained jar) and then adding it to the Docker image (which is what I want).

You can find my SSCCE at this Bootup repo on GitHub, whose README has all the instructions to reproduce what I'm seeing. But basically:

  • I build the web app into a jar
  • Run docker build -t bootup . which succeeds
  • Run docker run -it -p 9200:9200 -d --name bootup bootup and then container seems to start up just fine, as is evidence by the docker ps output below
  • However, when I point a browser to http://localhost:9200, I get nothing

docker ps output:

CONTAINER ID        IMAGE               COMMAND                  CREATED
a8c4ee64a1bc        bootup              "/bin/sh -c 'java -ja"   2 days ago

STATUS              PORTS                    NAMES
Up 12 seconds       0.0.0.0:9200->9200/tcp   bootup

The web app is configured to run on port 9200, not the Java default of 8080. You can see this for yourself by running the app outside of docker (so, just locally on you host machine) by running ./gradlew clean build && java -jar build/libs/bootup.jar.

To my knowledge, there is no Firewall running on my host that would be blocking ports (I am on Mac 10.11.5 and verified that System Preferences >> Security & Privacy >> Firewall is turned off).

Can anyone spot where I'm going awry?


Updates:

I ran a curl, netstat and lsof on the host:

HOST:
curl http://localhost:9200
curl: (52) Empty reply from server

netstat -an | grep 9200
tcp6       0      0  ::1.9200               *.*                    LISTEN     
tcp4       0      0  *.9200                 *.*                    LISTEN 

lsof -n -i4TCP:9200 | grep LISTEN
com.docke 2578 myuser   19u  IPv4 <someHexNumber>      0t0  TCP *:wap-wsp (LISTEN)

And then docker exec'd into the container and ran another netstat:

CONTAINER:
netstat -an | grep 9200
bash: netstat: command not found

Update w/ photos:

Picture of my browser (Chrome) pointed to http://localhost:9200:

enter image description here

Picture of the source code at http://localhost:9200:

enter image description here

Picture of Chrome Developer Tools inspecting the page at http://localhost:9200:

enter image description here

Picture of the Network tab in Chrome Developer Tools:

enter image description here

What the heck is going on here?!?!? According to the source, the browser should be rendering my Well hello there, from Dockerland! message just fine. According to the actual browser page, it looks like there is a networking error. And according to Chrome Developer Tools, my app is returning all sorts of HTML/CSS/JS content that is not even remotely apart of my app (check out the source code, see for yourself)!!!

like image 728
smeeb Avatar asked Aug 26 '16 12:08

smeeb


People also ask

Does host Docker internal work on Mac?

docker. internal hostname to connect to your Docker host from inside a Docker container. This works fine on Docker for Mac and Docker for Windows, but unfortunately, this is not was not supported on Linux until Docker 20.10.

Does Docker use port forwarding?

Through port forwarding, services are exposed to the applications residing outside of the host's internal network. Here, port forwarding allows remote client applications to connect to the NCache servers running inside Docker containers (internal Docker network).

What port does Docker run on for Mac?

The rule is named docker, the protocol is set to tcp and port 2375 on the host is forwarded to port 2375 in the guest OS. You can find the names of all your virtual machines by using the VBoxManage list vms command.

Is Mac good for Docker?

Docker image was built in only seven minutes on MacBook M1 Pro, which was even better than the build time on my new VPS. This is not surprising, I gave Docker quite a lot of resources. But it also shows that if there are not too many I/O disk operations, performance is quite good.


1 Answers

The Dockerfile doesn't expose 9200 to the daemon. Add

EXPOSE 9200

to the Dockerfile before ENTRYPOINT

like image 71
Mano Marks Avatar answered Oct 09 '22 22:10

Mano Marks