2

On my windows laptop I have created a Play application which runs fine if I execute its scripts directly. On the local machine, I access the application using localhost:9000 URL.

I have now created a Docker image of the application and have exposed port 9000

#this docker file copies prod specific files to container, eg logback_prod.xml and application_prod.conf

    FROM openjdk:8
    #ENV APP_NAME      myapp
    #ENV APP_VERSION   1.0-SNAPSHOT
...
    #entrypoint is deploy/....
    EXPOSE 9000
    ENTRYPOINT ...

But I can't access the application on localhost:9000. I suspect that the image might be running on some other IP created by docker itself.

Am I correct? How can I access my application through the container? I don't need Kubernetes Services etc. as I already have that setup on another machine. My specific question is how to access the docker container directly.

UPDATE I also tried running the docker image using --network="host" but that doesn't work either

UPDATE 2

Based on the suggestions below, I executed the following commands but still can't access the application.

docker run -p 9000:9000 --env-file env.txt imagename

I see the trace

[debug] a.i.TcpListener - Successfully bound to /0.0.0.0:9000
[info] p.c.s.AkkaHttpServer - Listening for HTTP on /0.0.0.0:9000

docker ps -a shows application is up with port binding 0.0.0.0:9000->9000/tcp

docker inspect shows IP - "IPAddress": "172.17.0.2"

but http://172.17.0.2:9000/ on Chrome doesn't work This site can’t be reached172.17.0.2

netstat -ab on cmd shows TCP 0.0.0.0:9000 LAPTOP-788I0GL1:0 LISTENING [com.docker.backend.exe]

aran
  • 10,978
  • 5
  • 39
  • 69
Manu Chadha
  • 15,555
  • 19
  • 91
  • 184
  • 1
    How did you start the container? You should be able to connect to a `docker run -p` port number; just `EXPOSE` in a Dockerfile isn't enough. The container-private IP address is an internal Docker implementation detail that doesn't work in many common situations (from a different host, if Docker is in a VM, on MacOS or Windows platforms) and I would not recommend looking it up. – David Maze Dec 29 '20 at 00:57
  • Most recently, I did `docker run -p 9000:9000 --env-file env.txt manuchadha25/mydockerimage`. I can see `0.0.0.0:9000->9000/tcp` when I do `docker ps -a` – Manu Chadha Dec 29 '20 at 00:59
  • 1
    Does the application listen on 127.0.0.1:9000 ("localhost only"), 0.0.0.0:9000 ("all interfaces"), or something else? (It should be the 0.0.0.0 variant, and the port number needs to match the second `-p` port.) Are you using Docker Toolbox? (That needs a special `docker-machine ip` address, perhaps 192.168.99.100, instead of `localhost`.) – David Maze Dec 29 '20 at 01:00
  • `Listening for HTTP on /0.0.0.0:9000` – Manu Chadha Dec 29 '20 at 01:06

2 Answers2

3

Identify container's IP Address

Try these options with a running container. <docker> refers to container's name or id

docker inspect --format '{{ .NetworkSettings.IPAddress }}' <docker>

//WINDOWS ONLY
docker inspect --format "{{ .NetworkSettings.IPAddress }}" <docker>

docker inspect <docker> | grep "IPAddress"

Grep not avaliable on Windows*


docker network inspect bridge

This last one would output all running containers' info that are allocated in the bridge network (default). Once identified, check the container's IPv4Address field.


Expose ports

In order to be able to connect to the a container's port, you could expose it. Note that the previous step is not needed in this case:

docker run -p 9000:9000 --env-file env.txt manuchadha25/mydockerimage

By default the docker will bind/listen to all interfaces on the host. The -p 9000:9000 option exposes the port, and as a result you get: 0.0.0.0:9000->9000/tcp

Now localhost:9000 succesfully connects to your docker process.

aran
  • 10,978
  • 5
  • 39
  • 69
  • Thanks. Did that but it doesn't show anything. Just an empty output – Manu Chadha Dec 29 '20 at 00:40
  • I see "IPAddress": "".when I run `inspect`. Is this weird? Also, I am on Windows so no `Grep` for me – Manu Chadha Dec 29 '20 at 00:45
  • sorry, the mistake earlier was I was running using `--network="host"`. I removed that and did only `docker run`. I can now see IP `"IPAddress": "172.17.0.2"` but I can't still access the app using `172.17.0.2:9000` – Manu Chadha Dec 29 '20 at 00:49
  • that's because you must "expose" the port to the host when running the container. try the syntax docker run (...) -p 9200:9200 (...) ---> https://forums.docker.com/t/how-to-expose-port-on-running-container/3252/8 – aran Dec 29 '20 at 00:51
  • did `docker run -p 9000:9000 --env-file env.txt manuchadha25/mydockerimage` but no love. I am sure it is some silly mistake somewhere as this isn't suppose to be difficult. I can see `0.0.0.0:9000->9000/tcp` when I do `docker ps -a` – Manu Chadha Dec 29 '20 at 00:58
  • `0.0.0.0:9000->9000` should let you make a `localhost:9000` call – aran Dec 29 '20 at 01:15
  • I have no clue what just happened but now I can access `localhost:9000`! – Manu Chadha Dec 29 '20 at 01:16
  • `0.0.0.0` is binded to all interfaces, and since the port is exposed, localhost:9000 is a valid endpoint. Glad you could fix it! – aran Dec 29 '20 at 01:17
2

You can try the command docker inspect:

docker inspect <containerid>

for list the containers ids you can run the command:

docker ps

In order to get the ipAddress information you should look to node:

"IPAddress": "172.23.0.2"

In the json output produced.

Here is the documentation for docker inspect command.

Danizavtz
  • 3,166
  • 4
  • 24
  • 25
  • I see "IPAddress": "".when I run `inspect`. Is this weird? – Manu Chadha Dec 29 '20 at 00:45
  • Found that question with accepted answer, maybe that's your case: https://stackoverflow.com/questions/43803849/my-docker-container-does-not-have-ip-address-why – Danizavtz Dec 29 '20 at 00:49
  • Thanks. Will check. Sorry, the mistake earlier was I was running using --network="host". I removed that and did only docker run. I can now see IP "IPAddress": "172.17.0.2" but I can't still access the app using 172.17.0.2:9000 – Manu Chadha Dec 29 '20 at 00:50