debug django docker dont work postgresql os.getenv don’t work with VSCode

  debugging, django, docker, visual-studio-code

Hi i have worked nginx and app local server dockers. And all work fine but when i use VSCode debugger its dont load my .env file.
This is my files:

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.getenv('NAME'),
        'USER': os.getenv('USER'),
        'PASSWORD': os.getenv('PASSWORD'),
        'HOST': os.getenv('HOST'),
        'PORT': os.getenv('PORT'),
    }
}

and my debug files:
version: "3.9"

services:
    docker_app:
        image: docker_app
        build:
            context: .
            dockerfile: ./Dockerfile
        command:
            [
                "sh",
                "-c",
                "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 manage.py runserver 0.0.0.0:8000 --nothreading --noreload",
            ]
        ports:
            - 8000:8000
            - 5678:5678

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "echo Hello"
        },
        {
            "type": "docker-build",
            "label": "docker-build",
            "platform": "python",
            "dockerBuild": {
                "tag": "docker:latest",
                "dockerfile": "${workspaceFolder}/Dockerfile",
                "context": "${workspaceFolder}",
                "pull": true
            }
        },
        {
            "type": "docker-run",
            "label": "docker-run: debug",
            "dependsOn": ["docker-build"],
            "python": {
                "args": ["runserver", "0.0.0.0:8000", "--nothreading", "--noreload"],
                "file": "manage.py"
            }
        }
    ]
}

launch.json

{
    "configurations": [
        {
            "name": "Docker: Python - Django",
            "type": "docker",
            "request": "launch",
            "preLaunchTask": "docker-run: debug",
            "python": {
                "pathMappings": [
                    {
                        "localRoot": "${workspaceFolder}/app",
                        "remoteRoot": "/app"
                    }
                ],
                "projectType": "django"
            }
        }
    ]
}

I don’t have idea why debugger dont load .env i add to my docker-compose.debug.yml

env_file:
    - .env

and this don’t works for me ;/
I know its this beacuse work when i add my conf to DATABASES in settings but i want this values hidden

my Dockerfile:

FROM python:3.9-alpine3.13

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1

COPY ./requirements.txt /requirements.txt
COPY ./app /app
COPY ./scripts /scripts

WORKDIR /app
EXPOSE 8000


RUN python -m venv /py && 
    /py/bin/pip install --upgrade pip && 
    apk add --update --no-cache postgresql-client && 
    apk add --update --no-cache --virtual .tmp 
    build-base postgresql-dev musl-dev linux-headers && 
    /py/bin/pip install -r /requirements.txt && 
    apk del .tmp && 
    adduser --disabled-password --no-create-home --gecos "" appuser && 
    chown -R appuser /app && 
    mkdir -p /vol/web/static && 
    mkdir -p /vol/web/media && 
    chown -R appuser:appuser /vol && 
    chmod -R 755 /vol && 
    chmod -R +x /scripts

ENV PATH="/scripts:/py/bin:$PATH"

USER appuser

CMD ["run.sh"]

Source: Docker Questions

LEAVE A COMMENT