Search code examples
dockervisual-studio-codedocker-composevscode-debugger

Docker-compose config, vscode debugger does not start


I am trying to setup vscode for debugging my python fastapi app in a docker environment. At the beginning it was easy because I used vscode docker compose tool, modified the generated Dockerfile for my needs, launched the debugger extension and that was it.

Then I add a mariadb service to my docker-compose.yml, started another debugging session without configuring anything more and that was it, my debug container always crashed because of a connection failure database error, of course the reason is that I didn't update anything in launch.json and tasks.json properly and the db container wasn't reachable probably because it was missing in the docker-compose.debug.yml file.

I changed the configuration and also my debug docker-compose file but for some reason the vscode client doesn't connect to the container and the debug session is not launched automatically like expected.

So here's what I did so far based on the files that vscode generated:

launch.json

{
    "name": "Docker: Python - Belbackend",
    "type": "docker",
    "request": "launch",
    "preLaunchTask": "docker-compose-start",
        "python": {
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}/app",
                    "remoteRoot": "/code/app"
                }
            ],
        "port": 8000,
        "host": "localhost"
    },
},

tasks.json

{
    "label": "docker-compose-start",
    "type": "shell",
    "command": "docker-compose -f docker-compose.debug.yml up --build -d",
    "isBackground": true,
    "problemMatcher": [
        {
        "pattern": [
        {
            "regexp": ".",
            "file": 1,
                "location": 2,
            "message": 3,
        }
        ],
        "background": {
            "activeOnStart": true,
        "beginsPattern": "^(Building py-service)$",
        "endsPattern": "^(Creating|Recreating|Starting) (py-container) ... (done)$",
        }
        },
    ],
},

docker-compose.debug.yml

version: '3.4'

services:
  belbackend:
    container_name: belbe
    image: belbackend
    environment:
      - DATABASE_URL=mysql+pymysql://bel:1234@beldb/local
    build:
      context: .
      dockerfile: ./Dockerfile
    command: ["sh", "-c", "python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 -m uvicorn app.main:app --host 0.0.0.0 --port 8000"]
    ports:
      - 8000:8000
      - 5678:5678
    depends_on:
      - db
    networks:
      - belnet

  db:
    image: mariadb:10.10.2-jammy
    restart: always
    container_name: beldb
    environment:
      MYSQL_DATABASE: 'local'
      MYSQL_USER: 'bel'
      MYSQL_PASSWORD: '1234'
      MYSQL_ROOT_PASSWORD: '1234'
    ports:
      - '3306:3306'
    expose:
      - '3306'
    networks:
      - belnet
networks:
  belnet:
    driver: bridge

And for info the docker:

Dockerfile

# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.8-slim

EXPOSE 8000

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

WORKDIR /code

COPY ./requirements.txt /code/requirements.txt
COPY ./alembic.ini /code/alembic.ini

RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

COPY ./alembic /code/alembic
COPY ./app /code/app

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
RUN pip install debugpy -t /tmp \
    && adduser -u 5678 --disabled-password --gecos "" bebel \
    && chown -R bebel /code
USER bebel

# During debugging, this entry point will be overridden.
CMD ["uvicorn", "app.main:app", "--proxy-headers", "--reload", "--host", "0.0.0.0", "--port", "8000"]

I was expecting a debug session to automatically start after clicking on "play" but no interface is showing and when testing with postman, the app does not respond which is actually normal because of the --wait-for-client option when executing debugpy on belbe container launch (see compose file) like below:

python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 -m uvicorn app.main:app --host 0.0.0.0 --port 8000

Therefore I'm still trying to figure out and I'm thinking something's maybe wrong with launch.json. I'm thinking about choosing an attach request type to bind the debugger to my running dev container but I'm not sure if it's possible and if it is, how can we set the link to the container.

Any idea/experience about that?


Solution

  • I could make it finally work with the help of thisanswer, attach is more efficient. So it seems launch mode was not the good option to handle this.