Skip to content

Instantly share code, notes, and snippets.

@masuidrive
Created June 24, 2025 01:53
Show Gist options
  • Save masuidrive/97c51e6de59d94de7507c6c3629cc320 to your computer and use it in GitHub Desktop.
Save masuidrive/97c51e6de59d94de7507c6c3629cc320 to your computer and use it in GitHub Desktop.
ホストOSからVSCode devcontainerにdiveするコマンド
#!/bin/bash
# Get list of running containers with images containing "vsc-"
containers=$(docker ps --format "{{.ID}}:{{.Image}}:{{.Names}}" | grep "vsc-")
if [ -z "$containers" ]; then
echo "No devcontainer (vsc-*) containers are running."
exit 1
fi
# Display containers with numbers
echo "Available devcontainers:"
echo
i=1
declare -a container_array
while IFS= read -r container; do
container_id=$(echo "$container" | cut -d: -f1)
image=$(echo "$container" | cut -d: -f2)
name=$(echo "$container" | cut -d: -f3)
echo "[$i] $container_id $image"
container_array[$i]=$name
((i++))
done <<< "$containers"
# Get user selection
echo
read -p "Enter the number of the container to connect to: " selection
# Validate selection
if ! [[ "$selection" =~ ^[0-9]+$ ]] || [ "$selection" -lt 1 ] || [ "$selection" -ge "$i" ]; then
echo "Invalid selection."
exit 1
fi
# Connect to selected container
selected_container="${container_array[$selection]}"
echo "Connecting to $selected_container..."
docker exec -it "$selected_container" /bin/bash

devcontainerに入るためのコマンドを作ります。docker psでプロセスリストを取り出して、vsc-で始まるimageのコンテナIDを連番付きで表示します。 その番号をユーザが入力すると、そのコンテナにexec --it /bin/bashするようなシェルスクリプト書いて
選択肢を出すときは [{#}] {CONTAINER ID} {IMAGE}って表示して

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