dockervisual-studio-codenvidiaptvsnvidia-docker

Run an NGC Container in VS Code on Ubuntu


I need to run a Python script within an NVIDA GPU Cloud (NGC) container on Docker in Ubuntu and I want to use Visual Studio Code to edit, run and debug it. I have installed the VS Code Docker Extension and read the documentation but none of it seems to fit my purpose.

I have followed the NGC docs, installed the NVIDIA Container Runtime for Docker (nvidia-docker2) and am now at the point where on the command line I would launch an NGC container tarball

docker load -i  foo.tar
sudo docker run {...}

How do I configure VS Code so that I can run and debug Python scripts within this container?


Solution

  • Download the NVIDA GPU Cloud (NGC) container.

    Create /home/bob/foobar.py in Visual Studio Code with the VS Code Docker Extension

    import ptvsd
    import time
    ptvsd.enable_attach(address = ('0.0.0.0', 5678))
    ptvsd.wait_for_attach()
    time.sleep(2)
    print("all righty then")
    

    Set a breakpoint on the last line.

    Debug|Add Configuration

    Docker: Attach to Node

    In launch.json add to "configurations"

    {
       "name": "Python Attach (Remote Debug ptsvd default)",
       "type": "python",
       "request": "attach",
       "pathMappings": [
           {
              "localRoot": "/home/bob", // You may also manually specify the directory containing your source code.
              "remoteRoot": "/home/bob" // Linux example; adjust as necessary for your OS and situation.
           }
        ],
                "port": 5678, // Set to the remote port.
                "host": "0.0.0.0" // Set to your remote host's public IP address.
    },
    

    Open a Terminal window:

    $ docker load -i foo.tar
    $ docker images
    REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
    nvidia/cuda           9.0-base            9dcd7cd95db6        2 weeks ago         135MB
    nvcr.io/nvidia/cuda   latest              506c995952d1        7 weeks ago         2.74GB    
    
    $ docker run -p 5678:5678 latest    
    
    root@deadbeef: python -m pip install --user --upgrade ptvsd
    root@deadbeef: python foobar.py 
    

    Start the debugger with configuration "Python Attach (Remote Debug ptsvd default)". It stops at the breakpoint.