Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select interpreter of docker container in the vscode

In vscode, we can install the python extension, then select the interpreter which we want, like python in the conda environment.

So we can use "shift" + "enter" key for running the code line by line in the terminal. For managing different virtual environment, using docker container is a better way.

If I already install the docker, and pull the python image. How to select the interpreter which is created in the docker container? Not just remote to the docker container.

like image 397
Hou ZeYu Avatar asked Sep 05 '25 03:09

Hou ZeYu


2 Answers

Tested on VSCode 1.61:

  1. Install the Remote-Containers extension
  2. Build/start the Docker container
  3. Open the Command Palette and type Remote-Containers: Attach to Running Container..., then select the running Docker container
  4. VSCode will restart and reload
  5. On the Get Started page, click the Open... and enter the path to the Docker volume mounted to your source code. It must be set to the same path as WORKDIR in your Dockerfile-local, e.g. to /app.
  6. Install the Python extension on the container
  7. Open the Command Palette and type Python: Select Interpreter, then select the Docker interpreter
  8. Open the Command Palette and type Python: Configure Tests, then select the framework you use

Source: https://dev.to/alvarocavalcanti/setting-up-a-python-remote-interpreter-using-docker-1i24

UPD #1. Remote development extensions seem to be one of the main focuses in VSCode development currently, e.g. the newer versions have got the Remote explorer Activity tab enabled by default, which allows much more intuitive approach to connecting to Docker containers. Check release notes here: https://github.com/microsoft/vscode-docs/tree/main/remote-release-notes

UPD #2. Nowadays you should use Dev Containers instead of Remote Containers

See the docs: https://code.visualstudio.com/docs/devcontainers/containers

  1. Create .devcontainer/devcontainer.json
{
    "name": "My Devcontainer",
    "dockerComposeFile": "docker-compose.yml",
    "service": "app",
    "workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
    "customizations": {
        ...

Docs: https://containers.dev/implementors/json_reference/

  1. Add your Dockerfile, docker-compose.yml, .env to .devcontainers
...
services:
  app:
    build:
      context: ..
      dockerfile: .devcontainer/Dockerfile

    volumes:
      - ../..:/workspaces:cached

    env_file: .env
...
  1. Use Dev containers: Open folder in container and Dev containers: Rebuild container prompts to proceed
like image 166
Aliaksandr Adzinets Avatar answered Sep 07 '25 15:09

Aliaksandr Adzinets


If your objective is to have vscode to work on a local project and run it with a docker-based interpreter, the solution is: mounting the local project directory to the docker container that contains the interpreter, then in vscode open the project directory (mounted) from the container.

How to mount your project directory:

docker run -v /user/localproject:/root/mountedproject

https://docs.docker.com/storage/volumes/

I have tested it. It should work.

like image 22
CyberPlayerOne Avatar answered Sep 07 '25 17:09

CyberPlayerOne