我有两个 docker 容器,一个具有 fastAPI 后端,一个具有 Streamlit 应用程序作为前端。这是我的 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
当我运行这个(
docker-compose -f compose.yaml up
)时,它会旋转两个容器。但是,当我从浏览器中的 Streamlit GUI 发送请求时,我得到:
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'))
我的 fastAPI 应用程序有一个 /qa 端点,如下所示:
@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
在我的 Streamlit 代码中,我从 sys 环境变量获取 URL,如下所示:
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)
这似乎做了一些事情,因为错误消息告诉我它尝试了
(host='semcity-backend', port=8003)
(它显然从我的docker compose文件中获取了这么多)。但它似乎并没有完全实现。根据我的理解(虽然绝对不是 docker 专家),如果两个容器共享一个“网络”(即我的情况下的“本地”),它们应该能够通过服务名称相互引用。我不确定这是否最终是一个 docker 问题,但是在没有 docker 的情况下在本地运行我的(python)代码,一切都有效,所以我有点迷失了。任何关于可能出现问题的想法或想法将不胜感激!
网络需要是一个列表:
semcity-frontend:
networks:
- local
semcity-backend:
networks:
- local
你把它作为一个属性。
local: