python - Dockerfile指定API的路径

标签 python docker docker-compose

我是第一次使用 docker。我有一个 Angular 的 Web 应用程序和一个 python/flask 的后端应用程序。经过一番努力,我终于设法让一切正常工作,但是 docker 在我的后端找不到我的 API 程序:

我的服务器文件位于/my_backend/src/server.py
我的 docker 文件位于/my_backend/Dockerfile.dockerfile

Dockerfile.dockerfile的内容:

FROM python:3.7-slim
COPY /src .
WORKDIR /src/app
ENV PYTHONUNBUFFERED 1
COPY requirements.txt /tmp/requirements.txt
RUN python3 -m pip install -r /tmp/requirements.txt
CMD ["python","server.py"]

命令提示符中的错误消息是

Attaching to backend, frontend
backend   | python: can't open file 'server.py': [Errno 2] No such file or directory
backend exited with code 2

欢迎询问更多信息。
我使用了这个教程:https://medium.com/analytics-vidhya/dockerizing-angular-application-and-python-backend-bottle-flask-framework-21dcbf8715e5

最佳答案

如果您的脚本位于 /src 目录中,请勿使用 WORKDIR/src/app 而是使用 WORKDIR/src CMD ["python","../server.py"]

解决方案1:

FROM python:3.7-slim
COPY /src .
WORKDIR /src    # <- HERE
ENV PYTHONUNBUFFERED 1
COPY requirements.txt /tmp/requirements.txt
RUN python3 -m pip install -r /tmp/requirements.txt
CMD ["python","server.py"]

解决方案2:

FROM python:3.7-slim
COPY /src .
WORKDIR /src/app
ENV PYTHONUNBUFFERED 1
COPY requirements.txt /tmp/requirements.txt
RUN python3 -m pip install -r /tmp/requirements.txt
CMD ["python","../server.py"]     # <- HERE, or "/src/server.py"

@TheFool 建议:

FROM python:3.7-slim
WORKDIR /src/app  # Swap WORKDIR and COPY
COPY /src .
ENV PYTHONUNBUFFERED 1
COPY requirements.txt /tmp/requirements.txt
RUN python3 -m pip install -r /tmp/requirements.txt
CMD ["python","server.py"]

关于python - Dockerfile指定API的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71011112/

相关文章:

python - 使用不同的用户代理和 IP 地址抓取不同的 URL

python - 如何防止plotly.figure_factory.create_table将整数转换为浮点值?

python - 使用 pykml 将扩展数据添加到 python 中的地标

Python 重新编译。不平衡的括号错误

docker - Docker运行-即使Docker图像显示存在图像,也无法在本地找到图像

Docker - Prometheus 容器立即死亡

docker从容器连接到主机隧道

docker - 阻止在撰写文件中定义的发布端口

Docker 无法连接到容器暴露端口

docker - 错误 : Get https://registry-1. docker.io/v2/: net/http: Docker 中的 TLS 握手超时