6

I want to create with docker-compose 2 Docker containers. 1 for DB (Mongo) and 1 for web (Django). Here are my files

docker-compose.yml

version: '3'
services:
  db:
    image: mongo
    command: mongod
  web:
    build: code/
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

code/Dockerfile

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY cleverInvestWeb/ /code/

In the directory code/cleverInvestWeb/ is my manage.py file from django.

when i run docker-compose up it throws me the following error:

web_1  | python: can't open file 'manage.py': [Errno 2] No such file or directory

When i start the container via Docker itself with docker exec -it dockerdiplom_web bash and do an ls there it shows the manage.py file.

Do you have an idea why docker-compose doesn't find my file when starting?

1
  • 2
    Does it work if you remove volumes:? If you docker-compose run web sh with and without that declaration, what do you see inside the running containers? Commented Sep 1, 2019 at 11:22

4 Answers 4

8

The directory structure in your Dockerfile and docker-compose seems confusing. Another thing that is strange is you are able to see to file after docker exec.

You copy your code to COPY cleverInvestWeb/ /code/ in Dockerfile, and then mount the volume in Docker-compose to .:/code so everything will be overide in the existing image on /code location.

I assume your python file place inside local directory cleverInvestWeb so docker-compose boot up it will override the existing image code and your file update location will be code/cleverInvestWeb/manage.py or /code/manage.py

To debug:

  • remove mounting in docker-compose and check if it works
  • command: tail -f /dev/null set this command in docker-compose and verify your file location using docker exec
Sign up to request clarification or add additional context in comments.

1 Comment

@David Maze comment and your answer were helpful. I removed the volumessection in my docker-compose.yml file and corrected the command with python /code/manage.py runserver 0.0.0.0:8000. Thank you guys it works now! :D
2

This is a common error among the Django beginners

The docker file 'docker-compose.yml' configuration (shown at the beginning of this request) is alrigh... Except for the django project folder missing when calling the manage.py file!

The Line should have been:

command: python nameOfDjangoFolder/manage.py runserver 0.0.0.0:8000

Explaination about Volumes named "/code" The Volume "/code" is building your application using the root of the Docker project folder:

/home/user/Documents/youDockerProjectFolder = (Code)

Inside the "/home/user/Documents/youDockerProjectFolder" you should find the Django project folder: yourDjangoProjectFolder/

Inside that folder you will find the manage.py... So it's normal to have to use yourDjangoProjectFolder/manage.py in order to call manage.py

MORE but not important for this solution Inside the Docker Project Folder another important file is found yourDjangoProjectFolder/youDjangoProjectFolder/settings.py

Comments

1

change your command to

command: python code/manage.py runserver 0.0.0.0:8000

1 Comment

same problem web_1 | python: can't open file 'code/manage.py': [Errno 2] No such file or directory
0

I encountered this problem while deploying to Digital ocean from github repo using Dockerfile. For my case I had accidentally added manage.py to .gitignore file. So, the my github repo never the had the manage.py file.

You should ensure that manage.py file is actually present in its expected directory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.