附加到正在运行的容器

Visual Studio Code 可以为您创建并启动容器,但这可能不符合您的工作流程,您可能更倾向于将 VS Code“附加”到一个已经在运行的 Docker 容器上——无论它是如何启动的。一旦附加,您可以像使用 devcontainer.json 在容器中打开文件夹时一样,安装扩展、编辑和调试。

附加到Docker容器

要附加到Docker容器,可以从命令面板(F1)中选择Dev Containers: Attach to Running Container...,或者在活动栏中使用Remote Explorer,并从Containers视图中选择要连接的容器上的Attach to Container内联操作。

容器资源管理器截图

注意: 使用Alpine Linux容器时,由于扩展中的本地代码依赖glibc,某些扩展可能无法正常工作。

附加容器配置文件

VS Code 支持图像或容器名称级别的配置文件,以加快在您重复连接到给定的 Docker 容器时的设置。一旦连接,任何时候您打开文件夹,安装扩展,或转发端口,本地图像特定的配置文件将自动更新以记住您的设置,以便当您再次连接时,一切都回到正确的位置。

  • 默认情况下,使用镜像级别的配置。在附加后查看或更新它,请从命令面板中选择Dev Containers: 打开容器配置文件F1)。

  • 如果您希望将配置与容器名称绑定,请在连接后从命令面板(F1)中选择Dev Containers: 打开命名配置文件。从此时起的任何更新都将应用于此名称级别的配置,而不是镜像级别。

这两个文件都支持devcontainer.json属性的一个子集:

{
  // Default path to open when attaching to a new container.
  "workspaceFolder": "/path/to/code/in/container/here",

  // Set *default* container specific settings.json values on container create.
  "settings": {
    "terminal.integrated.defaultProfile.linux": "bash"
  },

  // Add the IDs of extensions you want installed when the container is created.
  "extensions": ["dbaeumer.vscode-eslint"],

  // An array port numbers to forward
  "forwardPorts": [8000],

  // Container user VS Code should use when connecting
  "remoteUser": "vscode",

  // Set environment variables for VS Code and sub-processes
  "remoteEnv": { "MY_VARIABLE": "some-value" }
}

请参阅附加容器配置参考以获取完整的属性列表及其用途。

一旦保存,每当您首次使用相同的镜像/容器名称打开容器时,这些属性将用于配置环境。

提示: 如果您的配置有问题,您也可以在未连接到容器时通过从命令面板(F1)中选择开发容器:打开附加容器配置文件...来编辑它,然后从显示的列表中选择镜像/容器名称。

最后,如果您希望无论连接到哪个容器都安装某些扩展,您可以更新settings.json以指定一个始终应安装的扩展列表

附加容器配置参考

附加的容器配置文件类似于devcontainer.json,并支持其属性的一个子集。

Property Type Description
workspaceFolder string Sets the default path that VS Code should open when connecting to the container (which is often the path to a volume mount where the source code can be found in the container). Not set by default (an empty window is opened).
extensions array An array of extension IDs that specify the extensions that should be installed inside the container when it is created. Defaults to [].
settings object Adds default settings.json values into a container/machine specific settings file.
forwardPorts array A list of ports that should be forwarded from inside the container to the local machine.
portsAttributes object Object that maps a port number, "host:port" value, range, or regular expression to a set of default options. See port attributes for available options. For example:
"portsAttributes": {"3000": {"label": "Application port"}}
otherPortsAttributes object Default options for ports, port ranges, and hosts that aren't configured using portsAttributes. See port attributes for available options. For example:
"otherPortsAttributes": {"onAutoForward": "silent"}
remoteEnv object A set of name-value pairs that sets or overrides environment variables for VS Code (or sub-processes like terminals) but not the container as a whole. Environment and pre-defined variables may be referenced in the values.
For example: "remoteEnv": { "PATH": "${containerEnv:PATH}:/some/other/path" }
remoteUser string Overrides the user that VS Code runs as in the container (along with sub-processes like terminals, tasks, or debugging). Defaults to the user the container as a whole is running as (often root).
userEnvProbe enum Indicates the type of shell to use to "probe" for user environment variables to include in VS Code or other connected tool's processes: none, interactiveShell, loginShell, or loginInteractiveShell (default). The specific shell used is based on the default shell for the user (typically bash). For example, bash interactive shells will typically include variables set in /etc/bash.bashrc and ~/.bashrc while login shells usually include variables from /etc/profile and ~/.profile. Setting this property to loginInteractiveShell will get variables from all four files.
postAttachCommand string,
array
A command string or list of command arguments to run after VS Code attaches to the container. Use && in a string to execute multiple commands. For example, "yarn install" or "apt-get update && apt-get install -y curl". The array syntax ["yarn", "install"] will invoke the command (in this case yarn) directly without using a shell. Not set by default.
Note that the array syntax will execute the command without a shell. You can learn more about formatting string vs array properties.

附加容器配置文件中的变量

变量可以在附加配置文件的某些字符串值中以以下格式引用:${variableName}。下表是您可以使用的可用变量列表。

Variable Properties Description
${containerEnv:VAR_NAME} remoteEnv Value of an existing environment variable inside the container (in this case, VAR_NAME) once it is up and running. For example: "remoteEnv": { "PATH": "${containerEnv:PATH}:/some/other/path" }

附加到Kubernetes集群中的容器

要附加到Kubernetes集群中的容器,请从命令面板中选择Dev Containers: Attach to Running Kubernetes Container...⇧⌘P (Windows, Linux Ctrl+Shift+P))。或者,首先安装Kubernetes扩展kubectl以及Dev Containers扩展。然后从活动栏中选择Kubernetes资源管理器,并展开集群和包含您要附加的容器的Pod。最后,右键单击容器并从上下文菜单中选择Attach Visual Studio Code

注意: Kubernetes 集群中的容器尚不支持附加的容器配置文件。

附加到Kubernetes容器

下一步