Freddy
Freddy

Reputation: 876

Running Django in docker-compose

I have a problem starting a django project inside a docker container. My Dockerfile looks as follows:

FROM python:2.7
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
EXPOSE 8000

The requirements.txt consists of the single line Django which install django successfully.

My docker-compose.yml has the following content:

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

To create a project I am calling:

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

which crashes with the following message:

python: can't open file 'django-admin.py': [Errno 2] No such file or directory

Also this

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

crashes with

ERROR: Cannot start service web: oci runtime error: container_linux.go:247: starting container process caused "exec: \"django-admin.py\": executable file not found in $PATH"

It seems to me that there is an error in the $PATH when executing the container like this. When I use the interactive mode -it, I am able to call the django-admin from this folder. Is the PATH not set correctly or am I doing something else wrong here? The Dockerfile is in the directory web.

Edit: After the suggestion in the comment, I tried to run the django-admin with the full path:

root@935ca5543589:/code# which django-admin
/usr/local/bin/django-admin

Still no change:

docker-compose run web /usr/local/bin/django-admin startproject web .
ERROR: Cannot start service web: oci runtime error: container_linux.go:247: starting container process caused "exec: \"/usr/local/bin/django-admin\": stat /usr/local/bin/django-admin: no such file or directory"

Upvotes: 6

Views: 5634

Answers (2)

Gyulnara Grigoryan
Gyulnara Grigoryan

Reputation: 31

What I ran :

docker-compose run app djando-admin.py startproject app

Output:

sh: djando-admin.py: not found

Solution:

sudo docker-compose run app django-admin startproject app .

Try to use sudo.

Upvotes: 2

Freddy
Freddy

Reputation: 876

The answer is very simple actually. I had an error in my Dockerfile when I ran the project first. After changing the Dockerfile, docker-compose did not rebuild the image automatically which I was assuming. Even rebuilding with docker build did not solve this. Only after deleting the whole image, a rebuild was forced and gave me the correct result.

Upvotes: 3

Related Questions