5

I am running simple Docker-compose example shown here

Here is Dockerfile:

 FROM python:2.7
 ENV PYTHONUNBUFFERED 1
 RUN mkdir /code
 WORKDIR /code
 ADD requirements.txt /code/
 RUN pip install -r requirements.txt
 ADD . /code/

Here is docker-compose.yml:

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

When I am trying to launch the example with docker-compose it gives me following:

alex@universe:~/projects/testcompose$ docker-compose up
testcompose_db_1 is up-to-date
Recreating testcompose_web_1
Attaching to testcompose_db_1, testcompose_web_1
web_1  | python: can't open file 'manage.py': [Errno 2] No such file or directory
db_1   | The files belonging to this da...

Probably tutorial is not up to date so if you know answer please write it here.

2
  • 1
    Have you ran this command: docker-compose run web django-admin.py startproject composeexample . inside folder with docker-compose.yml? It will actually create manage.py and all Django related files inside Docker Volume. Commented Oct 18, 2016 at 22:13
  • Yes, I did it. Actually the problem was that after running this command the project structure was not correct. It has created additional composeexample folder with project and manage.py file inside. After moving all the files one level up everything was working as expected. Commented Oct 19, 2016 at 21:03

5 Answers 5

4

Right, project file structure was generated correctly. Django always has a my-project-folder/my-site/my-site file structure.

To make it work with docker-compose you just need to move up the following files to my-project-folder/my-site/

Dockerfile
docker-compose.yml
requirements.txt

my-project-folder/my-site/ already contains manage.py file (if that is not the case then something is wrong)

At this point make sure that my-project-folder/ only contains my-site/ folder and nothing else.

Once everything is setup correctly just move up to my-project-folder/my-site/my-site execute command docker-compose up it will build everything needed. execute command docker-compose up a second time and there you have it.

Dont forget to check allowed host and add '0.0.0.0' on settings.py file if needed.

that's all.

Sign up to request clarification or add additional context in comments.

Comments

2

If you're using docker-machine or Docker Toolbox this is likely caused because the project is not under the /Users directory. Only that directory is availabled as a shared folder in the VM.

You can remove the volumes: from the config, or add another shared folder to Virtualbox.

1 Comment

@dhephin thanks for the answer! I am not using docker-machine on Ubuntu. The problem was with not correct project structure after starting Django project inside container.
1

I arrived here with the same error message, turns out I was simply forgetting to add the dot . at the end of the below command:

docker-compose run web django-admin.py startproject composeexample .

Comments

1

I also faced the same issue while following this docker basic tutorial https://docs.docker.com/compose/django/

So, basically I was at the same stage and I am also noob in docker. My approach was first of all I used the below command to identify the contents inside my image directory make sure your django project is available in that directory

docker run -it container_name sh

after that you will be getting this error when you run docker-compose up command

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

So, basically docker is executing instructions from your Dockerfile and it is executing your python manage.py runserver command in code folder which doesn't contain your manage.py file.

So, add this line it will navigate you to the folder which consists manage.py file

CMD . /code/composeexample/

and now build your project again with

docker-compose build

Now run the command below and you are good to go

docker-compose up

1 Comment

Where should I add CMD . /code/composeexample/ ?
0

I was using pipenv which creates virtualenv, my problem was i forgot use

RUN pipenv shell

to enable virtualenv.

but using pipenv shell in container makes some problem so the solution is use --system option with pipenv install command

RUN pipenv install --system

which causes packages to be installed on container itself and not on virtualenv in the container.

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.