Skip to content

SQLite dev Docker setup still uses PS_DATABASE_URI from prod.env and tries to connect to missing db host #158

Description

@armandoschianodicola

Description:

I am following the Docker development documentation:

https://wger.readthedocs.io/en/latest/development/docker.html

I am using the SQLite dev setup from dev/docker-compose.yml, with WGER_CODEPATH pointing to my local wger backend checkout.

When I start the dev environment and run wger bootstrap inside the web container, Django tries to use PostgreSQL instead of SQLite and fails because there is no db service in the SQLite dev compose setup.

Steps to reproduce:

  1. Clone wger-docker.

  2. Clone wger next to it.

  3. Configure dev/.env, for example:

    WGER_CODEPATH=C:\my_path\wger

  4. Start the SQLite dev compose setup:

    docker compose -f .\dev\docker-compose.yml up --watch

  5. Enter the web container:

    docker compose -f .\dev\docker-compose.yml exec web /bin/bash

  6. Confirm the database env:

    env | grep -E 'DJANGO_DB|DATABASE|POSTGRES|PS_'

    The container has:

    DJANGO_DB_DATABASE=/home/wger/src/database.sqlite
    DJANGO_DB_ENGINE=django.db.backends.sqlite3
    PS_DATABASE_URI=postgres://wger:wger@db:5432/wger

  7. Run:

    wger bootstrap

Expected behavior:

The SQLite dev setup should use:

DJANGO_DB_ENGINE=django.db.backends.sqlite3
DJANGO_DB_DATABASE=/home/wger/src/database.sqlite

and wger bootstrap should create/use /home/wger/src/database.sqlite.

Actual behavior:

wger bootstrap uses PostgreSQL and fails while resolving the db hostname:

django.db.utils.OperationalError: failed to resolve host 'db': [Errno -2] Name or service not known

Relevant traceback excerpt:

*** Using settings from env: settings.main
*** Database does not exist, creating one now
*** Using settings from env: settings.main
...
File "/home/wger/src/settings/main.py", line 65, in
DATABASES = {'default': env.db_url('PS_DATABASE_URI')}
...
django.db.utils.OperationalError: failed to resolve host 'db': [Errno -2] Name or service not known

Cause:

dev/docker-compose.yml loads these env files:

  • ../config/prod.env
  • ../config/dev.env
  • ../config/dev-sqlite.env

config/prod.env sets:

PS_DATABASE_URI=postgres://wger:wger@db:5432/wger

config/dev-sqlite.env sets:

DJANGO_DB_ENGINE=django.db.backends.sqlite3
DJANGO_DB_DATABASE=/home/wger/src/database.sqlite

However, in settings/main.py, PS_DATABASE_URI is checked before DJANGO_DB_ENGINE:

if os.environ.get('PS_DATABASE_URI'):
    DATABASES = {'default': env.db_url('PS_DATABASE_URI')}
elif os.environ.get('DJANGO_DB_ENGINE'):
    DATABASES = {
        'default': {
            'ENGINE': env.str('DJANGO_DB_ENGINE'),
            'NAME': env.str('DJANGO_DB_DATABASE'),
            'USER': env.str('DJANGO_DB_USER'),
            'PASSWORD': env.str('DJANGO_DB_PASSWORD'),
            'HOST': env.str('DJANGO_DB_HOST'),
            'PORT': env.int('DJANGO_DB_PORT'),
        }
    }
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': env.str('DJANGO_DB_DATABASE', '/home/wger/db/database.sqlite'),
        }
    }

So the PostgreSQL URL inherited from prod.env wins over the SQLite-specific env file.

Furthermore, simply unsetting PS_DATABASE_URI may expose a second SQLite config problem unless those optional fields of the elif condition get defaults or are omitted.

Environment:

  • OS: Windows
  • Shell: PowerShell
  • Docker Compose setup: dev/docker-compose.yml
  • wger Docker dev mode: SQLite

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions