Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

339
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda