使用 docker 的容器化 python 应用程序无法正确运行

问题描述 投票:0回答:1

在本地运行以下 app.py 时,我有一个团队聊天网络服务器运行得非常好。但是当我尝试使用 docker 将其容器化时,它没有正确运行 Web 服务器。它在警告时停止,并且不会进入服务器运行部分。是什么导致了这种行为。

app.py 上的网络服务器部分

# Listen for incoming requests on /api/messages.
async def messages(req: Request) -> Response:
    # Main bot message handler.
    if "application/json" in req.headers["Content-Type"]:
        body = await req.json()
    else:
        return Response(status=HTTPStatus.UNSUPPORTED_MEDIA_TYPE)

    activity = Activity().deserialize(body)
    auth_header = req.headers["Authorization"] if "Authorization" in req.headers else ""

    response = await ADAPTER.process_activity(activity, auth_header, BOT.on_turn)
    if response:
        return json_response(data=response.body, status=response.status)
    return Response(status=HTTPStatus.OK)


def init_func(argv):
    APP = web.Application(middlewares=[aiohttp_error_middleware])
    APP.router.add_post("/api/messages", messages)
    return APP

if __name__ == "__main__":
    
    APP = init_func(None)
    try:
        web.run_app(APP, host="0.0.0.0", port="3978")
    except Exception as error:
        raise error

我的dockerfile是这样的。

# Use an official Python runtime as a parent image
FROM python:3.10.11-slim

# Set the working directory in the container to /app
WORKDIR /app

# Add the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Run app.py when the container launches
CMD ["python", "app.py"]

我也尝试过将此 cmd 用作此 CMD

["python3.10", "-m", "aiohttp.web", "-H", "0.0.0.0", "-P", "8000", "app:init_func"]
。我在这里做错了什么。

python docker dockerfile botframework
1个回答
0
投票
# Make port 3978 available to the world outside this container
EXPOSE 3978

# Run app.py when the container launches
CMD ["python", "-u", "app.py"]

这解决了问题。

© www.soinside.com 2019 - 2024. All rights reserved.