4

I've installed djangorestframework with markdown and django filter using pip in virtualenv in django docker container, checked via pip freeze. The absolute path in OS X is /Users/user/project/denv/lib/python2.7/site-packages. Added 'rest_framework', in settings.py but still after docker-compose up get the following error. I guess it has something to do with wrong path, but have no idea how to fix that.

Traceback (most recent call last):
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
web_1  |     fn(*args, **kwargs)
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
web_1  |     autoreload.raise_last_exception()
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
web_1  |     six.reraise(*_exception)
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
web_1  |     fn(*args, **kwargs)
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
web_1  |     apps.populate(settings.INSTALLED_APPS)
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
web_1  |     app_config = AppConfig.create(entry)
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/apps/config.py", line 90, in create
web_1  |     module = import_module(entry)
web_1  |   File "/usr/local/lib/python2.7/importlib/__init__.py", line 37, in import_module
web_1  |     __import__(name)
web_1  | ImportError: No module named rest_framework

EDIT: terminal screenshot enter image description here

EDIT2: 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

EDIT2: I suppose that error is because of application actually using docker's python not virtualenv python.

Is it possible? How to either install djangorestframework to docker's python packages or better make django application use own python ?

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
  • @DanLowe I call it by 'source denv/bin/activate' and any command is called with (denv) in line –  Feb 25 '17 at 20:18
  • @AllІѕVаиітy so how to install djangorestfrmawork to docker's python or to make app to use env's python? it's likely to be the issue –  Feb 25 '17 at 20:27
  • @AllІѕVаиітy ok, it doesn't really matter to me. I care about make it work. With djangorestframework. With or without virtualenv. You know how?) –  Feb 25 '17 at 20:35

2 Answers2

7

You don't need to pip install manually. The following line in your dockerfile will install requirements.

RUN pip install -r requirements.txt

You just need to add djangorestframework to your requirements.txt

# previous stuff Django etc...
djangorestframework

and run

docker-compose up --build
All Іѕ Vаиітy
  • 24,861
  • 16
  • 87
  • 111
  • it's activated. and it was activated when I was installing those things –  Feb 25 '17 at 16:55
  • denv is actually my virtual environment –  Feb 25 '17 at 16:56
  • it's docker django app. there is docker-compose up instead of python manage.py runserver. it was always activated before I ran the server –  Feb 25 '17 at 17:10
0

Can you show us your Dockerfile ?

It should look like this :

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

actually using docker's python

Yes, building an Docker-Image withFROM python:2.7installs a fresh Python Environment.

not virtualenv python.

Yes, unless you copy your virtualenv inside your Docker-Image and activate it there (which you should not do).

it's activated. and it was activated when I was installing those things

It doesn't matter unless you pip freeze > requirements.txt and use RUN pip install -r requirements.txt inside your Dockerfile, which will install all your Python-Dependencies inside your virtualenvin the Python-Environment inside your Docker-Image.

Maik3141
  • 33
  • 1
  • 6