Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django project debug in docker container doesn't start #2313

Closed
jmejia32 opened this issue Sep 14, 2020 · 16 comments · Fixed by #2331
Closed

Django project debug in docker container doesn't start #2313

jmejia32 opened this issue Sep 14, 2020 · 16 comments · Fixed by #2331

Comments

@jmejia32
Copy link

Redirected from microsoft/vscode#106561 (comment)

In vscode, when I start the debug in my local computed (no docker), debug starts completely but when I start debug with docker, the container builds and starts while vscode ui waiting for connection and never "starts" debugging process.

@bwateratmsft
Copy link
Collaborator

bwateratmsft commented Sep 14, 2020

@jmejia32 Can you share the project you are encountering this issue in? Or at least the Docker and docker-compose files, tasks.json, and launch.json?

Also, can you share the logs from the following places:

  • Output
    • Docker
    • Python
  • Terminal
    • docker-build task
    • docker-run task
    • Python Debug Console
  • Debug Console

Debugging in Docker is done differently; the app is run by running Debugpy in the container, which is supposed to connect back to the host. It sounds like Debugpy may not be able to connect back to the host and so VSCode sits there waiting for the connection. If this is the case the "Python Debug Console" output probably has the most important info.

EDIT: Ah, I see your launch and tasks in the bug in the VSCode repo, as well as the docker-run task output. Can you share the logs from Terminal > Python Debug Console and Debug Console? The tasks, launch, and docker-run output look fine to me.

@jmejia32
Copy link
Author

@bwateratmsft ok, here we go:

  • Output
    • Docker: Nothing
    • Python:
Python interpreter path: ~/.local/pyenvs/django_env/bin/python
Starting Pylance language server.
> ~/.local/pyenvs/django_env/bin/python ~/.vscode/extensions/ms-python.python-2020.8.108011/pythonFiles/pyvsc-run-isolated.py -c "import notebook"
> ~/.local/pyenvs/django_env/bin/python ~/.vscode/extensions/ms-python.python-2020.8.108011/pythonFiles/pyvsc-run-isolated.py -c "import jupyter"
> conda --version
> pyenv root
> pyenv root
> pyenv root
> pyenv root
> pyenv root
> pyenv root
> pyenv root
> pyenv root
> pyenv root
  • Terminal
    • docker-build task
    {
        "type": "docker-build",
        "label": "docker-build",
        "platform": "python",
        "dockerBuild": {
          "tag": "talentumplus:latest",
          "dockerfile": "${workspaceFolder}/Dockerfile",
          "context": "${workspaceFolder}",
          "pull": true
        }
      },
    • docker-run task
    {
        "type": "docker-run",
        "label": "docker-run: debug",
        "dependsOn": [
          "docker-build"
        ],
        "dockerRun": {
          "volumes": [
            {
              "containerPath": "/app", "localPath": "${workspaceFolder}"
            }
          ],
          "ports": [
            {
              "containerPort": 8000,
              "hostPort": 8000
            }
          ]
        },
        "python": {
          "args": [ "runserver", "0.0.0.0:8000", "--noreload" ],
          "file": "manage.py"
        }
      }
    • Python Debug Console: ¿?
  • Debug Console: Nothing

docker-compose.yml

version: "3"
services:
  django:
    container_name: django
    build:
      context: .
    user: deploy
    restart: always
    tty: true
    volumes:
      - .:/home/deploy/web
    environment:
      - LANG=es_CO.UTF-8
      - DJANGO_ENV=prod
    networks:
      default:
        ipv4_address: 172.19.0.2
networks:
  default:
    driver: bridge
    ipam:
      config:
        - subnet: "172.19.0.0/16"

Dockerfile

FROM bitnami/python:3.7-debian-9-prod

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

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED 1

# MS ODBC
RUN install_packages curl gnupg2 apt-transport-https ca-certificates

RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - && \
    curl https://packages.microsoft.com/config/debian/9/prod.list > /etc/apt/sources.list.d/mssql-release.list && apt-get update
RUN ACCEPT_EULA=Y install_packages msodbcsql17 unixodbc-dev

# for uwsgi
RUN install_packages build-essential
# for requirements.txt
RUN install_packages libpq-dev default-jdk locales
RUN pip install --no-cache-dir --upgrade pip

