skip to Main Content

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


  1. You should change your docker-compose file command like this because the manage.py file is present in the project folder.

    version: '3'
    services:
    web:
        build: .
        command: python3 project/manage.py runserver 0.0.0.0:8000
        volumes:
          - .:/app
        ports:
          - "8000:8000"
        depends_on:
          - db
    
    Login or Signup to reply.
  2. Your Dockerfile is copying the current directory tree into the image’s /app directory, which is pretty normal. When the Compose command: override runs, you’re still in that /app directory, but your application code is in a project subdirectory.

    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. 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.

    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.

    version: '3.8'  # "3" means "3.0"
    services:
      web:
        build: .
        ports:
          - "8000:8000"
        depends_on:
          - db
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search