Docker Community Forums

Share and learn in the Docker community.

How to connect to container from Docker Host

docker

(Ravimeetsu) #1

Hello,

I have created a used defined bridge networK :

docker network create -d bridge --subnet 10.2.0.0/24 --gateway 10.2.0.1 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 10.2.0.99 \
    --cpus=${CPU} \
    -m=${MEMORY} \
    -e AUTH=no \
    --rm \
    mongo:3.6-jessie

source-analysis docker command:

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

But when I execute the curl below, I get:

curl "http://10.2.0.1:9002/v1/codeStats/overall"
(7) Failed to connect to 10.2.0.1 port 9002: Connection refused

System:
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.

Question:

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: http://127.0.0.1:9092/foo/bar and it reach the application listening on 9002? I am using 127.0.0.1 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