-
-
Save IgnisDa/f4c1ffb3b798686df3193a80cbd5b3eb to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash | |
set -e | |
remove_flag="" | |
if [ "$1" = "true" ]; then | |
remove_flag="--remove-existing-container" | |
fi | |
# Generate certificate | |
path=tmp/devcontainers | |
mkdir -p $path && pushd $path | |
ssh_key=$(cat /dev/urandom | tr -dc '[:alpha:]' | fold -w ${1:-20} | head -n 1) | |
ssh-keygen -q -N '' -t rsa -f $ssh_key | |
popd | |
# Start container | |
devcontainer up $remove_flag \ | |
--mount "type=bind,source=$HOME/.config/helix,target=/home/archlinux/.config/helix" \ | |
--mount "type=bind,source=$HOME/.wakatime.cfg,target=/home/archlinux/.wakatime.cfg" \ | |
--workspace-folder . | |
script=" | |
# Copy generated keys | |
mkdir -p \$HOME/.ssh | |
cat \$PWD/$path/$ssh_key.pub > \$HOME/.ssh/authorized_keys | |
chmod 644 \$HOME/.ssh/authorized_keys | |
chmod 700 \$HOME/.ssh | |
" | |
# Add pub key to SSH allow list | |
devcontainer exec --workspace-folder . sh -c "$script" | |
devcontainer exec --workspace-folder . sudo /usr/local/share/ssh-init.sh | |
name=$(devcontainer read-configuration --workspace-folder . | jq -r '.configuration.name') | |
container_name="${name}_devcontainer-app-1" # you might also need to change this if `devcontainer-cli` creates a differently named container | |
ip_addr=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $container_name) | |
PORT=${PORT:-2222} | |
# Create a `known_hosts` file that will be used by only this project | |
ssh-keyscan -t ssh-rsa -p $PORT $ip_addr >> tmp/known_hosts | |
# Connect directly via IP address instead of localhost | |
ssh -t -i $PWD/$path/$ssh_key \ | |
-o UserKnownHostsFile=tmp/known_hosts \ | |
-o NoHostAuthenticationForLocalhost=yes \ | |
-o UserKnownHostsFile=/dev/null \ | |
-o GlobalKnownHostsFile=/dev/null \ | |
-p $PORT "archlinux@$ip_addr" \ | |
"cd /workspaces/${PWD##*/}; fish --login" # you might need to change this according to the shell inside the image |
Just cd
into the project with has the .devcontainer
folder in it. Then you can run this script (I have it saved as ~/.local/bin/devcontainers.sh
. After this, you will be dropped into the shell of the container. There you can run hx .
and continue editing as you would in helix.
This is interesting. So, from an existing project to get helix inside a devcontainer running, I have to do the followig:
- Create a .devcontainer folder with at least a devcontainer.json inside
- Run the script above
Did you work with devcontainer in vscode too? How is this different from this approach?
Thanks!
The basic idea is that you should have a running ssh daemon inside your devcontainer to connect to. The links I have provided above lead to my specific devcontainer configuration that do that. Of course you're free to devise your own configuration.
Yes i have worked extensively with vscode devcontainers. Devcontainers in vscode work on almost the same premise, but instead of connecting to an ssh daemon, they connect to a vscode-server (which gets installed once the devcontainer is created the first time) and work via a client server model (AFAIK).
Am I understanding this correctly that I'd be using helix
from the container instead of locally?
If so, how's responsiveness (and a bit about your network performance metrics)?
It's been a long time since I last used this, but i don't remember having any problems about responsiveness. The connection is made over localhost and ssh is quite fast :)
So via your solution, run my container and just connect via ssh into it?
How should I use your script correctly?