[英]python3: can't open file '/app/manage.py': [Errno 2] No such file or directory
I'm new to docker, and I tried to run django on the docker container.我是 docker 的新手,我尝试在 docker 容器上运行 django。 However, after I ran the "docker-compose up -d" command the error但是,在我运行“docker-compose up -d”命令后出现错误
python3: can't open file '/app/manage.py': [Errno 2] No such file or directory
shows in docker. Since it seems that the code can be run successfully in mac os, I doubt if it is the problem of Windows11 which I run the file currently.显示在docker。由于代码在mac os中貌似可以成功运行,我怀疑是不是我目前运行文件的Windows11的问题。
My questions are: 1.Why the this error happens?我的问题是: 1.为什么会出现这个错误? 2.How I can fixed it? 2.我该如何解决?
The dockerfile: dockerfile:
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /app
WORKDIR /app
COPY requirements.txt /app/
RUN pip install -r requirements.txt
COPY . /app/
the docker-compose file docker-compose 文件
version: '3'
services:
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/app
ports:
- "8000:8000"
depends_on:
- db
The structure of my project directory:我的项目目录结构:
├── project
| ├──project
│ ├── __init__.py
│ ├── asgi.py
| ├── setting.py
| ├── urls.py
│ └── wsgi.py
| ├── manage.py
├── docker-compose.yml
├── Dockerfile
└── requirements.txt
I've tried to solve the problem for hours, but the solutions I searched did not work.我试图解决这个问题几个小时,但我搜索的解决方案没有用。 Hope someone can help.希望有人能帮忙。 Thank you very much!!非常感谢你!!
You should change your docker-compose file command like this because the manage.py file is present in the project folder.您应该像这样更改 docker-compose 文件命令,因为 manage.py 文件存在于项目文件夹中。
version: '3'
services:
web:
build: .
command: python3 project/manage.py runserver 0.0.0.0:8000
volumes:
- .:/app
ports:
- "8000:8000"
depends_on:
- db
Your Dockerfile is copying the current directory tree into the image's /app
directory, which is pretty normal.您的 Dockerfile 正在将当前目录树复制到图像的/app
目录中,这很正常。 When the Compose command:
override runs, you're still in that /app
directory, but your application code is in a project
subdirectory.当 Compose command:
override 运行时,您仍在该/app
目录中,但您的应用程序代码位于project
子目录中。
You should be able to see this launching temporary containers to look at the built filesystem in the image, like你应该能够看到这个启动临时容器来查看图像中构建的文件系统,比如
docker-compose run web ls
docker-compose run web ls ./project
There are a couple of ways to approach this ( @Divyessh's answer should work as well) but the most straightforward might be to make the project
subdirectory be the current directory when running the container.有几种方法可以解决这个问题( @Divyessh 的回答也应该有效),但最直接的可能是在运行容器时使project
子目录成为当前目录。 It's also a little better practice to put the CMD
in the Dockerfile, so that you could independently docker run
the image without its Compose setup.将CMD
中也是一个更好的做法,这样您就可以独立docker run
图像而无需其 Compose 设置。
FROM python:3
ENV PYTHONUNBUFFERED 1
WORKDIR /app # creates the directory, don't need to RUN mkdir
COPY requirements.txt ./ # shorter and safer to COPY into current directory
RUN pip install -r requirements.txt
COPY ./ ./
# add the following
WORKDIR /app/project # switch to project subdirectory
EXPOSE 8000 # document container-side port
CMD python3 ./manage.py runserver 0.0.0.0:8000 # standard command to run
In your docker-compose.yml
file you don't need the command:
(it's the same as the Dockerfile CMD
) or volumes:
block (which will cause the code in the image to be ignored and replaced with something else) and I'd delete these lines.在您的docker-compose.yml
文件中,您不需要command:
它与 Dockerfile CMD
相同)或volumes:
块(这将导致图像中的代码被忽略并替换为其他内容)我会删除这些线。
version: '3.8' # "3" means "3.0"
services:
web:
build: .
ports:
- "8000:8000"
depends_on:
- db
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.