skip to Main Content

I have a docker-compose with flask app and some tests written in python to test the api. The api is working fine but the containers with the tests return this error :
equests.exceptions.ConnectionError: HTTPConnectionPool(host=’my_api’, port=5000): Max retries exceeded with url: /Authorization (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x7f02ff5abf10>: Failed to establish a new connection: [Errno 111] Connection refused’)).

here is my docker-compose.yml :

version: "3.9"

networks:
  app_subnet:
    external: true
    

services:
       api:
         image: my_api
         container_name: my_api
         networks:
              app_subnet :
                    ipv4_address: 172.16.0.10
         ports:
              - "5000:5000"
              #- target: 5000
               # published: 5000
               # protocol: tcp
               # mode: host
       
       authentication:
          image: authentication
          depends_on:
              - api
          container_name: authentication_test
          networks:
                    - app_subnet 
                        
          volumes:
              - /home/oussama/Downloads/projet2/:/home/


volumes:
  shared_volume:
       name: shared_volume
       driver: local
       driver_opts:
              type: 'none'
              o: 'bind'
              device: '/home/oussama/Downloads/projet2'

one of the tests that i am running :

import os
import requests

# définition de l'adresse de l'API
api_address = 'my_api'
# port de l'API
api_port = 5000


r = requests.get(
    url='http://{address}:{port}/Authorization'.format(address=api_address, port=api_port),
    headers= {
        'Authorization': 'alice=wonderland'
        }
)

output = '''
============================
    Authentication test
============================

request done at "/Authorization"
| username="alice"
| password="wonderland"

expected result = 200
actual restult = {status_code}

==>  {test_status}

'''


# statut de la requête
status_code = r.status_code

# affichage des résultats
if status_code == 200:
    test_status = 'SUCCESS'
else:
    test_status = 'FAILURE'
print(output.format(status_code=status_code, test_status=test_status))

the api is working after the docker-compose and i get the results that i want with curl command on the port 5000. I checked and no other process is using this port. I tried to change the api_adress in the test code to the ipv4_address that i fixed in the yml file or used the api container_name directly and both times i have the same error : Failed to establish a new connection: [Errno 111] Connection refused’

Any ideas please ?

2

Answers


  1. Chosen as BEST ANSWER

    in my flask app i end it with the following : if name == "main": app.run(host="0.0.0.0", port=5000)

    in the dockerfile i expose the port 5000 and the if i do : docker container run -p 5000:5000 -d my_api:latest

    the api responds to my curl command using localhost


  2. You need to make sure your Flask application is listening on the correct address as you’ve listed in the docker-compose file here.

    localhost does NOT work, because of different network namespaces that each container has, it means a different host for different containers.

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