Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up a python dev environment using Docker (with linting and code completion in vscode)

Setup

I'm exploring the ways of setting up a python dev environment inside a docker container.

In my local (host) I have the project directory:

.
├── Dockerfile
└── foo.py

Where my Dockerfile is:

FROM python:latest

RUN pip install --upgrade pip
RUN pip install matplotlib

RUN mkdir /src
WORKDIR /src

ENTRYPOINT [ "python" ]

and the script is:

try:
    import matplotlib
    print("import successfully")
except ImportError:
    print("unable to import matplotlib")

Assuming I don't have matplotlib installed on my host, then python foo.py yields unable to import matplotlib. When I run inside the container:

docker run -it --rm -v ~/tmp/:/src/ py-deb-test foo.py

I get import successfully.

Problem

Now, my intention is to work on my code using vscode on the local host and run it in the container. The problem is that in this setting I don't know how to point vscode to the python "installation" running in the container. I thought of starting the container and "mounting" the python from the container to a local location and point vscode to this location. But so far it didn't work.

Without solving this, vscode is not aware of what packages are installed, where to find the linter, etc...

Any help and ideas would be welcome!

like image 663
Dror Avatar asked Sep 12 '25 03:09

Dror


1 Answers

Good news. The folks at VScode recently released what seems to be exactly what I was looking for. Using the containers remote VScode it is now possible to run the editor on a container which has the exact environment that one needs. In this repo I compiled a minimal example and I hope you'd find it useful.

like image 142
Dror Avatar answered Sep 14 '25 18:09

Dror