Gilbert François Duivesteijn
This is a guide on how to setup a development environment for python in a
docker container using NeoVim as the editor and the remote plugin to connect
to the container. Although this is just showing how to setup python, you can
use the same setup for other languages as well, e.g. C and C++ using codelldb.

In your debug.lua section, you can add the following:
-- Python debugging setup
local debugpy_path = require('mason-registry').get_package('debugpy'):get_install_path()
require('dap-python').setup(debugpy_path .. '/venv/bin/python')
dap.adapters.python = {
type = 'server',
host = 'localhost',
port = 5678,
options = {
source_filetype = 'python',
},
}
dap.configurations.python = {
{
type = 'python',
request = 'attach',
connect = {
host = 'localhost',
port = 5678,
},
mode = 'remote',
name = 'Attach to Docker python in /app',
redirectOutput = true,
justMyCode = true,
pathMappings = {
{
localRoot = vim.fn.getcwd(),
remoteRoot = '/app',
},
},
},
}Notes:
- The
pathMappingssection is important. It tells the debugger where to find the source code in the container. In this case, the source code is in the/appdirectory in the container. If you miss this step, the breakpoints will not work. - You can add more configurations for different python projects. Just copy the
dap.configurations.pythonsection and change thenameandpathMappingsto fit your project. - Enable
redirectOutputto see the output of the python script in the terminal window.
For a full example, see my personal nvim config repository: gilbertfrancois/nvim
To build the docker image locally, you can use the following command:
docker buildx build -t my-python-container:latest --local .The CMD inside the docker container is set to (see the Dockerfile):
python -Xfrozen_modules=off -m debugpy --listen 0.0.0.0:5678 --wait-for-client /app/main.pyNotes:
- The
--wait-for-clientflag is important. It tells the debugpy server to wait for a client to connect before starting the python script. - The
-Xfrozen_modules=offflag is needed to disable the frozen modules feature in python. This is needed because the debugpy server needs to be able to import thedebugpymodule.
This starts the debugpy server and waits for a client to connect. Start the container with:
docker run --rm -v $(pwd):/app -p 5678:5678 my-python-container:latestNote: The -v $(pwd):/app flag mounts the current directory to the /app. This
is important because the pathMappings section in the debug.lua file expects
the source code to be in the /app directory.
To start debugging in NeoVim, you can use the following command:
:DapContinueThis will connect to the debugpy server running in the docker container at 0.0.0.0:5678. The breakpoints should work now and you can start debugging your python code.