Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

340
Vistas
Communicating between 2 flask apps in docker containers

With 2 flask apps inside 2 separate containers, I thought it would be possible to run a GET request from one and return information from the second, but I get [Errno 111] Connection refused. With the following setup:

app1.py

@app.route('/get_message')
def get_message():
    message = requests.get('http://web2:5001/return_message')
    return message.text

if __name__ == '__main__':
    app.run(host='0.0.0.0')

app2.py

@app.route('/return_message')
def return_message():
    return 'the message'

if __name__ == '__main__':
    app.run(host='0.0.0.0')

docker-compose.yml

version: "3.7"
services:
  web1:
    build: ./web1
    container_name: web1
    ports: 
      - "5000:5000"
  web2:
    build: ./web2
    container_name: web2
    ports: 
      - "5001:5001"

The endpoint for app1 works when going to http://127.0.0.1:5000/get_message, but flask returns:

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

Trying to assign a static IP address to the web2 container and using the IP in the get request doesn't work, neither does using networks, link or depends_on inside the docker-compose file. Also tried exposing different ports on both containers in different combinations but doesn't get the message between them.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I think that when you spin your apps they both run on port 5000 but in different containers so try changing your app1.py to :

@app.route('/get_message')
def get_message():
    message = requests.get('http://web2:5000/return_message')
    return message.text

if __name__ == '__main__':
    app.run(host='0.0.0.0')

And also change ports in docker-compose for web2 :

version: "3.7"
services:
  web1:
    build: ./web1
    container_name: web1
    ports: 
      - "5000:5000"
  web2:
    build: ./web2
    container_name: web2
    ports: 
      - "5001:5000"

However changing ports in docker-compose is not necessary if you do not want to acces web2 from host - keep in mind that here I map port 5001 of your host to port 5000 of web2 container and both your containers expose app on port 5000.

Remember that these are seperate containers so like seperate environments. And also keep in mind that EXPOSE is only for documenting that you expose some service at this port - it does not make your app in the container run on this port.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda