'port mapping/forwarding between two docker container is not working
I have two docker containers C1 and C2. C1 has a python script which sends an HTTP req using requests.get('http://127.0.0.1:8034/') and C2 has a server listening on 127.0.0.1:8034 however C1 is not able to connect to C2 on 127.0.0.1:8034 and shows err msg "ConnectionError". Though both C1 and C2 are on the same network, and I am able to ping from C1 to C2 using their secondary interfaces, i.e. between 172.21.0.4(C2) and 172.21.0.5(C2). I thought exposing port 8034 on C2 will establish reachability between C1 and C2 via 127.0.0.1 interface but did not work. Can anyone let me know the fix for this? Note that I have reachability between my host to C2 127.0.0.1:8034, as I have add -port setting in my docker-compose.yaml file.
C1 -------- trying to connect via 127.0.0.1:8034 ---------- C2
ip1:172.21.0.4 ip1: 172.21.0.5
ip2:127.0.0.1 ip2: 127.0.0.1(listens on 8034)
my docker compose file
C1:
build: ./C1
volumes:
- ./C1:/C1
ports:
- "127.0.0.1:8000:80"
C2:
container_name: C2
image: c2_img:latest
environment:
CUSTOM_CONFIG_LOCATION: "/config.json"
ports:
- "127.0.0.1:8016:8034" ----> connectivity from host to docker container is working
expose:
- "8034" ----> added with an intention that it will establish connectivity between C1 to C2. but did not work
volumes:
- ./config.json:/config.json
Is this issue something as I am using both containers with 127.0.0.1 and sending req from C1 on 127.0.0.1:8034 makes C1 confused whether the req would be served from local container C1 or it would server by remote C2? But that is what port mapping is meant for between containers. Isn't it? Please suggest a fix.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
