I’m new to docker, and I tried to run django on the docker container.
However, after I ran the "docker-compose up -d" command the error
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.
My questions are:
1.Why the this error happens?
2.How I can fixed it?
The 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
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!!
2
Answers
You should change your docker-compose file command like this because the manage.py file is present in the project folder.
Your Dockerfile is copying the current directory tree into the image’s
/app
directory, which is pretty normal. When the Composecommand:
override runs, you’re still in that/app
directory, but your application code is in aproject
subdirectory.You should be able to see this launching temporary containers to look at the built filesystem in the image, like
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. It’s also a little better practice to put theCMD
in the Dockerfile, so that you could independentlydocker run
the image without its Compose setup.In your
docker-compose.yml
file you don’t need thecommand:
(it’s the same as the DockerfileCMD
) orvolumes:
block (which will cause the code in the image to be ignored and replaced with something else) and I’d delete these lines.