RUN sed -i -e 's/# es_CO.UTF-8 UTF-8/es_CO.UTF-8 UTF-8/' /etc/locale.gen && locale-gen

# For google-oauth api
RUN rm -r /opt/bitnami/python/lib/python3.7/site-packages/setuptools*
RUN pip install --no-cache-dir setuptools

COPY requirements.txt .
RUN pip install --no-cache-dir uwsgi && pip install --no-cache-dir -r requirements.txt

# for pyjasperreport
COPY core-2.2.jar /opt/bitnami/python/lib/python3.7/site-packages/pyreportjasper/jasperstarter/jdbc

WORKDIR /app
RUN useradd -ms /bin/bash -u 1000 appuser && chown -R appuser /app
USER appuser

COPY entrypoint.sh .
COPY server.yaml .
CMD [ "./entrypoint.sh" ]

FYI, the menu entry Terminal > Python Debug Console doesn't appear
image

@jmejia32
Copy link
Author

@bwateratmsft Terminal output

Terminal will be reused by tasks, press any key to close it.

> Executing task: docker-run: debug <

> docker run -dt -P --name "talentumplus-dev" --label "com.microsoft.created-by=visual-studio-code" -v "/home/javiermejia/Documents/GitHub/talentum-plus:/app" -v "/home/javiermejia/.vscode/extensions/ms-python.python-2020.8.108011/pythonFiles/lib/python/debugpy:/debugpy:ro" -p "8000:8000" --entrypoint "python" "talentumplus:latest" <

f2bcf35f5d164dd69729aa73106872327ad9c15e0a74541d456b9de2261490bf

Terminal will be reused by tasks, press any key to close it.

@bwateratmsft
Copy link
Collaborator

bwateratmsft commented Sep 14, 2020

What version of the Python extension do you have? 2020.8.108011 per the original issue, that's the latest which is good

For Terminal > Python Debug Console, I mean this:
image
Sorry for being unclear

@jmejia32
Copy link
Author

jmejia32 commented Sep 14, 2020

@bwateratmsft No, I'm sorry for being a little dumb hahaha.

And the entry keeps hidden for me:

image

@bwateratmsft
Copy link
Collaborator

Hm, that's not good. Our debug launch config in launch.json resolves down to a Python configuration, so basically once the Docker container is started, we hand control over to the Python extension--they use docker exec to actually start debugpy. I'll investigate to see if I can reproduce this issue. Can you also share the output from docker inspect talentumplus-dev when the container is running but seemingly doing nothing?

@jmejia32
Copy link
Author

Yep, there it is:
docker inspect talentumplus-dev

