mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-22 23:00:57 +08:00
TSL
This commit is contained in:
parent
b3a96c0fe9
commit
fa8e387ece
@ -1,138 +0,0 @@
|
||||
[#]: subject: (Use VS Code to develop in containers)
|
||||
[#]: via: (https://opensource.com/article/21/7/vs-code-remote-containers-podman)
|
||||
[#]: author: (Brant Evans https://opensource.com/users/branic)
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (wxy)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
|
||||
Use VS Code to develop in containers
|
||||
======
|
||||
Create consistency to avoid problems when you have multiple developers
|
||||
working on the same project.
|
||||
![Women programming][1]
|
||||
|
||||
Coding and testing inconsistencies are a risk when you have multiple developers with different development environments working on a project. [Visual Studio Code][2] (VS Code) is an integrated development environment (IDE) that can help minimize these issues. It can be combined with containers to provide separate development environments for each application alongside a consistent development environment.
|
||||
|
||||
VS Code's [Remote - Containers extension][3] enables you to define a container, use that definition to build a container, and develop inside the container. This container definition can be checked into the source code repository along with the application code, which allows all developers to use the same definition to build and develop within a container.
|
||||
|
||||
By default, the Remote - Containers extension uses Docker to build and run the container, but it is easy to use [Podman][4] for container runtimes, and it enables using [rootless containers][5].
|
||||
|
||||
This article walks you through the setup to develop inside a rootless container using Podman with VS Code and the Remote - Containers extension.
|
||||
|
||||
### Initial configuration
|
||||
|
||||
Before continuing, ensure your Red Hat Enterprise Linux (RHEL) or Fedora workstation is updated with the latest errata and that VS Code and the Remote - Containers extension are installed. (See the [VS Code website][2] for more information on installing.)
|
||||
|
||||
Next, install Podman and its supporting packages with a simple `dnf install` command:
|
||||
|
||||
|
||||
```
|
||||
`$ sudo dnf install -y podman`
|
||||
```
|
||||
|
||||
After you install Podman, configure VS Code to use the Podman executable (instead of Docker) for interacting with the container. Within VS Code, navigate to **File > Preferences > Settings** and click the **>** icon next to **Extensions**. In the dropdown menu that appears, select **Remote - Containers**, and scroll down to find the **Remote > Containers: Docker Path** option. In the text box, replace docker with **podman**.
|
||||
|
||||
![Enter "podman" in the text box][6]
|
||||
|
||||
(Brant Evans, [CC BY-SA 4.0][7])
|
||||
|
||||
Now that the configurations are done, create and open a new folder or an existing folder for the project in VS Code.
|
||||
|
||||
### Define the container
|
||||
|
||||
This tutorial uses the example of creating a container for Python 3 development.
|
||||
|
||||
The Remote - Containers extension can add the necessary basic configuration files to the project folder. To add these files, open the Command Pallet by entering **Ctrl+Shift+P** on your keyboard, search for **Remote-Containers: Add Development Container Configuration Files**, and select it.
|
||||
|
||||
![Remote-Containers: Add Development Container Configuration Files][8]
|
||||
|
||||
(Brant Evans, [CC BY-SA 4.0][7])
|
||||
|
||||
In the next pop-up, define the type of development environment you want to set up. For this example configuration, search for the **Python 3** definition and select it.
|
||||
|
||||
![Select Python 3 definition][9]
|
||||
|
||||
(Brant Evans, [CC BY-SA 4.0][7])
|
||||
|
||||
Next, select the version of Python that will be used in the container. Select the **3 (default)** option to use the latest version.
|
||||
|
||||
![Select the 3 \(default\) option][10]
|
||||
|
||||
(Brant Evans, [CC BY-SA 4.0][7])
|
||||
|
||||
The Python configuration can also install Node.js, but for this example, **uncheck Install Node.js** and click OK.
|
||||
|
||||
![Uncheck "Install Node.js"][11]
|
||||
|
||||
(Brant Evans, [CC BY-SA 4.0][7])
|
||||
|
||||
It will create a `.devcontainer` folder containing files named `devcontainer.json` and `Dockerfile`. VS Code automatically opens the `devcontainer.json` file so that you can customize it.
|
||||
|
||||
### Enable rootless containers
|
||||
|
||||
In addition to the obvious security benefits, one of the other reasons to run a container as rootless is that all the files created in the project folder will be owned by the correct user ID (UID) outside the container. To run the development container as a rootless container, modify the `devcontainer.json` file by adding the following lines to the end of it:
|
||||
|
||||
|
||||
```
|
||||
"workspaceMount": "source=${localWorkspaceFolder},target=/workspace,type=bind,Z",
|
||||
"workspaceFolder": "/workspace",
|
||||
|
||||
"runArgs": ["--userns=keep-id"],
|
||||
"containerUser": "vscode"
|
||||
```
|
||||
|
||||
These options tell VS Code to mount the Workspace with the proper SELinux context, create a user namespace that maps your UID and GID to the same values inside the container, and use `vscode` as your username inside the container. The `devcontainer.json` file should look like this (don't forget the commas at the end of the lines, as indicated):
|
||||
|
||||
![Updated devcontainer.json file][12]
|
||||
|
||||
(Brant Evans, [CC BY-SA 4.0][7])
|
||||
|
||||
Now that you've set up the container configuration, you can build the container and open the workspace inside it. Reopen the Command Palette (with **Ctrl+Shift+P**), and search for **Remote-Containers: Rebuild and Reopen in Container**. Click on it, and VS Code will start to build the container. Now is a great time to take a break (and get your favorite beverage), as building the container may take several minutes.
|
||||
|
||||
![Building the container][13]
|
||||
|
||||
(Brant Evans, [CC BY-SA 4.0][7])
|
||||
|
||||
Once the container build completes, the project will open inside the container. Files created or edited within the container will be reflected in the filesystem outside the container with the proper user permissions applied to the files. Now, you can proceed with development within the container. VS Code can even bring your SSH keys and Git configuration into the container so that committing code will work just like it does when editing outside the container.
|
||||
|
||||
### Next steps
|
||||
|
||||
Now that you've completed the basic setup and configuration, you can further enhance the configuration's usefulness. For example:
|
||||
|
||||
* Modify the Dockerfile to install additional software (e.g., required Python modules).
|
||||
* Use a customized container image. For example, if you're doing Ansible development, you could use Quay.io's [Ansible Toolset][14]. (Be sure to add the `vscode` user to the container image via the Dockerfile.)
|
||||
* Commit the files in the `.devcontainer` directory to the source code repository so that other developers can take advantage of the container definition for their development efforts.
|
||||
|
||||
|
||||
|
||||
Developing inside a container helps prevent conflicts between different projects by keeping the dependencies and code for each separate. You can use Podman to run containers in a rootless environment that increases security. By combining VS Code, the Remote - Containers extension, and Podman, you can easily set up a consistent environment for multiple developers, decrease setup time, and reduce bugs from differences in development environments in a secure fashion.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/21/7/vs-code-remote-containers-podman
|
||||
|
||||
作者:[Brant Evans][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/branic
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/collab-team-pair-programming-code-keyboard2.png?itok=WnKfsl-G (Women programming)
|
||||
[2]: https://code.visualstudio.com/
|
||||
[3]: https://code.visualstudio.com/docs/remote/containers
|
||||
[4]: https://podman.io/
|
||||
[5]: https://www.redhat.com/sysadmin/rootless-podman-makes-sense
|
||||
[6]: https://opensource.com/sites/default/files/uploads/vscode-remote_podman.png (Enter "podman" in the text box)
|
||||
[7]: https://creativecommons.org/licenses/by-sa/4.0/
|
||||
[8]: https://opensource.com/sites/default/files/uploads/adddevelopmentcontainerconfigurationfiles.png (Remote-Containers: Add Development Container Configuration Files)
|
||||
[9]: https://opensource.com/sites/default/files/uploads/python3.png (Select Python 3 definition)
|
||||
[10]: https://opensource.com/sites/default/files/uploads/python3default.png (Select the 3 (default) option)
|
||||
[11]: https://opensource.com/sites/default/files/uploads/unchecknodejs.png (Uncheck "Install Node.js")
|
||||
[12]: https://opensource.com/sites/default/files/uploads/newdevcontainerjson.png (Updated devcontainer.json file)
|
||||
[13]: https://opensource.com/sites/default/files/uploads/buildingcontainer.png (Building the container)
|
||||
[14]: https://quay.io/repository/ansible/toolset
|
124
translated/tech/20210713 Use VS Code to develop in containers.md
Normal file
124
translated/tech/20210713 Use VS Code to develop in containers.md
Normal file
@ -0,0 +1,124 @@
|
||||
[#]: subject: (Use VS Code to develop in containers)
|
||||
[#]: via: (https://opensource.com/article/21/7/vs-code-remote-containers-podman)
|
||||
[#]: author: (Brant Evans https://opensource.com/users/branic)
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (wxy)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
|
||||
使用 VS Code 在容器中开发
|
||||
======
|
||||
|
||||
>创建一致性,以避免当你有多个开发人员开发同一个项目时出现问题。
|
||||
|
||||
![Women programming][1]
|
||||
|
||||
当你有多个不同开发环境的开发人员在一个项目上工作时,编码和测试的不一致性是一种风险。[Visual Studio Code][2](VS Code)是一个集成开发环境(IDE),可以帮助减少这些问题。它可以和容器结合起来,为每个应用程序提供独立的开发环境,同时提供一个一致的开发环境。
|
||||
|
||||
VS Code 的 [Remote - Containers 扩展][3] 使你能够定义一个容器,使用该定义来构建一个容器,并在容器内进行开发。这个容器定义可以和应用程序代码一起被签入到源代码库中,这使得所有的开发人员可以使用相同的定义在容器中进行构建和开发。
|
||||
|
||||
默认情况下,Remote - Containers 扩展使用 Docker 来构建和运行容器,但使用 [Podman][4] 的容器运行环境环境也很容易,它可以让你使用 [免 root 容器][5]。
|
||||
|
||||
本文将带领你完成设置,通过 Podman 在免 root 容器内使用 VS Code 和 Remote - Containers 扩展进行开发。
|
||||
|
||||
### 初始配置
|
||||
|
||||
在继续之前,请确保你的红帽企业 Linux(RHEL)或 Fedora 工作站已经更新了最新的补丁,并且安装了 VS Code 和 Remote - Containers 扩展。(参见 [VS Code 网站][2]了解更多安装信息)
|
||||
|
||||
接下来,用一个简单的 `dnf install` 命令来安装 Podman 和它的支持包:
|
||||
|
||||
|
||||
```
|
||||
$ sudo dnf install -y podman
|
||||
```
|
||||
|
||||
安装完 Podman 后,配置 VS Code 以使用 Podman 的可执行文件(而不是 Docker)与容器进行交互。在 VS Code 中,导航到 “文件 > 首选项 > 设置”,点击 “扩展” 旁边的 “>” 图标。在出现的下拉菜单中,选择 “Remote - Containers”,并向下滚动找到 “Remote - Containers: Docker Path” 选项。在文本框中,用 “podman” 替换 “docker”。
|
||||
|
||||
![在文本框中输入 “podman”][6]
|
||||
|
||||
现在配置已经完成,在 VS Code 中为该项目创建一个新的文件夹或并打开现有的文件夹。
|
||||
|
||||
### 定义容器
|
||||
|
||||
本教程以创建 Python 3 开发的容器为例。
|
||||
|
||||
Remote - Containers 扩展可以在项目文件夹中添加必要的基本配置文件。要添加这些文件,通过在键盘上输入 `Ctrl+Shift+P` 打开命令面板,搜索 “Remote-Containers: Add Development Container Configuration Files”,并选择它。
|
||||
|
||||
![Remote-Containers: Add Development Container Configuration Files][8] 。
|
||||
|
||||
在接下来的弹出窗口中,定义你想设置的开发环境的类型。对于这个例子的配置,搜索 “Python 3” 定义并选择它。
|
||||
|
||||
![选择 Python 3 定义][9] 。
|
||||
|
||||
|
||||
接下来,选择将在容器中使用的 Python 的版本。选择 “3 (default)” 选项以使用最新的版本。
|
||||
|
||||
![选择 “3 (default)” 选项][10]
|
||||
|
||||
Python 配置也可以安装 Node.js,但在这个例子中,*取消勾选“Install Node.js”,然后点击 “OK”。
|
||||
|
||||
![取消勾选 “Install Node.js"][11]
|
||||
|
||||
它将创建一个 `.devcontainer` 文件夹,包含文件`devcontainer.json`和`Dockerfile`。VS Code 会自动打开`devcontainer.json` 文件,这样你就可以对它进行自定义。
|
||||
|
||||
### 启用免 root 容器
|
||||
|
||||
除了明显的安全优势外,以免 root 方式运行容器的另一个原因是,在项目文件夹中创建的所有文件将由容器外的正确用户 ID(UID)拥有。要将开发容器作为免 root 容器运行,请修改 `devcontainer.json` 文件,在它的末尾添加以下几行:
|
||||
|
||||
|
||||
```
|
||||
"workspaceMount": "source=${localWorkspaceFolder},target=/workspace,type=bind,Z",
|
||||
"workspaceFolder": "/workspace",
|
||||
|
||||
"runArgs": ["--userns=keep-id"],
|
||||
"containerUser": "vscode"
|
||||
```
|
||||
|
||||
这些选项告诉 VS Code 用适当的 SELinux 上下文挂载工作区,创建一个用户命名空间,将你的 UID 和 GID 同样映射到容器内,并在容器内使用 `vscode` 作为你的用户名。`devcontainer.json` 文件应该是这样的(别忘了行末的逗号,如图所示):
|
||||
|
||||
![更新后的 devcontainer.json 文件][12]
|
||||
|
||||
现在你已经设置好了容器的配置,你可以构建容器并打开里面的工作空间。重新打开命令调板(用 `Ctrl+Shift+P`),并搜索 “Remote-Containers: Rebuild and Reopen in Container”。点击它,VS Code 将开始构建容器。现在是休息一下的好时机(拿上你最喜欢的饮料),因为构建容器可能需要几分钟时间:
|
||||
|
||||
![构建容器][13]
|
||||
|
||||
一旦容器构建完成,项目将在容器内打开。在容器内创建或编辑的文件将反映在容器外的文件系统中,并对这些文件应用适当的用户权限。现在,你可以在容器内进行开发。VS Code 甚至可以把你的 SSH 密钥和 Git 配置带入容器中,这样提交代码就会像在容器外编辑时那样工作。
|
||||
|
||||
### 接下来的步骤
|
||||
|
||||
现在你已经完成了基本的设置和配置,你可以进一步加强配置的实用性。比如说:
|
||||
|
||||
* 修改 Dockerfile 以安装额外的软件(例如,所需的 Python 模块)。
|
||||
* 使用一个定制的容器镜像。例如,如果你正在进行 Ansible 开发,你可以使用 Quay.io 的 [Ansible Toolset][14]。(确保通过 Dockerfile 将 `vscode` 用户添加到容器镜像中)。)
|
||||
* 将 `.devcontainer` 目录下的文件提交到源代码库,以便其他开发者可以利用容器的定义进行开发工作。
|
||||
|
||||
在容器内开发有助于防止不同项目之间的冲突,因为不同项目的依赖关系及代码都是分开的。你可以使用 Podman 在免 root 环境下运行容器,从而提高安全性。通过结合 VS Code、Remote - Containers 扩展和 Podman,你可以轻松地为多个开发人员建立一个一致的环境,减少设置时间,并以安全的方式减少开发环境的差异带来的错误。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/21/7/vs-code-remote-containers-podman
|
||||
|
||||
作者:[Brant Evans][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[wxy](https://github.com/wxy)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/branic
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/collab-team-pair-programming-code-keyboard2.png?itok=WnKfsl-G (Women programming)
|
||||
[2]: https://code.visualstudio.com/
|
||||
[3]: https://code.visualstudio.com/docs/remote/containers
|
||||
[4]: https://podman.io/
|
||||
[5]: https://www.redhat.com/sysadmin/rootless-podman-makes-sense
|
||||
[6]: https://opensource.com/sites/default/files/uploads/vscode-remote_podman.png (Enter "podman" in the text box)
|
||||
[7]: https://creativecommons.org/licenses/by-sa/4.0/
|
||||
[8]: https://opensource.com/sites/default/files/uploads/adddevelopmentcontainerconfigurationfiles.png (Remote-Containers: Add Development Container Configuration Files)
|
||||
[9]: https://opensource.com/sites/default/files/uploads/python3.png (Select Python 3 definition)
|
||||
[10]: https://opensource.com/sites/default/files/uploads/python3default.png (Select the 3 \(default\) option)
|
||||
[11]: https://opensource.com/sites/default/files/uploads/unchecknodejs.png (Uncheck "Install Node.js")
|
||||
[12]: https://opensource.com/sites/default/files/uploads/newdevcontainerjson.png (Updated devcontainer.json file)
|
||||
[13]: https://opensource.com/sites/default/files/uploads/buildingcontainer.png (Building the container)
|
||||
[14]: https://quay.io/repository/ansible/toolset
|
Loading…
Reference in New Issue
Block a user