简体   繁体   English

在 vscode 中选择 docker 容器的解释器

[英]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.在 vscode 中,我们可以安装 python 扩展,然后选择我们想要的解释器,比如 conda 环境中的 python。

So we can use "shift" + "enter" key for running the code line by line in the terminal.所以我们可以使用“shift”+“enter”键在终端中逐行运行代码。 For managing different virtual environment, using docker container is a better way.对于管理不同的虚拟环境,使用 docker 容器是一种更好的方法。

If I already install the docker, and pull the python image.如果我已经安装了 docker,并拉取了 python 图像。 How to select the interpreter which is created in the docker container?如何选择在 docker 容器中创建的解释器? Not just remote to the docker container.不只是远程到 docker 容器。

Tested on VSCode 1.61:在 VSCode 1.61 上测试:

  1. Install the Remote-Containers extension安装Remote-Containers扩展
  2. Build/start the Docker container构建/启动 Docker 容器
  3. Open the Command Palette and type Remote-Containers: Attach to Running Container... , then select the running Docker container打开命令面板并输入Remote-Containers: Attach to Running Container... ,然后选择正在运行的 Docker 容器
  4. VSCode will restart and reload VSCode 将重新启动并重新加载
  5. On the Get Started page, click the Open... and enter the path to the Docker volume mounted to your source code.Get Started页面上,单击Open...并输入安装到源代码的 Docker 卷的路径。 It must be set to the same path as WORKDIR in your Dockerfile-local , eg to /app .它必须设置为与Dockerfile-local中的WORKDIR相同的路径,例如/app
  6. Install the Python extension on the container在容器上安装Python扩展
  7. Open the Command Palette and type Python: Select Interpreter , then select the Docker interpreter打开命令面板并输入Python: Select Interpreter ,然后选择 Docker 解释器
  8. Open the Command Palette and type Python: Configure Tests , then select the framework you use打开命令面板并输入Python: Configure Tests ,然后选择您使用的框架

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

UPD. UPD。 Remote development extensions seem to be one of the main focuses in VSCode development currently, eg the newer versions have got the Remote explorer Activity tab enabled by default, which allows much more intuitive approach to connecting to Docker containers. Remote development extensions似乎是目前 VSCode 开发的主要焦点之一,例如,较新的版本默认启用了Remote explorer Activity 选项卡,这允许更直观地连接到 Docker 容器。 Check release notes here: https://github.com/microsoft/vscode-docs/tree/main/remote-release-notes在此处查看发行说明: https ://github.com/microsoft/vscode-docs/tree/main/remote-release-notes

在 vscode 创建的devcontainer.json文件中,您有image键,它的值是图像的路径,如果您想使用更改 python 版本,您可以在此处执行此操作或使用 vscode 文档中的快速说明

I think it's impossible, I am afraid you must remote to the docker container.我认为这是不可能的,恐怕你必须远程到 docker 容器。

I really can't imagine out you taking a python interpreter in Linux to work on windows directly.我真的无法想象你在 Linux 中使用 python 解释器直接在 Windows 上工作。

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.如果您的目标是让 vscode 在本地项目上工作并使用基于 docker 的解释器运行它,则解决方案是:将本地项目目录安装到包含解释器的 docker 容器中,然后在 vscode 中打开项目目录(已安装) 从容器中。

How to mount your project directory:如何挂载你的项目目录:

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

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

I have tested it.我已经测试过了。 It should work.它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM