Identify Docker container's IP Address and connect to port

Viewed 3592

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]

2 Answers

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.

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.

Related