Containers communication with python requests

心不动则不痛 提交于 2021-02-07 07:11:15

问题


Here is my architecture :

I have two containers (A and B) running on the same host with their own network.

docker-compose :

version : '3'
services: 

  A: 
    build: ./docker_A
    ports: 
      - "8090:8090"
    networks:
      - my_network

  B:
    build: ./docker_B
    ports: 
      - "8070:8070"    
    networks:
      - my_network

networks: 
  my_network:
    driver : bridge

Container b is running a bottle server :

@get('/')
def hello():
    return {"say":"Hello world"}

run(host='0.0.0.0', port=8070, debug=True)

docker inspect package_name_my_network returns :

{
        "Name": "package_name_my_network",
        "Id": "...",
        "Created": "...",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "...": {
                "Name": "package_name_A",
                "EndpointID": "...",
                "MacAddress": "...",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            },
            "...": {
                "Name": "package_name_B",
                "EndpointID": "...",
                "MacAddress": "...",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            }

I'm trying to do a GET request from A :

ret = requests.get('http://172.18.0.2:8070/')

But each time I get this response : <Response [503]> / Network Error (tcp_error)

I tried several things :

Changed 172.18.0.2 to :

  • B : KO
  • name_package_B : KO
  • 0.0.0.0 : KO

From host machine :

curl -X GET http://172.18.0.2:8070/ : {"say":"Hello world"}

From A : docker exec -t -i package_name_A /bin/bash : I can ping :

  • 172.18.0.2
  • B
  • package_name_B

If someone has a solution, it would be wonderful.

Thanks for your time.

EDIT :

It's a proxy problem. If I do unset https_proxy and unset http_proxy, I'm able to reach B with A. The problem is, I can't reach the server on the host (not a container) anymore. Even with EXPORT no_proxy=172.0.0.0 Any Idea ?


回答1:


Solved : It was a proxy problem. I'm using session without env var :

session = requests.Session()
session.trust_env = False
ret = session.get(url, json=my_json)


来源:https://stackoverflow.com/questions/50040023/containers-communication-with-python-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!