tkachuko
tkachuko

Reputation: 1986

Docker and http url of started web application

I would like to run my web application in docker (written in java/scala). So as a result I created the following docker file:

FROM java
ADD 0/backend-assembly-1.1.jar /app/backend-assembly-1.1.jar
EXPOSE 80
ENTRYPOINT ["java", "-jar", "\/app\/backend-assembly-1.1.jar", "0.0.0.0", "80"]

Application indeed start successfully when I run it with docker run command:

[DEBUG] [02/12/2017 21:28:56.940] [tkachuko-web-system-akka.actor.default-dispatcher-2] [akka://tkachuko-web-system/system/IO-TCP/selectors/$a/0] Successfully bound to /0:0:0:0:0:0:0:0:80

I do understand that docker images run on virtual machine so I decided to find out IP address of the virtual box machine:

docker inspect 7cf588493b41 | grep IP

and it returned "IPAddress": "172.17.0.2".

However neither 0.0.0.0, localhost or 172.17.0.2 are accessible, but when I run everything without docker (from command line) everything does work. Could you please specify which address my application can be accessed?

Just in case: I am using OS X.

Thanks for any help.

Upvotes: 0

Views: 1047

Answers (1)

Dan Lowe
Dan Lowe

Reputation: 56677

To be usable outside of Docker, you need to bind the port in the container to a port on the hosting system. You have not included your run command, but it would need to be something like this.

    docker run -d -p 8080:80 <image-name>

The key here is the -p (or --publish) flag, which binds a local port to a container port.

-p local-port:container-port

-p 80:80              # bind local port 80 to container's port 80
-p 8080:80            # bind local port 8080 to container's port 80

You can also bind the port only to your Mac's localhost if you prefer.

-p 127.0.0.1:8080:80

In all of the above cases, the local port you've bound should now answer on localhost, i.e.

http://localhost:8080/

You would normally also include the port in an EXPOSE statement in Dockerfile (and you have done so) but it's not technically required to publish the port.

Upvotes: 2

Related Questions