英文:
Unable to start FastAPI app with docker-compose
问题
The api
容器由于exited with code 2
错误而停止。
api-1 exited with code 2
...
我的docker-compose.yaml
:
version: "3.8"
services:
api:
image: python:3.9
restart: always
command: ["python3", "Main.py"]
build:
dockerfile: ./api/Dockerfile
volumes:
- type: bind
source: ./api
target: /app
ports:
- 17002:17002
Dockerfile
:
FROM python:3.9
WORKDIR /code
COPY ./api/requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
COPY ./api /code/app
CMD ["python3", "/code/app/Main.py"]
文件结构:
docker-compose.yaml
api
Dockerfile
Main.py
如何解决这个问题?
英文:
The api
container stops due to the exited with code 2
error.
api-1 exited with code 2
...
My docker-compose.yaml
:
version: "3.8"
services:
api:
image: python:3.9
restart: always
command: ["python3", "Main.py"]
build:
dockerfile: ./api/Dockerfile
volumes:
- type: bind
source: ./api
target: /app
ports:
- 17002:17002
Dockerfile
:
FROM python:3.9
WORKDIR /code
COPY ./api/requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
COPY ./api /code/app
CMD ["python3", "/code/app/Main.py"]
File structure:
docker-compose.yaml
api
Dockerfile
Main.py
How do I solve this issue?
答案1
得分: 0
删除几乎所有的Compose设置,只留下以下部分:
version: "3.8"
services:
api:
# restart: always
build:
dockerfile: ./api/Dockerfile
ports:
- 17002:17002
系统也会处于稍微损坏的状态;运行以下命令:
docker pull python:3.9
docker-compose build
docker-compose up -d
command:
和 volumes:
在Dockerfile中覆盖了一些内容,是不必要的。如果Dockerfile是正确的(在我看来它看起来合理),那么在镜像内部,当前工作目录是 /code
,默认命令运行 /code/app/Main.py
脚本,并将主机的 ./api
目录的内容复制到其中。volumes:
挂载会在运行中的容器文件系统中创建一个第二份不必要的代码副本,而 command:
覆盖尝试从当前目录(仍然是 /code
)运行 Main.py
;文件不在那里(它在 /code/app
中),这就是你出现启动错误的原因。
Compose文件中的 image:
具有不幸的交互。如果同时使用了 build:
和 image:
,则 image:
指定了要构建的镜像的名称(想象一下将其设置为应用程序的Docker Hub名称,例如)。当你指定 image: python:3.9
时,实际上会在本地系统上用你本地构建的内容覆盖 Docker Hub Python 镜像,因此当下一个构建开始时 FROM python:3.9
实际上会修改你之前构建的镜像。
restart: always
正是它听起来的那样。你的问题之一是你的应用程序崩溃并立即重新启动,注释掉此行将使你有一些空间来调试容器,而不会进入无限重启循环。
英文:
Delete almost all of the Compose settings. Leave behind only:
version: "3.8"
services:
api:
# restart: always
build:
dockerfile: ./api/Dockerfile
ports:
- 17002:17002
Your system will also be in a slightly broken state; run
docker pull python:3.9
docker-compose build
docker-compose up -d
The command:
and volumes:
override things in the Dockerfile, and aren't necessary. If the Dockerfile is correct (and it looks reasonable to me) then, inside the image, the current working directory is /code
, the default command runs the script /code/app/Main.py
, and the contents of the host's ./api
directory are copied there. The volumes:
mount makes a second unnecessary copy of the code in /app
in the running container filesystem, and the command:
override tries to run Main.py
out of the current direcory instead (still /code
); the file isn't there (it's in /code/app
) and that's why you get the startup error.
image:
in the Compose file has what here is an unfortunate interaction. If you have both build:
and image:
, the image:
specifies the name of the image that is built (imagine setting it to a Docker Hub name for your application, for example). When you specify image: python:3.9
, you actually overwrite the Docker Hub Python image (locally on your system) with what you've built locally, so then when your next build starts FROM python:3.9
it actually amends your previously-built image.
restart: always
does exactly what it sounds like. One of your problems is that your application is crashing and immediately restarting, and commenting out this line will give you some space to debug the container without it going into an infinite restart loop.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论