skip to Main Content

I found a Django project and failed to get it running in Docker container in the following way:

  1. git clone https://github.com/hotdogee/django-blast.git

  2. $ cat requirements.txt in this files the below dependencies had to be updated:

    • kombu==3.0.30
    • psycopg2==2.8.6

I have the following Dockerfile:

FROM python:2
ENV PYTHONUNBUFFERED=1
RUN apt-get update && apt-get install -y postgresql-client
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

For docker-compose.yml I use:

version: "3"

services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres

  dbi5k:
    image: postgres
    volumes:
      - ./data/dbi5k:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=django_i5k
      - POSTGRES_USER=django
      - POSTGRES_PASSWORD=postgres


  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
    links:
      - db

I had to change:

$ vim i5k/settings_prod.py
DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'postgres',
    'USER': 'postgres',
    'PASSWORD': 'postgres',
    'HOST': 'db',
    'PORT': '5432',
    }
}

and

$ vim i5k/settings.py
    DATABASES = {
        'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'django_i5k',
        'USER': 'django',
        'PASSWORD': 'postgres',
        'HOST': 'dbi5k',
        'PORT': '5432',
        }
    }

Please below the logs after I ran

docker-compose build
docker-compose up
# I had to wait for 2 minutes and do CTRL+C in order to give the database to start
docker-compose up
Attaching to djangoblast_dbik_1, djangoblast_db_1, djangoblast_web_1
dbik_1  | 
db_1    | 
dbik_1  | PostgreSQL Database directory appears to contain a database; Skipping initialization
dbik_1  | 
db_1    | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1    | 
dbik_1  | 2021-05-19 10:45:54.221 UTC [1] LOG:  starting PostgreSQL 13.3 (Debian 13.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1    | 2021-05-19 10:45:55.264 UTC [1] LOG:  starting PostgreSQL 13.3 (Debian 13.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1    | 2021-05-19 10:45:55.264 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
dbik_1  | 2021-05-19 10:45:54.221 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1    | 2021-05-19 10:45:55.264 UTC [1] LOG:  listening on IPv6 address "::", port 5432
dbik_1  | 2021-05-19 10:45:54.221 UTC [1] LOG:  listening on IPv6 address "::", port 5432
dbik_1  | 2021-05-19 10:45:54.226 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
dbik_1  | 2021-05-19 10:45:54.231 UTC [26] LOG:  database system was shut down at 2021-05-19 10:45:07 UTC
db_1    | 2021-05-19 10:45:55.271 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
dbik_1  | 2021-05-19 10:45:54.237 UTC [1] LOG:  database system is ready to accept connections
db_1    | 2021-05-19 10:45:55.283 UTC [26] LOG:  database system was shut down at 2021-05-19 10:45:18 UTC
db_1    | 2021-05-19 10:45:55.292 UTC [1] LOG:  database system is ready to accept connections
web_1   | /usr/local/lib/python2.7/site-packages/social/apps/django_app/default/models.py:29: RemovedInDjango19Warning: Model class social.apps.django_app.default.models.UserSocialAuth doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
web_1   |   class UserSocialAuth(models.Model, DjangoUserMixin):
web_1   | 
web_1   | /usr/local/lib/python2.7/site-packages/social/apps/django_app/default/models.py:67: RemovedInDjango19Warning: Model class social.apps.django_app.default.models.Nonce doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
web_1   |   class Nonce(models.Model, DjangoNonceMixin):
web_1   | 
web_1   | /usr/local/lib/python2.7/site-packages/social/apps/django_app/default/models.py:78: RemovedInDjango19Warning: Model class social.apps.django_app.default.models.Association doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
web_1   |   class Association(models.Model, DjangoAssociationMixin):
web_1   | 
web_1   | /usr/local/lib/python2.7/site-packages/social/apps/django_app/default/models.py:91: RemovedInDjango19Warning: Model class social.apps.django_app.default.models.Code doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
web_1   |   class Code(models.Model, DjangoCodeMixin):
web_1   | 
web_1   | /code/suit/admin.py:5: RemovedInDjango19Warning: django.contrib.contenttypes.generic is deprecated and will be removed in Django 1.9. Its contents have been moved to the fields, forms, and admin submodules of django.contrib.contenttypes.
web_1   |   from django.contrib.contenttypes import generic
web_1   | 
web_1   | /usr/local/lib/python2.7/site-packages/social/apps/django_app/default/models.py:29: RemovedInDjango19Warning: Model class social.apps.django_app.default.models.UserSocialAuth doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
web_1   |   class UserSocialAuth(models.Model, DjangoUserMixin):
web_1   | 
web_1   | /usr/local/lib/python2.7/site-packages/social/apps/django_app/default/models.py:67: RemovedInDjango19Warning: Model class social.apps.django_app.default.models.Nonce doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
web_1   |   class Nonce(models.Model, DjangoNonceMixin):
web_1   | 
web_1   | /usr/local/lib/python2.7/site-packages/social/apps/django_app/default/models.py:78: RemovedInDjango19Warning: Model class social.apps.django_app.default.models.Association doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
web_1   |   class Association(models.Model, DjangoAssociationMixin):
web_1   | 
web_1   | /usr/local/lib/python2.7/site-packages/social/apps/django_app/default/models.py:91: RemovedInDjango19Warning: Model class social.apps.django_app.default.models.Code doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
web_1   |   class Code(models.Model, DjangoCodeMixin):
web_1   | 
web_1   | /code/suit/admin.py:5: RemovedInDjango19Warning: django.contrib.contenttypes.generic is deprecated and will be removed in Django 1.9. Its contents have been moved to the fields, forms, and admin submodules of django.contrib.contenttypes.
web_1   |   from django.contrib.contenttypes import generic
web_1   | 
web_1   | Performing system checks...
web_1   | 
web_1   | System check identified no issues (0 silenced).
web_1   | 
web_1   | You have unapplied migrations; your app may not work properly until they are applied.
web_1   | Run 'python manage.py migrate' to apply them.
web_1   | May 19, 2021 - 06:45:57
web_1   | Django version 1.8, using settings 'i5k.settings'
web_1   | Starting development server at http://0.0.0.0:8000/
web_1   | Quit the server with CONTROL-C.
web_1   | /code/rest_framework_swagger/urlparser.py:4: RemovedInDjango19Warning: django.utils.importlib will be removed in Django 1.9.
web_1   |   from django.utils.importlib import import_module
web_1   | 
web_1   | [19/May/2021 06:46:12]"GET / HTTP/1.1" 404 5610
web_1   | [19/May/2021 06:46:12]"GET /favicon.ico HTTP/1.1" 404 5643

enter image description here

Finally, I ran

$ docker-compose exec web python manage.py makemigrations
Migrations for 'default':
  0003_auto_20210519_1900.py:
    - Alter field email on code
$ docker-compose exec web python manage.py dbshell
Password for user django: 
psql (11.12 (Debian 11.12-0+deb10u1), server 13.3 (Debian 13.3-1.pgdg100+1))
WARNING: psql major version 11, server major version 13.
         Some psql features might not work.
Type "help" for help.

django_i5k=# create extension hstore
django_i5k-# q
$ docker-compose run web python manage.py migrate
Starting djangoblast_db_1 ... done
Operations to perform:
  Synchronize unmigrated apps: pipeline, postgres, staticfiles, admindocs, axes, messages, rest_framework_swagger, proxy, suit, rest_framework, filebrowser
  Apply all migrations: default, sessions, admin, app, sites, auth, captcha, contenttypes, blast, data, webapollo
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying data.0001_initial...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 221, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 147, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 115, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/local/lib/python2.7/site-packages/django/db/migrations/operations/models.py", line 59, in database_forwards
    schema_editor.create_model(model)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 282, in create_model
    self.execute(sql, params or None)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 107, in execute
    cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: type "hstore" does not exist
LINE 1: ..." timestamp with time zone NOT NULL, "attributes" hstore NOT...

What did I miss?

Thank you in advance

2

Answers


  1. Chosen as BEST ANSWER

    I used this solution

    version: "3"
    
    services:
      dbik:
        image: postgres
        volumes:
          - ./data/dbik:/var/lib/postgresql/data
          - ./scripts/install-extensions.sql:/docker-entrypoint-initdb.d/install-extensions.sql
    
        environment:
          - POSTGRES_DB=django_i5k
          - POSTGRES_USER=django
          - POSTGRES_PASSWORD=postgres
        ports:
          - 5432:5432
    
    
      web:
        build: .
        command: python manage.py runserver 0.0.0.0:8000
        volumes:
          - .:/code
        ports:
          - "8000:8000"
        depends_on:
          - dbik
        links:
          - dbik
    
    
    $ cat scripts/install-extensions.sql 
    CREATE EXTENSION hstore;
    

    After docker-compose up --build I used docker-compose run web python manage.py migrate


  2. From https://github.com/hotdogee/django-blast.git manage.py, selected settings module is i5k.settings

    #manage.py
    ...
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "i5k.settings")
    ...
    

    So I think setup db in settings_prod.py does not really make sense as this module won’t be loaded in your example.

    You can drop the following container setup in your docker-compose:

    #docker-compose.yml
    ...
      db:
        image: postgres
        volumes:
          - ./data/db:/var/lib/postgresql/data
        environment:
          - POSTGRES_DB=postgres
          - POSTGRES_USER=postgres
          - POSTGRES_PASSWORD=postgres
    ...
    

    Then here is how you can add port mapping:

    # docker-compose.yml
    
      dbi5k:
        image: postgres
        volumes:
          - ./data/dbi5k:/var/lib/postgresql/data
        environment:
          - POSTGRES_DB=django_i5k
          - POSTGRES_USER=django
          - POSTGRES_PASSWORD=postgres
        ports:
          - 5432:5432
    
    

    To change django’s version:

    #requirements.txt
    Django==3.2
    ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search