Last active
August 5, 2025 16:50
-
-
Save Denys-Bushulyak/fc032f349a24352ff8e4be4a79ba0827 to your computer and use it in GitHub Desktop.
Create devcontainer shell script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
REPOSITORY_URL="$1" | |
if [ -z "$REPOSITORY_URL" ]; then | |
echo "Error: Repository URL is required" | |
echo "Usage: $0 <repository_url>" | |
exit 1 | |
fi | |
PROJECT_NAME=$(basename "$REPOSITORY_URL" .git | cut -d'/' -f2) | |
# If argument -d thet drop container and volumes | |
if [ "$2" == "-d" ]; then | |
docker stop $PROJECT_NAME-dev | |
docker rm $PROJECT_NAME-dev | |
docker volume rm $PROJECT_NAME | |
fi | |
# Create a named volume | |
if ! docker volume ls -q | grep -q "^$PROJECT_NAME$"; then | |
docker volume create $PROJECT_NAME | |
fi | |
# Run a container with git to clone the repository | |
docker run --rm \ | |
-v $PROJECT_NAME:/workspace \ | |
-v $HOME/.ssh:/root/.ssh \ | |
alpine/git \ | |
clone "$REPOSITORY_URL" /workspace | |
# Run nodejs container and mount the docker socket | |
docker run --rm \ | |
--name $PROJECT_NAME-builder \ | |
-v $PROJECT_NAME:/workspace \ | |
-v /var/run/docker.sock:/var/run/docker.sock \ | |
docker \ | |
sh -c "apk update && \ | |
apk add nodejs npm && \ | |
npm install -g @devcontainers/cli && \ | |
devcontainer build --workspace-folder /workspace --image-name $PROJECT_NAME-devcontainer" | |
# Generate ssh key with "devContainer" name | |
if [ ! -f ~/.ssh/id_rsa_devcontainer ]; then | |
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa_devcontainer -N "" -C "devContainer" | |
# Update SSH config for devcontainer | |
SSH_CONFIG="$HOME/.ssh/config" | |
CONFIG_BLOCK="# Devcontainer Start | |
Host github-developer | |
HostName localhost:2222 | |
User developer | |
IdentityFile ~/.ssh/id_rsa_devcontainer | |
IdentitiesOnly yes | |
# Devcontainer End" | |
# Check if the config block already exists | |
if ! grep -q "# Devcontainer Start" "$SSH_CONFIG" 2>/dev/null; then | |
echo "Adding devcontainer SSH config to ~/.ssh/config" | |
echo "" >> "$SSH_CONFIG" | |
echo "$CONFIG_BLOCK" >> "$SSH_CONFIG" | |
else | |
echo "Devcontainer SSH config already exists in ~/.ssh/config" | |
fi | |
fi | |
# Run devcontainer container and mount the docker socket | |
# Check if container already exists and is running | |
if docker ps -q -f name="$PROJECT_NAME-dev" | grep -q .; then | |
echo "Container $PROJECT_NAME-dev is already running" | |
elif docker ps -aq -f name="$PROJECT_NAME-dev" | grep -q .; then | |
echo "Starting existing container $PROJECT_NAME-dev" | |
docker start $PROJECT_NAME-dev | |
else | |
echo "Creating new container $PROJECT_NAME-dev" | |
docker run -d \ | |
--name $PROJECT_NAME-dev \ | |
-v $PROJECT_NAME:/workspace \ | |
-v /var/run/docker.sock:/var/run/docker.sock \ | |
-v ~/.ssh/id_rsa_devcontainer.pub:/tmp/id_rsa_devcontainer.pub:ro \ | |
-p 2222:22 \ | |
-w /workspace \ | |
-e HOME=/workspace \ | |
$PROJECT_NAME-devcontainer \ | |
sh -c "apt-get update && \ | |
apt-get install -y openssh-server && \ | |
useradd -m -s /bin/bash developer && \ | |
mkdir -p /home/developer/.ssh && \ | |
cat /tmp/id_rsa_devcontainer.pub >> /home/developer/.ssh/authorized_keys && \ | |
chown -R developer:developer /home/developer/.ssh && \ | |
chmod 700 /home/developer/.ssh && \ | |
chmod 600 /home/developer/.ssh/authorized_keys && \ | |
mkdir /var/run/sshd && \ | |
sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin no/' /etc/ssh/sshd_config && \ | |
sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/' /etc/ssh/sshd_config && \ | |
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config && \ | |
/usr/sbin/sshd -D" | |
fi | |
# Wait for SSH service to be ready | |
echo "Waiting for SSH service to be ready..." | |
while ! nc -z localhost 2222; do | |
echo "SSH service not ready yet, waiting 2 seconds..." | |
sleep 2 | |
done | |
echo "SSH service is ready!" | |
# Add SSH fingerprint for localhost:2222 | |
echo "Removing old SSH host key for localhost:2222 to prevent host key verification errors..." | |
ssh-keygen -R "[localhost]:2222" 2>/dev/null || true | |
echo "Adding SSH fingerprint for localhost:2222..." | |
ssh-keyscan -p 2222 localhost >> ~/.ssh/known_hosts 2>/dev/null || true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Devcontainer Start
Host devcontainer
HostName localhost
Port 2222
User developer
IdentityFile ~/.ssh/id_rsa_devcontainer
IdentitiesOnly yes