notes.dt.in.th

Sometimes I am working with some organizations that doesn’t pay for GitHub Codespaces yet. However, I’m already spoiled; I want to have a cloud-based development environment.

I want to use a cloud-based Linux virtual machine and I want its environment to closely match the one I use on GitHub Codespaces. Fortunately, Microsoft provides the exact image used by GitHub Codespace. Time to set it up.

Install Docker

curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

Add swap space for some extra RAM

sudo fallocate -l 12G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Launch a Docker container

# docker-compose.yml
services:
  dev:
    image: mcr.microsoft.com/vscode/devcontainers/universal:1-linux
    ports:
      - 127.0.0.1:10022:2222
    volumes:
      - ./docker:/var/lib/docker
      - ./workspace:/workspace
    cap_add:
      - SYS_PTRACE
    security_opt:
      - seccomp=unconfined
    privileged: true
    init: true
    tmpfs: /tmp
docker compose up -d

This will launch a Docker container that runs the same image as GitHub Codespace.

This container has already been configured with an SSH server, however, to access it I had to inject my SSH key into /home/codespace/.ssh/authorized_keys inside the container.

Connect to it from VS Code

My VM is configured as mybox in ~/.ssh/config file, so I added another entry to connect to the container:

Host mybox-codebox
  Hostname 127.0.0.1
  Port 10022
  HostKeyAlias mybox-codebox
  User codespace
  ProxyJump mybox

Now I can use VS Code to connect to the container via SSH. VS Code will also forward the authentication credentials into the container. This lets me git clone repositories over HTTPS right away.