[译]使用Visual Studio代码在容器内部开发服务
By robot-v1.0
本文链接 https://www.kyfws.com/applications/using-visual-studio-code-to-develop-a-service-insi-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 6 分钟阅读 - 2989 个词 阅读量 0[译]使用Visual Studio代码在容器内部开发服务
原文地址:https://www.codeproject.com/Articles/5163823/Using-Visual-Studio-Code-to-Develop-a-Service-Insi
原文作者:Dmitriy Repin
译文由本站 robot-v1.0 翻译
前言
Using VS Code to author and debug an any-language service inside of a container running locally, on a cloud VM, or in a Kubernetes pod
使用VS Code在本地运行的容器,云VM或Kubernetes容器中创作和调试任何语言的服务
介绍(Introduction)
Docker容器的优势是众所周知的.容器提供:(The advantages of Docker containers are well known. Containers provide:)
- 一致的开发/执行环境(consistent development/execution environment)
- 与其他应用程序或本地计算机更改隔离(isolation from other applications or local machine changes)
- 容器包含所有必需的依赖项,并确保其正确的版本(a container includes all required dependencies and ensures their right versions)
- 无需任何更改,就可以在Kubernetes容器中的云计算节点上本地部署容器.(without any changes, a container can be deployment locally, on a cloud compute node, in a Kubernetes pod) 但是,以上所有优点都是有代价的.直到最近,在开发容器化应用程序时,还不能直接使用他/她喜欢的IDE工具(例如VS Code,PyCharm,CLion,Eclipse)来编辑文件或调试运行容器的应用程序.(However, all the above advantages come at the price. Until recently, when developing a containerized application, one could not directly use his/her favorite IDE tools (e.g., VS Code, PyCharm, CLion, Eclipse) to edit files or to debug an application running a container.)
开发人员必须解决快捷方式和解决方法:(Developers had to resolve to shortcuts and workarounds:)
- 如果开发是在本地运行的容器中完成的,则可以在容器内部安装本地文件系统,并使用IDE在本地查看容器中的更改的同时编辑文件.(If development is done in a container running locally, it is possible to mount the local file system inside of the container and use an IDE to edit files locally while seeing changes in the container.)
- 为了进行调试,有人尝试在容器内部安装远程调试器服务器(例如,对于C ++为gdbserver,对于Python为ptvsd).(For debugging, some tried installing a remote debugger server inside of the container (e.g., gdbserver for C++, ptvsd for Python).)
- 其他开发人员将在容器内安装Windowing System,+ VNC服务器+ IDE,并使用VNC客户端在容器内进行连接和工作.(Other developers would install a Windowing System, + VNC server + IDE inside of the container and use a VNC client to connect and work within the container.)
- 但是,大多数开发人员会完全放弃IDE工具,而使用旧式(但功能强大)的终端编辑器(例如vi或emacs)和命令行调试器(例如gdb)(However, the majority of developers would abandon IDE tools altogether and use legacy (but powerful) terminal editors (e.g., vi or emacs) and command-line debuggers (e.g., gdb))
发生了什么变化?(What Changed?)
Visual Studio Code(版本1.35)于2019年5月发行,其中包含全新的远程开发和远程容器扩展.这些扩展允许人们使用Docker容器(本地,远程,Kubernetes容器)作为功能齐全的开发环境来编辑和调试VS Code或其插件之一支持的任何语言.(The May 2019 release of the Visual Studio Code (version 1.35) came with the brand-new Remote Development and Remote Container extensions. Those extensions allow one to use a Docker container (local, remote, Kubernetes pod) as a full-featured development environment to edit and debug any language supported by the VS Code or one of its plugins.)唯一的要求是对容器的SSH访问(The only requirement is SSH access to the container).(.)
本文将展示使用VS Code编辑文件并调试正在运行的容器中的C ++或Python服务现在多么容易.(This article will show how easy it is now to use VS Code to edit files and debug C++ or Python services within a running container.)
更多信息(More Information)
- 在容器内开发(Developing inside a Container) https://code.visualstudio.com/docs/remote/containers(https://code.visualstudio.com/docs/remote/containers)
- 先进的容器配置(Advanced Container Configuration) https://code.visualstudio.com/docs/remote/containers-advance(https://code.visualstudio.com/docs/remote/containers-advance)
先决条件(Prerequisites)
在撰写本文时,我使用了Windows 10企业级笔记本电脑(后称本地系统)和Google Cloud Compute Engine VM(后称远程系统).但是,所描述的工作流程将与其他云提供商一起使用.(While working on this article, I used a Windows 10 Enterprise laptop (later referred as the local system) and a Google Cloud Compute Engine VM (later referred as the remote system). However, the described workflow will work with other cloud providers.)
本地系统需要安装以下软件:(The local system needs to have the following software installed:)
-
Windows 10 OpenSSH客户端(Windows 10 OpenSSH client) (看到((see) https://docs.microsoft.com/zh-cn/windows-server/administration/openssh/openssh_install_firstuse(https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse) )())
-
Windows版Docker桌面(Docker Desktop for Windows) (看到((see) https://docs.docker.com/docker-for-windows/install/(https://docs.docker.com/docker-for-windows/install/) )())
-
Visual Studio代码及其插件:(Visual Studio Code and its plugins:)
- 远程开发扩展包((Remote Development extension pack ()
ms-vscode-remote.vscode-remote-extensionpack
)()) - 码头工人((Docker ()
ms-azuretools.vscode-docker
)())
- 远程开发扩展包((Remote Development extension pack ()
-
您将需要在本地系统上生成并安装SSH私钥((You will need to generate and install an SSH private key on your local system () https://www.linode.com/docs/security/authentication/use-public-key-authentication-with-ssh/(https://www.linode.com/docs/security/authentication/use-public-key-authentication-with-ssh/) )()) 远程系统需要安装以下软件:(The remote system needs to have the following software installed:)
-
您需要知道远程系统的IP地址.(You need to know an IP address of the remote system.)
-
必须为远程系统配置基于SSH密钥的身份验证(The remote system must be configured for SSH key-based authentication) ((() https://www.digitalocean.com/community/tutorials/how-to-configure-ssh-key-based-authentication-on-a-linux-server(https://www.digitalocean.com/community/tutorials/how-to-configure-ssh-key-based-authentication-on-a-linux-server) ).().) 您将需要在远程系统上安装公共SSH密钥.(You will need to install your public SSH key on the remote system.)
-
Docker Engine-社区版(Docker Engine - Community Edition) (例如.,((e.g.,) https://docs.docker.com/install/linux/docker-ce/centos/(https://docs.docker.com/install/linux/docker-ce/centos/) )()) 确保已为首选容器注册表配置了访问权限(Make sure that you have configured access permissions for your preferred container registry) (例如.,((e.g.,) https://cloud.google.com/container-registry/docs/advanced-authentication(https://cloud.google.com/container-registry/docs/advanced-authentication) )()) 要验证您的设置是否正确,请确保可以使用其IP地址建立到远程系统的SSH连接:(To verify, that you have a correct setup, make sure that you can establish an SSH connection to your remote system using its IP address:)
PS C:\> ssh drepin@10.128.0.79
Last login: Thu Aug 1 14:17:46 2019 from openvpn.internal
drepin@drepin-dev ~
在远程系统上,插入要使用的容器:(and on the remote system, pull-in a container that you are going to work with:)
drepin@drepin-dev$ docker pull gcr.io/your-project/server-dev
在本地系统上设置(Setup on the Local System)
- 启动第一个PowerShell,并发出以下命令以将本地Docker客户端连接到远程主机上的Docker引擎:(Launch the first PowerShell and issue the following command to connect your local Docker client to a Docker Engine on the remote host:)
- 创建一个空文件夹并放置一个(Create an empty folder and place a)**$ {FOLDER}/.devcontainer.json(${FOLDER}/.devcontainer.json)**归档进去.(file into it.) 文件格式在(The format of the file is described at) https://aka.ms/vscode-remote/devcontainer.json(https://aka.ms/vscode-remote/devcontainer.json) .(.) 以下是在容器中进行C ++远程开发的此类文件的示例:(Below is an example of such a file for C++ remote development in a container:)
- 启动第二个PowerShell,然后发出以下命令来设置(Launch the second PowerShell and issue the following commands to setup the)
DOCKER_HOST
环境变量,然后在刚刚创建的文件夹中启动VS Code:(environment variable and start a VS Code inside of the just created folder:) - 在VS代码中,运行命令"(In VS code, run the command “)远程容器:重新打开容器中的文件夹(Remote-Containers: Reopen Folder in Container)",只需点击"(” either by clicking on the “)在容器中重新打开(Reopen in Container)“按钮…(” button…)
…或通过键入(…or by typing) + + P(++P)"(")远程容器:重新打开容器中的文件夹(Remote-Containers: Reopen Folder in Container)”.(".)
VS Code将开始在容器内部安装VS Code Server和插件:(The VS Code will start installing the VS Code Server and plugins inside of the container:)
一两分钟后,您就可以在容器中编辑,构建和调试代码.(and in a minute or two, you will be able to edit, build, and debug your code within the container.)
临时限制(Temporary Limitations)
VS Code尚不能与非根容器(例如,从此类Dockerfile创建的容器)一起顺利运行:(VS Code does not yet work smoothly with non-root containers, e.g., with a container created from such Dockerfile:)
FROM ubuntu:18.04
USER 1000
要解决此限制,建议您以用户0身份启动容器,并使用VS Code附加到容器:(To work around this limitation, I suggest you start container as user 0 and attach to it using VS Code:)
docker run -it --rm --user 0 --entrypoint /bin/sh gcr.io/your-project/server-dev
我还观察到Microsoft C/C ++插件中的一些小错误((I also observed some minor bugs in the Microsoft C/C++ plugin () ms-vscode.cpptools
)在容器中运行时.() when it runs within a container.)
历史(History)
- 3(3)rd(rd)2019年8月:初始版本(August, 2019: Initial version)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
Python C++ Google cloud Windows Azure AWS Visual-Studio containers virtual-machine 新闻 翻译