VSCode debugpy with dockerized django app

212 Views Asked by At

I'm trying to use debugpy with a dockerized django app - it connects momentarily then disconnects - I'm not sure why

docker-compose.backend.yml: (the uplifty service below is the django server I want to attach to from vscode)

version: '3'
services:
  db:
    image: postgres
    platform: linux/amd64
    container_name: uplifty-db
    environment:
      - POSTGRES_HOST_AUTH_METHOD=trust
    ports:
      - "5432:5432"
  uplifty:
    build:
      context: .
      dockerfile: ./docker/Docker.server/Dockerfile
    image: uplifty
    env_file: .env
    container_name: uplifty
    volumes:
      - .:/code
    ports:
      - "8000:8000"
      - "5678:5678"
    depends_on:
      - db
    links:
      - db:postgres

Dockerfile:

FROM python:3.11.3

ENV PYTHONUNBUFFERED 1

RUN apt-get update && apt-get install -y \
    sudo && apt-get install -y \
    git \
    nginx \
    postgresql-client \
    supervisor

RUN mkdir /code
ADD docker /code

RUN rm /etc/supervisor/supervisord.conf && \
    ln -s /code/docker/Docker.server/supervisord.conf /etc/supervisor/supervisord.conf && \
    rm /etc/nginx/nginx.conf && \
    ln -s /code/docker/Docker.server/nginx.conf /etc/nginx/nginx.conf && \
    mkdir -p /var/log/supervisor && \
    mkdir -p /var/log/gunicorn && \
    mkdir -p /code/logs/supervisord

RUN groupadd -r app -g 1000 && \
    useradd -u 1000 -r -g app -d /code -s /bin/bash -c "Docker image user" app
RUN chown -R app:app /code && \
    chown -R app:app /var/run && \
    chown -R app:app /var/log/gunicorn

RUN pip install psycopg2
RUN pip install poetry
RUN pip install gunicorn
ADD server/pyproject.toml server/poetry.lock /code/server/
ADD .env /code
WORKDIR /code/server

# Install python requirements
RUN poetry export --dev -f requirements.txt --without-hashes --output /code/server/requirements.txt && pip install -r requirements.txt

EXPOSE 8000
EXPOSE 5678

ENTRYPOINT ["/bin/bash", "/code/docker/Docker.server/entrypoint.sh"]
CMD ["development"]

manage.py:

import os
import sys


def main():
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "uplifty.settings")

    print(os.environ)

    if os.environ.get("DEBUG_ATTACH") == "true":
        import debugpy
        print("Waiting for debugger to attach...")
        debugpy.listen(("0.0.0.0", 5678))
        print("Debugger listening...")
        debugpy.wait_for_client()
        print("Debugger attached!")

    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError("Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?") from exc

    execute_from_command_line(sys.argv)

if __name__ == "__main__":
    main()

./vscode/launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug Django",
      "type": "python",
      "request": "attach",
      "connect": {
        "port": 5678,
        "host": "localhost"
      },
      "pathMappings": [
        {
          "localRoot": "${workspaceFolder}/server",
          "remoteRoot": "/code/server"
        }
      ],
      "justMyCode": false
    }
  ]
}

start_gunicorn.sh

#!/bin/bash

NAME="app-wsgi"                         # Name of the application
DJANGODIR=/code/server                  # Django project directory
WSGI_MODULE=uplifty.wsgi:application    # Django WSGI module
DJANGO_SETTINGS_MODULE=uplifty.settings # Django settings
SOCKFILE=/tmp/gunicorn.sock             # we will communicate using this unix socket
LOGFILE=/var/log/gunicorn/access.log    # WSGI log here
USER=app                                # the user to run as
GROUP=app                               # the group to run as
NUM_WORKERS=2                           # how many worker processes should spawn

echo "Starting $NAME as `whoami`"

EXTRAOPTS=''
if [ "${DJANGO_DEBUG}" = "true" ]; then
    EXTRAOPTS='--reload'
fi

# Start gunicorn
exec gunicorn \
    --name $NAME \
    --workers $NUM_WORKERS \
    --user $USER \
    --group $GROUP \
    --log-level debug \
    --bind unix:$SOCKFILE \
    --access-logfile $LOGFILE \
    $EXTRAOPTS \
    $WSGI_MODULE

Any suggestions ? I can post more files if needed but these are most relevant ones

0

There are 0 best solutions below