skip to Main Content

I have two docker containers, one with a fastAPI back-end, one with a streamlit app as front-end. This is my docker compose yaml:

version: '3.9'

services:
  semcity-frontend:
    image: semcity-frontend
    environment:
      - SEMCITY_BACKEND_URL=http://semcity-backend:8003/
    ports:
      - 8004:8004
    networks:
      local:
    depends_on:
      - semcity-backend

  semcity-backend:
    image: semcity-backend
    environment:
      - OPENAI_API_KEY=<api-key-here...>
    ports:
      - 8003:8003
    networks:
      local:

networks:
  local:
    external: true

When I run this (docker-compose -f compose.yaml up), it spins up the two containers. But upon sending a request from the streamlit GUI in my browser, I get:

semcity-semcity-frontend-1  | requests.exceptions.ConnectionError: HTTPConnectionPool(host='semcity-backend', port=8003): Max retries exceeded with url: /qa?question=asd&celex_ids=32015L2366&max_len=300 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f9cb4183950>: Failed to establish a new connection: [Errno 111] Connection refused'))

My fastAPI app has a /qa endpoint like so:

@app.post("/qa")
def submit(question: str, celex_ids: str, max_len: int):
    relevant_celex_ids = celex_ids.split('_')
    paragraphs, scores, celex_ids, article_ids, paragraph_ids = engine.rank_by_relatedness(question, relevant_celex_ids, 10)
    context = create_context(zip(celex_ids, paragraphs, paragraph_ids, article_ids))
    answer = answer_question(context, question)
    return answer

In my streamlit code, I’m getting the URL from a sys env variable, like so:

BACKEND_URL = os.environ.get('SEMCITY_BACKEND_URL')
url = ('%sqa?question=%s&celex_ids=%s&max_len=%i' %
           (BACKEND_URL, question, '_'.join(celex_ids), max_words))

if st.button('Ask!'):
    res = requests.post(url)

This seems to do something, since the error message tells me that it tried at (host='semcity-backend', port=8003) (it’s apparently getting that much from my docker compose file). It doesn’t seem to get all the way there though. From what I’ve understood (definitely not an expert at docker though), if both containers share a "networks" (i.e., "local" in my case), they should be able to refer to each other by the service name. I’m not sure if this is ultimately a docker problem, but running my (python) code locally without docker, it all works, so I’m a bit lost. Any thoughts or ideas on what could be the problem would be greatly appreciated!

2

Answers


  1. Chosen as BEST ANSWER

    For others running into similar issues; found my answer through the first comment by @Botje. My fastAPI app was running on 127.0.0.1. Changing that to 0.0.0.0 solved my problem, so not an issue with my docker compose setup, apparently.


  2. network needs to be a list:

     version: '3.9'
    
    services:
      semcity-frontend:
        image: semcity-frontend
        environment:
          - SEMCITY_BACKEND_URL=http://semcity-backend:8003/
        ports:
          - 8004:8004
        networks:
          - local
        depends_on:
          - semcity-backend
    
      semcity-backend:
        image: semcity-backend
        container_name: semcity-backend
        environment:
          - OPENAI_API_KEY=<api-key-here...>
        ports:
          - 8003:8003
        networks:
          - local
    
    networks:
      local:
        external: true
    
    

    You put it as an attribute.

    local:
    
    services:
      frontend:
        image: example/webapp
        networks:
          - front-tier
          - back-tier
    
    networks:
      front-tier:
      back-tier:
    ``
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search