skip to Main Content

I want to be able to connect to a third-party .NET API I am running as a docker image within a container. When I connect to the ip address and port on my browser it works though when I try and run a get request in python it does not. I am not sure what I am doing wrong.

The Django python application is an image within the container using 0.0.0.0:8000.

This is the docker-compose.yml file for a .NET image I am running from within a container:

mt4rest:
    image: mt4rest:latest
    ports:
      - "0.0.0.0:5000:80"

Here are the ports as per docker inspect

  "Ports": {
                "443/tcp": null,
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "5000"
                    }
                ]
            },

When I run

http://0.0.0.0:5000/Ping

or

http://127.0.0.1:5000/Ping

in my browser, I get OK though when I run the same endpoints in Python:

response = requests.get('http://0.0.0.0:5000/Ping')

or

response = requests.get('http://127.0.0.1:5000/Ping')

I receive the following error:

requests.exceptions.ConnectionError: HTTPConnectionPool(host='0.0.0.0', port=5000):
Max retries exceeded with url: /Ping (Caused by
NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fe7aa5af640>: 
Failed to establish a new connection: [Errno 111] Connection refused'))

or

requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=5000): 
Max retries exceeded with url: /Ping (Caused by 
NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f6322e2b640>: 
Failed to establish a new connection: [Errno 111] Connection refused'))

I understand this to mean that the address for the request is not found.

I am not sure what I am doing wrong.

2

Answers


  1. Chosen as BEST ANSWER

    There is an acceptable answer for this question on stackoverflow which solves the problem:

    Python requests in Docker Compose containers


  2. Lets call your host as HOST_BASE.
    You are having two containers, one is exposing 5000 on host. Its HOST_BASE:5000.
    Other is your python-app container.

    Now, you are running another python-app in container on host HOST_BASE. And try to hit: http://0.0.0.0:5000, which is not accessible. Note: its 0.0.0.0 is localhost of the container itself.

    You want to connect to HOST_BASE:5000, which is not localhost or 0.0.0.0
    You need to configure networking between your two containers. Or, for simplicity, use --network host while running both of your container.

    Hope that helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search