How to add a Python module to a docker container?
Asked Answered
T

3

6

I created a Boilerplate project from the Divio Django project template: (Python 3.6, Django CMS, HTML 5)

Checking out the repository returns a couple of files, among which are a docker-compose and a dockerfile.

Docker-compose:

version: "2"

services:
  web:
    build: "."
    links:
      - "db:postgres"
    ports:
      - "8000:80"
    volumes:
      - ".:/app:rw"
      - "./data:/data:rw"
    command: python manage.py runserver 0.0.0.0:80
    env_file: .env-local

  db:
    image: postgres:9.6-alpine
    environment:
      POSTGRES_DB: "db"
    volumes:
      - ".:/app:rw"

So locally I try to run

docker-compose run web

to startup a local Django CMS instance. However, I run into the following error:

Starting ale_db_1 ... done
/app/addons/aldryn-django/aldryn_config.py:137: RuntimeWarning: no cache configured. Falling back to CACHE_URL=locmem://
  RuntimeWarning,
/app/addons/aldryn-django/aldryn_config.py:137: RuntimeWarning: no cache configured. Falling back to CACHE_URL=locmem://
  RuntimeWarning,
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f42a18c4950>
Traceback (most recent call last):
  File "/virtualenv/lib/python3.6/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/virtualenv/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
    autoreload.raise_last_exception()
  File "/virtualenv/lib/python3.6/site-packages/django/utils/autoreload.py", line 251, in raise_last_exception
    six.reraise(*_exception)
  File "/virtualenv/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/virtualenv/lib/python3.6/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/virtualenv/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/virtualenv/lib/python3.6/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/virtualenv/lib/python3.6/site-packages/django/apps/config.py", line 94, in create
    module = import_module(entry)
  File "/virtualenv/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'aldryn_apphooks_config'

Any ideas?

I can reproduce the error, when starting the container interactively:

mzh@ale $ docker run -it --rm divio/base:4.14-py3.6-slim-stretch /bin/bash
root@5d65902b34a4:/app# python
Python 3.6.8 (default, Mar 27 2019, 08:53:45) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import aldryn_apphooks_config
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'aldryn_apphooks_config'
Turd answered 8/4, 2019 at 18:24 Comment(0)
J
3

Use Dockerfile to install python packages.

Dockerfile content

FROM python:3

RUN pip install --upgrade pip && \
    pip install aldryn_apphooks_config
$: docker build -t "web:python" .
Sending build context to Docker daemon  3.584kB
Step 1/2 : FROM python:3
 ---> 59a8c21b72d4
Step 2/2 : RUN pip install --upgrade pip &&     pip install aldryn_apphooks_config
 ---> Running in e16a679af3d8
.
.
.
.
Successfully built aldryn-apphooks-config django-treebeard djangocms-admin-style
Installing collected packages: pytz, Django, django-appdata, django-treebeard, django-classy-tags, django-sekizai, djangocms-admin-style, django-formtools, django-cms, aldryn-apphooks-config
Successfully installed Django-2.1.8 aldryn-apphooks-config-0.5.2 django-appdata-0.2.1 django-classy-tags-0.8.0 django-cms-3.6.0 django-formtools-2.1 django-sekizai-0.10.0 django-treebeard-4.3 djangocms-admin-style-1.3.0 pytz-2018.9
Removing intermediate container e16a679af3d8
 ---> 78ecd015d983
Successfully built 78ecd015d983
Successfully tagged web:python

Start the container and verify the package installion.

$ docker run -it web:python /bin/bash
root@1d1df7416b8a:/# pip freeze
aldryn-apphooks-config==0.5.2
Django==2.1.8
django-appdata==0.2.1
django-classy-tags==0.8.0
django-cms==3.6.0
django-formtools==2.1
django-sekizai==0.10.0
django-treebeard==4.3
djangocms-admin-style==1.3.0
pytz==2018.9
Jovian answered 9/4, 2019 at 6:53 Comment(0)
Y
4

you can use a requirements.txt file. To write your module to import and call him in your dockerfile

$ more requirements.txt 
paramiko
lxml
pg8000

And in your dockerfile :

ADD             ./requirements.txt ./
RUN             python3 -m pip install -r requirements.txt
Yukoyukon answered 9/4, 2019 at 7:45 Comment(1)
This is not necessary in a Divio Cloud project - the Dockerfile already contains instructions to process the requirements file, requirements.in by default. See my answer.Schwejda
J
3

Use Dockerfile to install python packages.

Dockerfile content

FROM python:3

RUN pip install --upgrade pip && \
    pip install aldryn_apphooks_config
$: docker build -t "web:python" .
Sending build context to Docker daemon  3.584kB
Step 1/2 : FROM python:3
 ---> 59a8c21b72d4
Step 2/2 : RUN pip install --upgrade pip &&     pip install aldryn_apphooks_config
 ---> Running in e16a679af3d8
.
.
.
.
Successfully built aldryn-apphooks-config django-treebeard djangocms-admin-style
Installing collected packages: pytz, Django, django-appdata, django-treebeard, django-classy-tags, django-sekizai, djangocms-admin-style, django-formtools, django-cms, aldryn-apphooks-config
Successfully installed Django-2.1.8 aldryn-apphooks-config-0.5.2 django-appdata-0.2.1 django-classy-tags-0.8.0 django-cms-3.6.0 django-formtools-2.1 django-sekizai-0.10.0 django-treebeard-4.3 djangocms-admin-style-1.3.0 pytz-2018.9
Removing intermediate container e16a679af3d8
 ---> 78ecd015d983
Successfully built 78ecd015d983
Successfully tagged web:python

Start the container and verify the package installion.

$ docker run -it web:python /bin/bash
root@1d1df7416b8a:/# pip freeze
aldryn-apphooks-config==0.5.2
Django==2.1.8
django-appdata==0.2.1
django-classy-tags==0.8.0
django-cms==3.6.0
django-formtools==2.1
django-sekizai==0.10.0
django-treebeard==4.3
djangocms-admin-style==1.3.0
pytz==2018.9
Jovian answered 9/4, 2019 at 6:53 Comment(0)
S
0

If you are running a Divio Cloud project locally, then the best way to add Python modules is via the requirements.in file.

See How to install Python dependencies in a project .

The Divio-provided Dockerfile will process this when you build the project: docker-compose build web.

Schwejda answered 23/4, 2019 at 6:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.