Mac host doesn't like port forwarding of docker container

I am experimenting with Docker for the first time and am trying to run a Spring web application to load inside a Docker container. I create an application (which is packaged in a standalone jar) and then add it to the Docker image (this is what I want).

You can find SSCCE in this Bootup repo on GitHub , whose README has all the instructions for reproducing what I see. But basically:

  • I am creating a web application in a bank
  • Run docker build -t bootup . that succeeds
  • Run docker run -it -p 9200:9200 -d --name bootup bootup and then the container seems to start working fine, as docker ps output shows below
  • However, when I point the 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 application is configured to work on port 9200 , not the default default is 8080. You can see this for yourself by running the application outside of dockers (for example, only locally on your host computer) by running ./gradlew clean build && java -jar build/libs/bootup.jar .

As far as I know, my host does not have a firewall to block ports (I am on Mac 10.11.5 and checked that System Preferences >> Security & Privacy >> Firewall turned off).

Can anyone see where I will be wrong?


Update:

I ran 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 in the container and ran another netstat :

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

Update from photo:

The image of my browser (Chrome) is indicated at http://localhost:9200 :

enter image description here

Image of source code at http://localhost:9200 :

enter image description here

Image of the Chrome developer tools checking the page at http://localhost:9200 :

enter image description here

Image of the Network tab in Chrome Developer Tools:

enter image description here

What the hell is going on here?!?!? According to the source, the browser should show me hi from Dockerland! the message is only good. According to the actual browser page, there seems to be a network error. And according to Chrome Developer Tools, my application returns all kinds of HTML / CSS / JS content that are not even removed from my application (check the source code, see for yourself) !!!

+6
source share
4 answers

Now let's add another answer, because I saw something related to the Github reputation you posted:

So repo is the spring boot repo with the application.yml file.

Your Dockerfile is as follows:

 FROM openjdk:8 RUN mkdir /opt/bootup ADD build/libs/bootup.jar /opt/bootup WORKDIR /opt/bootup EXPOSE 9200 ENTRYPOINT java -jar bootup.jar 

Which adds a built-in jar to the image. If my understanding is correct, jar does not include application.yml because:

  • This is not part of the assembly (gradle will be only the src / main package). It is located in the root folder of the project
  • It is not explicitly added to Docker.

So, can you assume that your application is actually running on 8080 (the default) at the moment?

A few options you could try:

  • Try to expose 8080 instead of 9200 (or expose both of them) and see if this has changed?
  • Entrypoint command can add port --server.port=9200
  • The application.yml file must be added to the image (you may need to add an argument for the correct link) [ ADD application.yml /opt/bootup , after the first ADD command]
  • Include the application.yml file in src / main / resources so that spring boots automatically.

References

Spring Download boot order reference documentation for external configuration

+1
source

The Docker file does not provide a 9200 daemon. Add

EXPOSE 9200

to the Docker file before ENTRYPOINT

+2
source

I ran your repo as is on Docker 1.12 on OSX.

If you look closely at the launch of your container:

 2016-08-29 20:52:31.028 INFO 5 --- [ main] o.eclipse.jetty.server.ServerConnector : Started ServerConnector@47949d1a {HTTP/1.1}{0.0.0.0:8080} 2016-08-29 20:52:31.033 INFO 5 --- [ main] .sbcejJettyEmbeddedServletContainer : Jetty started on port(s) 8080 (http/1.1) 

Although application.yml and Dockerfile contain 9200 , the application runs on 8080

+2
source

Assuming you are using the Docker Toolbox, not the beta ...

There is a 3-step process for port demonstration:

  • use EXPOSE 8080 , where 8080 is just the port number in the Dockerfile
  • use the -p 8080: 8080 command in your docker run command
  • Make sure you configure port forwarding in Oracle Virtual Box so that the boot2docker machine can receive requests from port 8080.

This applies to both Windows and OSX, where the Docker Toolbox is used. Linux does not use Oracle VirtualBox to run dockers, so these hosts do not need to make a third point.

+1
source

All Articles