[
    {
        "Id": "02e358841c9b79e0e674772f9344ab924c8ac50eef38f45ad14172737fe00f8e",
        "Created": "2020-09-14T16:27:18.588783348Z",
        "Path": "python",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 48499,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-09-14T16:27:19.157994199Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:8a6b47beb8243369f52ac50ff46ad53f67b1f713751c42bcce279dfd39e79c13",
        "ResolvConfPath": "/var/lib/docker/containers/02e358841c9b79e0e674772f9344ab924c8ac50eef38f45ad14172737fe00f8e/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/02e358841c9b79e0e674772f9344ab924c8ac50eef38f45ad14172737fe00f8e/hostname",
        "HostsPath": "/var/lib/docker/containers/02e358841c9b79e0e674772f9344ab924c8ac50eef38f45ad14172737fe00f8e/hosts",
        "LogPath": "/var/lib/docker/containers/02e358841c9b79e0e674772f9344ab924c8ac50eef38f45ad14172737fe00f8e/02e358841c9b79e0e674772f9344ab924c8ac50eef38f45ad14172737fe00f8e-json.log",
        "Name": "/talentumplus-dev",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/home/javiermejia/Documents/GitHub/talentum-plus:/app",
                "/home/javiermejia/.vscode/extensions/ms-python.python-2020.8.108011/pythonFiles/lib/python/debugpy:/debugpy:ro"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "8000/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "8000"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Capabilities": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": true,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/07063fc18b419e50fb2314ba2f1a31dfa69f526c3b683d9f3c0f187b53008888-init/diff:/var/lib/docker/overlay2/1a7fba0c17212da0653a4fbd9ca6a47a3f5f5c584e032653bdebe16b4f49ce92/diff:/var/lib/docker/overlay2/1cb05892f096bc1e2e6a603b9d219fac8e804c8a2c08552ba2c7254c25356943/diff:/var/lib/docker/overlay2/1eb4f2fd56a418058dc680134425fe807f6e1c794bf18f0ac0536436ddde00f4/diff:/var/lib/docker/overlay2/5c36e416acbac384a307e5e0ee11b6254e8cb9364bea1aba10bd235f1c3aa8f9/diff:/var/lib/docker/overlay2/7f212b2ebde4f9789df5d2722565b9b870021677b06997afd12c9a327b22209c/diff:/var/lib/docker/overlay2/8171f933fc0a307e6dbbc475019da984ca361640dc2b5ffab31a6cf38f93acf3/diff:/var/lib/docker/overlay2/d1f538ae9e1ff058c357335045b2bb98d92e584ed3eb17f2e52c1311d338b684/diff:/var/lib/docker/overlay2/5d9052a3e04aa1837543ec84a811ddeb2ebc8ddea8c3e9576d3e138bb60fa879/diff:/var/lib/docker/overlay2/2eae8458da83c8bd30e39c8789bf85cf9a05f989b34aa84c527b6c366102cade/diff:/var/lib/docker/overlay2/d460f111011b098f420146a8e1989bb25b56c9aaabf143f9379cc1ece6acfcd4/diff:/var/lib/docker/overlay2/dcc95a055553c7d0185e4dccd30c6f8edd473f993d8da956f027574509dec04d/diff:/var/lib/docker/overlay2/72a5f4ad5cf8e689052b99b944973a6fe8d6be683574b79842e2403977118886/diff:/var/lib/docker/overlay2/86670358c526f9a93edbbdee0e86e7ea6cbb995bc9597fb8dfceb721f88c0cec/diff:/var/lib/docker/overlay2/31ed2e8cada3897e093b811a9641477f2c8092e99afa2df2c46f7895ff545005/diff:/var/lib/docker/overlay2/2a4a071cd77bee12eb65da4831503f14513922191e6a014f4dff5875c971543a/diff:/var/lib/docker/overlay2/dd0479dcbd744ec3d048b7a0c7ab06dbd0378f0f965549fdf9183c228f93ab89/diff:/var/lib/docker/overlay2/877d21ee21b25149d1a8815bbe3da4eb967e1bb0a2a5c518a9983a376b8bd082/diff:/var/lib/docker/overlay2/28feb46e24654cb1b19ee6383602b65ac41b6e967030766231a184df33f74b32/diff:/var/lib/docker/overlay2/ad4473fd3ab65dceffc15b3b72d47c9a1e29148597af528d5bf70d6bb40204fb/diff:/var/lib/docker/overlay2/abe8796d95c4ed45da3103d42f8c5161dee7478788a05bf636ccc01df19ba11a/diff:/var/lib/docker/overlay2/cafa8e202d9e4e901d2921f9dcc9774e9ca0d1002402f5b755cefbc2caa3eb6f/diff:/var/lib/docker/overlay2/2ec417ba4ce15af72b9600f34a7e5f34cc6608a51d22f52caebcd202c2ef33d4/diff:/var/lib/docker/overlay2/6d9e3299fe3a282a5c94bb23e1cc77f8d81bc8849ff03247deb08927b21efc0d/diff:/var/lib/docker/overlay2/1e1d9111e5b524ec69777297946556142daa1e4e852c6d5e18dc67cf1bf2826d/diff:/var/lib/docker/overlay2/6051c93ba5757b1ba654031e43fdb851e02dccba7c7b57ead5cc509dea7d5d85/diff:/var/lib/docker/overlay2/bfd1d2da8bd90453b74a147381490cd10de75c91e57c6e42db1570177a72b3b3/diff",
                "MergedDir": "/var/lib/docker/overlay2/07063fc18b419e50fb2314ba2f1a31dfa69f526c3b683d9f3c0f187b53008888/merged",
                "UpperDir": "/var/lib/docker/overlay2/07063fc18b419e50fb2314ba2f1a31dfa69f526c3b683d9f3c0f187b53008888/diff",
                "WorkDir": "/var/lib/docker/overlay2/07063fc18b419e50fb2314ba2f1a31dfa69f526c3b683d9f3c0f187b53008888/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/home/javiermejia/Documents/GitHub/talentum-plus",
                "Destination": "/app",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "bind",
                "Source": "/home/javiermejia/.vscode/extensions/ms-python.python-2020.8.108011/pythonFiles/lib/python/debugpy",
                "Destination": "/debugpy",
                "Mode": "ro",
                "RW": false,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "02e358841c9b",
            "Domainname": "",
            "User": "appuser",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "8000/tcp": {}
            },
            "Tty": true,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/opt/bitnami/python/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "BITNAMI_APP_NAME=python",
                "BITNAMI_IMAGE_VERSION=3.7.6-debian-9-r30-prod",
                "PYTHONDONTWRITEBYTECODE=1",
                "PYTHONUNBUFFERED=1"
            ],
            "Cmd": null,
            "Image": "talentumplus:latest",
            "Volumes": null,
            "WorkingDir": "/app",
            "Entrypoint": [
                "python"
            ],
            "OnBuild": null,
            "Labels": {
                "com.microsoft.created-by": "visual-studio-code",
                "maintainer": "Bitnami <[email protected]>"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "6e31f76e46176cc1da2efda9ca18112ee0093eb60ce60ccd685b5d71469f7913",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "8000/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "8000"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/6e31f76e4617",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "444e8d02bfa04bf16cdadf8ecf71ddcb5cace8dc17774c4215c64b7cc37b3e1c",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "6ceed50894105ac809829deb938c1b10f3e7a19798c42102a56ed046386183b4",
                    "EndpointID": "444e8d02bfa04bf16cdadf8ecf71ddcb5cace8dc17774c4215c64b7cc37b3e1c",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

@bwateratmsft
Copy link
Collaborator

Everything looks pretty normal to me; I'm not able to reproduce at least on Windows. In theory it shouldn't matter the host OS.

@int19h do you know what might be happening? It seems like the Python extension is not executing the Python debug command (python ~/.vscode/extensions/ms-azuretools.vscode-docker-1.6.0/resources/python/launcher.py XXX -- manage.py ...) in order to start debugpy in the container, but VSCode is still waiting for it to attach, so the Python debug adapter is at least getting that far.

@bwateratmsft
Copy link
Collaborator

@jmejia32 I have one theory. I was able to get the same symptoms to reproduce if the debugAdapterHost that we resolve is incorrect or undefined.

Can you run this command and share the output? docker inspect bridge --format="{{(index .IPAM.Config 0).Gateway}}"

@jmejia32
Copy link
Author

Yes, the output is 172.17.0.1

@bwateratmsft
Copy link
Collaborator

Ok, if you put that into your launch config as below, does debugging work? Note, it goes at the same level as "type", i.e. it does not go into the "python" section.

image

It will give a yellow underline but that's OK, you can ignore that.

@jmejia32
Copy link
Author

Man, that was awesome!! It works. Finally I can debug my project, i'm so thankful.

Congrats!! You deserve my respect.

image

@bwateratmsft
Copy link
Collaborator

I'm glad to help! This step shouldn't be necessary though 😕

The most likely explanation I can think of is that somehow our code to get that IP address failed. The exact command line we use is docker inspect --format '{{(index .IPAM.Config 0).Gateway}}' 'bridge'.

Do you have a custom shell configured in VSCode, and does the command with the above ordering/syntax/quoting still work on your OS?

@jmejia32
Copy link
Author

@bwateratmsft yeah, actually I use fish as default shell. The commands explained here works without issues.

@bwateratmsft
Copy link
Collaborator

I found the root cause of this. The CliDockerClient was returning an IP address with a trailing newline, which got the Python extension / debugger confused. This is now fixed by #2331 which no longer uses the CLI to get the info. I'm not sure if this ever worked.

@bwateratmsft
Copy link
Collaborator

This is now fixed in Docker extension version 1.7.0.

@vscodebot vscodebot bot locked and limited conversation to collaborators Oct 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants