mrc
mrc

Reputation: 3153

Start docker container and start django project at the same time

I have the following docker-compose.yml file which works with no problem:

version: '2'
services:
  nginx:
    image: nginx:latest
    container_name: nz01
    ports:
      - "8001:8000"
    volumes:
      - ./src:/src
      - ./config/nginx:/etc/nginx/conf.d
    depends_on:
      - web
  web:
    build: .
    container_name: dz01
    depends_on:
      - db
    volumes:
      - ./src:/src
    expose:
      - "8000"
  db:
    image: postgres:latest
    container_name: pz01
    ports:
        - "5433:5432"
    volumes:
      - postgres_database:/var/lib/postgresql/data:Z
volumes:
    postgres_database:
        external: true

The problem is when I run docker-compose up I get this error:

python: can't open file 'manage.py': [Errno 2] No such file or directory
python: can't open file 'manage.py': [Errno 2] No such file or directory
[2018-10-26 12:41:59 +0000] [8] [INFO] Starting gunicorn 19.7.1
[2018-10-26 12:41:59 +0000] [8] [INFO] Listening at: http://0.0.0.0:8000 (8)
[2018-10-26 12:41:59 +0000] [8] [INFO] Using worker: sync
[2018-10-26 12:41:59 +0000] [12] [INFO] Booting worker with pid: 12
[2018-10-26 12:41:59 +0000] [12] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 578, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 126, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 135, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python2.7/site-packages/gunicorn/util.py", line 352, in import_app
    __import__(module)
ImportError: No module named C.wsgi
[2018-10-26 12:41:59 +0000] [12] [INFO] Worker exiting (pid: 12)
[2018-10-26 12:41:59 +0000] [8] [INFO] Shutting down: Master
[2018-10-26 12:41:59 +0000] [8] [INFO] Reason: Worker failed to boot.

I know that is because in my src folder does not exists the django-project C but I tried to automatically create it within the dockerfile using this script:

FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /src
RUN mkdir /static
WORKDIR /src
ADD ./src /src
RUN pip install -r requirements.pip
RUN django-admin startproject C
WORKDIR /C
CMD python manage.py collectstatic --no-input;python manage.py migrate; gunicorn C.wsgi -b 0.0.0.0:8000

I first execute docker-compose build and it works with no error:

docker-compose build
db uses an image, skipping
Building web
Step 1/10 : FROM python:2.7
 ---> 3c43a5d4034a
Step 2/10 : ENV PYTHONUNBUFFERED 1
 ---> Using cache
 ---> da201dd581f7
Step 3/10 : RUN mkdir /src
 ---> Using cache
 ---> 30938986b6e0
Step 4/10 : RUN mkdir /static
 ---> Using cache
 ---> ccc265f2cb16
Step 5/10 : WORKDIR /src
 ---> Using cache
 ---> 9886c1215843
Step 6/10 : ADD ./src /src
 ---> Using cache
 ---> 4b4861dc6d8d
Step 7/10 : RUN pip install -r requirements.pip
 ---> Using cache
 ---> e57283657840
Step 8/10 : RUN django-admin startproject bmat
 ---> Running in 0b25e38d9edd
Removing intermediate container 0b25e38d9edd
 ---> 1b39be297aac
Step 9/10 : WORKDIR /bmat
Removing intermediate container 5ed83a067b98
 ---> 9fd466c2d795
Step 10/10 : CMD python manage.py collectstatic --no-input;python manage.py migrate; gunicorn bmat.wsgi -b 0.0.0.0:8000
 ---> Running in 3beba0b212f0
Removing intermediate container 3beba0b212f0
 ---> d83eb0913d98
Successfully built d83eb0913d98
Successfully tagged bmat_web:latest
nginx uses an image, skipping

Then, I run docker-compose up -d which returns that all containers are up, but when in go to kinematic I find this problem mentioned above, and it is because the django-admin startproject C is not running.

There is an option to make it automatically run when build a docker container? Probably the best option is to use a first version of the dockerfile without executing nothing against the framework, build all the containers, create the project and then modify manually the dockerfile to add the corresponding migration and collectstatic functionabilities.

Thank you

Upvotes: 1

Views: 511

Answers (2)

mrc
mrc

Reputation: 3153

Finally I have it working. Thanks to @DavidMaze I fix the first error but it not solved the django project generation.

The final script is:

FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /src
RUN mkdir /static
WORKDIR /src
ADD ./src /src
RUN pip install -r requirements.pip
CMD django-admin.py startproject bmat; cd /src/bmat; python manage.py collectstatic --no-input;python manage.py migrate; gunicorn bmat.wsgi -b 0.0.0.0:8000

Now the Run django-admin statement is not in running within Docker, but using CMD inside the running container.

Upvotes: 0

David Maze
David Maze

Reputation: 158908

I suspect your larger problem is that the Dockerfile ends with:

WORKDIR /C
CMD python manage.py ...

But there is nothing at all in /C, everything is (I'm guessing) in /src/C. Try instead

WORKDIR /src/C

Separately, your docker-compose.yml file says:

volumes:
  - ./src:/src

which hides everything in /src in the container and replaces it with whatever happens to be in ./src on the host. In particular, that replaces pretty much everything that your Dockerfile does. While this is a common enough development setup (it avoids the step of re-running docker build after making edits and running tests) it's not what you'd actually run in production (you want to be able to use the image in isolation without separately copying around the source tree).

Try deleting these two lines, and using the copy of the tree that's actually in the image.

Upvotes: 2

Related Questions