skip to Main Content

I am pretty new to Docker and Django. I am trying to set up a Django project for a REST-ful API running in a Docker container. I am trying to import the relavent python packages from a RUN command in the dockerfile, however, not all the packages are successfully installing.

Here are the files I’m using and the error I am getting.

Dockerfile:

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt .
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . .

docker-compose.yml:

version: '3'

services:
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: password
  web:
    build: .
    # command: bash -c "pip install -r requirements.txt && python manage.py runserver 0.0.0.0:8000"
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

requirements.txt

djangorestframework
django-filter
markdown
Django
psycopg2

When I execute docker-compose up I get this output

Starting apiTest_db_1 ... done
Recreating apiTest_web_1 ... done
Attaching to apiTest_db_1, apiTest_web_1
db_1   | 
db_1   | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1   | 
db_1   | 2020-04-17 21:35:57.022 UTC [1] LOG:  starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1   | 2020-04-17 21:35:57.023 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2020-04-17 21:35:57.023 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2020-04-17 21:35:57.028 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2020-04-17 21:35:57.075 UTC [27] LOG:  database system was shut down at 2020-04-17 21:34:34 UTC
db_1   | 2020-04-17 21:35:57.100 UTC [1] LOG:  database system is ready to accept connections
web_1  | Watching for file changes with StatReloader
web_1  | Exception in thread django-main-thread:
web_1  | Traceback (most recent call last):
web_1  |   File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner
web_1  |     self.run()
web_1  |   File "/usr/local/lib/python3.8/threading.py", line 870, in run
web_1  |     self._target(*self._args, **self._kwargs)
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 53, in wrapper
web_1  |     fn(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
web_1  |     autoreload.raise_last_exception()
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
web_1  |     raise _exception[1]
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 357, in execute
web_1  |     autoreload.check_errors(django.setup)()
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 53, in wrapper
web_1  |     fn(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
web_1  |     apps.populate(settings.INSTALLED_APPS)
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/apps/registry.py", line 91, in populate
web_1  |     app_config = AppConfig.create(entry)
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/apps/config.py", line 90, in create
web_1  |     module = import_module(entry)
web_1  |   File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
web_1  |     return _bootstrap._gcd_import(name[level:], package, level)
web_1  |   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
web_1  |   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
web_1  |   File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
web_1  | ModuleNotFoundError: No module named 'rest_framework'

Which indicates that djangorestframework has not been installed by pip.
Furthermore, when I switch the comented line in the docker-compose.yml file for the line below it (so that section becomes)

command: bash -c "pip install -r requirements.txt && python manage.py runserver 0.0.0.0:8000"
# command: python manage.py runserver 0.0.0.0:8000

Then when I run docker-compose up I get the following output.

Creating network "apiTest_default" with the default driver
Creating apiTest_db_1 ... done
Creating apiTest_web_1 ... done
Attaching to apiTest_db_1, apiTest_web_1
db_1   | The files belonging to this database system will be owned by user "postgres".
db_1   | This user must also own the server process.
db_1   | 
db_1   | The database cluster will be initialized with locale "en_US.utf8".
db_1   | The default database encoding has accordingly been set to "UTF8".
db_1   | The default text search configuration will be set to "english".
db_1   | 
db_1   | Data page checksums are disabled.
db_1   | 
db_1   | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1   | creating subdirectories ... ok
db_1   | selecting dynamic shared memory implementation ... posix
db_1   | selecting default max_connections ... 100
db_1   | selecting default shared_buffers ... 128MB
db_1   | selecting default time zone ... Etc/UTC
db_1   | creating configuration files ... ok
db_1   | running bootstrap script ... ok
db_1   | performing post-bootstrap initialization ... ok
web_1  | Collecting djangorestframework
db_1   | syncing data to disk ... initdb: warning: enabling "trust" authentication for local connections
db_1   | You can change this by editing pg_hba.conf or using the option -A, or
db_1   | --auth-local and --auth-host, the next time you run initdb.
db_1   | ok
db_1   | 
db_1   | 
db_1   | Success. You can now start the database server using:
db_1   | 
db_1   |     pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1   | 
db_1   | waiting for server to start....2020-04-17 22:47:22.783 UTC [46] LOG:  starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1   | 2020-04-17 22:47:22.789 UTC [46] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
web_1  |   Downloading djangorestframework-3.11.0-py3-none-any.whl (911 kB)
db_1   | 2020-04-17 22:47:22.823 UTC [47] LOG:  database system was shut down at 2020-04-17 22:47:22 UTC
db_1   | 2020-04-17 22:47:22.841 UTC [46] LOG:  database system is ready to accept connections
db_1   |  done
db_1   | server started
db_1   | 
db_1   | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1   | 
db_1   | 2020-04-17 22:47:22.885 UTC [46] LOG:  received fast shutdown request
db_1   | waiting for server to shut down....2020-04-17 22:47:22.889 UTC [46] LOG:  aborting any active transactions
db_1   | 2020-04-17 22:47:22.908 UTC [46] LOG:  background worker "logical replication launcher" (PID 53) exited with exit code 1
db_1   | 2020-04-17 22:47:22.920 UTC [48] LOG:  shutting down
db_1   | 2020-04-17 22:47:22.974 UTC [46] LOG:  database system is shut down
db_1   |  done
db_1   | server stopped
db_1   | 
db_1   | PostgreSQL init process complete; ready for start up.
db_1   | 
db_1   | 2020-04-17 22:47:23.021 UTC [1] LOG:  starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1   | 2020-04-17 22:47:23.022 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2020-04-17 22:47:23.023 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2020-04-17 22:47:23.036 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2020-04-17 22:47:23.063 UTC [55] LOG:  database system was shut down at 2020-04-17 22:47:22 UTC
db_1   | 2020-04-17 22:47:23.073 UTC [1] LOG:  database system is ready to accept connections
web_1  | Collecting django-filter
web_1  |   Downloading django_filter-2.2.0-py3-none-any.whl (69 kB)
web_1  | Collecting markdown
web_1  |   Downloading Markdown-3.2.1-py2.py3-none-any.whl (88 kB)
web_1  | Requirement already satisfied: Django in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 4)) (3.0.5)
web_1  | Requirement already satisfied: psycopg2 in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 5)) (2.8.5)
web_1  | Requirement already satisfied: setuptools>=36 in /usr/local/lib/python3.8/site-packages (from markdown->-r requirements.txt (line 3)) (46.1.3)
web_1  | Requirement already satisfied: pytz in /usr/local/lib/python3.8/site-packages (from Django->-r requirements.txt (line 4)) (2019.3)
web_1  | Requirement already satisfied: sqlparse>=0.2.2 in /usr/local/lib/python3.8/site-packages (from Django->-r requirements.txt (line 4)) (0.3.1)
web_1  | Requirement already satisfied: asgiref~=3.2 in /usr/local/lib/python3.8/site-packages (from Django->-r requirements.txt (line 4)) (3.2.7)
web_1  | Installing collected packages: djangorestframework, django-filter, markdown
web_1  | Successfully installed django-filter-2.2.0 djangorestframework-3.11.0 markdown-3.2.1
web_1  | Watching for file changes with StatReloader
web_1  | Performing system checks...
web_1  | 
web_1  | System check identified no issues (0 silenced).
web_1  | 
web_1  | You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
web_1  | Run 'python manage.py migrate' to apply them.
web_1  | April 17, 2020 - 22:47:25
web_1  | Django version 3.0.5, using settings 'apiTesting.settings'
web_1  | Starting development server at http://0.0.0.0:8000/
web_1  | Quit the server with CONTROL-C.

Which shows that some packages such as Django have been successfully installed by the Dockerfile but some like djangorestframework, django-filter and markdown have not.
Why is this and what can I do in my Dockerfile to make them correctly install?

2

Answers


  1. Chosen as BEST ANSWER

    Both the main problem and the problem mentioned in the comments of itamar-turner-trauring's answer were solved by instead of running docker-compose up running

    docker-compose up --build
    

    Not 100% sure why this fixed it but I'd guess the compose file was loaing up the container from an old image which didn't include the new python packages. So forcing it to rebuild made it include the new python packages.


  2. You are doing two things that potentially conflict:

    1. Inside the image, as part of the build you copy everything in to /code.
    2. In the compose file you mount current working directory into /code.

    I am not sure that’s the problem, but I suggest removing the volumes bit from the compose.yml and see if that help.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search