Docker Community Forums

Share and learn in the Docker community.

How to connect to container from Docker Host


(Ravimeetsu) #1


I have created a used defined bridge networK :

docker network create -d bridge --subnet --gateway docker_dev_tool_bridge

I have 2 containers: mongodb and source-analysis

mongodb container:

docker run \
    --name ${CONTAINER_NAME} \
    -it \
    -d \
    -v /var/lib/mongo:/data/db \
    --network docker_dev_tool_bridge \
    --ip \
    --cpus=${CPU} \
    -m=${MEMORY} \
    -e AUTH=no \
    --rm \

source-analysis docker command:

docker run \
    --name ${CONTAINER_NAME} \
    -it \
    -d \
    --network docker_dev_tool_bridge \
    -p 9002:9002 \
    --ip \
    -v ~/.m2:/root/.m2 \
    -v ${HOST_LOG_DIR}:/var/log \
    --env-file \
    --cpus=${CPU} \
    -m=${MEMORY} \

But when I execute the curl below, I get:

curl ""
(7) Failed to connect to port 9002: Connection refused

Linux AWS
Docker version 17.06.2-ce, build 3dfb8343b139d6342acfd9975d7f1068b5b1c3d3

I have executed the command:
sysctl net.ipv4.conf.all.forwarding=1 and sudo iptables -P FORWARD ACCEPT on the server and restarted docker service.


I would like communication back n forth between the container and host. How do I do that?

Thank you in advance for your help.

NOTE: The containers can talk to each other just fine.

(Sam) #2

what in the second container is actually listening on port 9002?

the -p mapping is only documentation, it does not MAKE an application in the container actually USE that port

(Ravimeetsu) #3

@sdetweil I have a tomcat application / web app running on port 9002. And I use -p to Publish a container᾿s port or a range of ports to the host

What am I missing? How do I have the host send a curl: and it reach the application listening on 9002? I am using as an example. What IP can I use?

(Sam) #4

what is the IP address of the container?
use docker inspect

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id