Skip to content

Instantly share code, notes, and snippets.

@utamori
Last active July 22, 2020 10:13
Show Gist options
  • Select an option

  • Save utamori/2797250a91234f1c9350429f19140189 to your computer and use it in GitHub Desktop.

Select an option

Save utamori/2797250a91234f1c9350429f19140189 to your computer and use it in GitHub Desktop.
Order 9
Area remote
TOCTitle Attach to Container
PageTitle Attach to a running container using Visual Studio Code Remote Development
ContentId ed14ef07-f44c-4675-b95b-cb5faffc7abb
MetaDescription Attach to a running container using Visual Studio Code Remote Development
DateApproved 7/9/2020

実行中のコンテナーに接続する

Visual Studio Codeではコンテナを作成して起動することができますが、ワークフローに合わない場合がありますので、すでに起動しているDockerコンテナにVS Codeを「アタッチ」することをお勧めします。一度アタッチすると、devcontainer.jsonを使ってコンテナ内のフォルダを開いたときのように、拡張機能のインストール、編集、デバッグができます。

Dockerコンテナにアタッチする

Dockerコンテナーに接続するには、コマンドパレット(F1)から[ リモートコンテナー:実行中のコンテナーに接続...]を選択するか、アクティビティバーのリモートエクスプローラーを使用して、コンテナービューから[ コンテナーにインラインで接続]アクションを選択します。接続するコンテナ。

Containers Explorer screenshot

Note: Alpine Linuxコンテナを使用している場合、拡張機能の中にあるネイティブコードの glibc 依存により、一部の拡張機能が動作しない場合があります。

コンテナアタッチの設定ファイル

VS Code supports image or container name-level configuration files to speed up setup when you repeatedly connect to a given Docker container. Once attached, anytime you open a folder, install an extension, or forward a port, a local image-specific configuration file will automatically be updated to remember your settings so that when you attach again, everything is back to the right place.

  • By default, an image-level configuration is used. To view or update it after attaching, select Remote-Containers: Open Container Configuration from the Command Palette (kbstyle(F1)).

  • If you would prefer to tie your configuration to a container name, select Remote-Containers: Open Named Configuration File from the Command Palette (kbstyle(F1)) after attaching. Any updates from this point forward will apply to this name-level configuration rather than at the image level.

Both of these files support a subset of devcontainer.json properties:

{
    // 新しいコンテナにアタッチするときに開くデフォルトのパス。
    "workspaceFolder": "/path/to/code/in/container/here",

    // コンテナに最初にアタッチしたときにコンテナ内にインストールする拡張子を指定する拡張子 ID の配列。
    "extensions": [
        "dbaeumer.vscode-eslint"
    ],

    // *デフォルト*のコンテナ固有のVSコード設定
    "settings": {
        "terminal.integrated.shell.linux": "/bin/bash"
    },

    // ポートフォワード番号の配列
    "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" }
}

See the attached container config reference for a complete list of properties and their uses.

Once saved, whenever you open a container for the first time with the same image / container name, these properties will be used to configure the environment.

Tip: If something is wrong with your configuration, you can also edit it when not attached to the container by selecting Remote-Containers: Open Attached Container Configuration File... from the Command Palette (kbstyle(F1)) and then picking the image / container name from the presented list.

Finally, if you have extensions you want installed regardless of the container you attach to, you can update settings.json to specify a list of extensions that should always be installed.

Attach to a container in a Kubernetes cluster

To attach to a container in a Kubernetes cluster, first install the Kubernetes extension and kubectl along with the Remote - Containers extension. Then select the Kubernetes explorer from the Activity bar and expand the cluster and Pod where the container you want to attach to resides. Finally, right-click on the container and select Attach Visual Studio Code from context menu.

Note: Attached container configuration files are not yet supported for containers in a Kubernetes cluster.

Attach to Kubernetes Container

Next steps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment