LoginSignup
1
0

More than 1 year has passed since last update.

コマンドラインからDockerのコンテナをvscodeで直接開く

Last updated at Posted at 2021-02-28

containerを直接vscodeで開く

ちょっと感動したので記載。

起動したdocker containerをvscodeで開く場合、コマンドパレットやサイドバーから開くことが一般的だと思いますが、container名さえわかれば、下記コマンドでコマンドラインから直接開くことができます。

code --folder-uri=vscode-remote://attached-container+{container_hex}{path_to_open_folder}

上記のcontainer_hexの計算方法は以下(例としてbashです。)

text="{\"containerName\":\"/${1}\"}"
container_hex=$(printf $1 | od -A n -t x1 | tr -d '[\n\t ]')

${1}には起動したいコンテナの名前を指定してください。

これでdocker containerをvscodeで直接開くことができます。
開発環境にscript化しておいておけば手間が省けるかもしれません。

例として、powershellのスクリプトです。

$container_name = $args[0]
$containerinfo = "{`"containerName`":`"${container_name}`"}"
$container_hex = ""
foreach ($i in $containerinfo.toCharArray()) {
    $hex = "{0:x1}" -f [Int][Char]$i
    $container_hex += $hex.ToString()
}
$folderuri="vscode-remote://attached-container+${container_hex}/root/workspace"
code --folder-uri=$folderuri

参考

script化してくれてる方がいらっしゃいます。
https://github.com/geircode/vscode-attach-to-container-script

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0