Developing in a remote Docker container
Let’s say you have some reason for not wanting to install Docker Desktop on your development machine. Maybe your machine is slow, or the license agreement scares you. By using Visual Studio Code, we can write and develop in a Docker conatiner on a remote machine! Below are the steps I used to setup my development environment for this capability.
If you’re on MacOS, you’re in luck, as that is what I’m going to cover specifically.
A quick note
Before you start connecting to the remote server via VS code, make sure your current user has permission to access Docker on the remote machine. If you run into issues here, you may need to find documentation on how to add a user to the docker
group for your linux distribution. For Ubuntu, use sudo usermod -aG docker $USER
. This will avoid issues with VS codes SSH cache not recognizing the docker permissions.
Pick your poison
Visual Studio Code currently gives us four options to develop in a container on a remote host(reference docs):
As you can see, all but the first option require a local docker client, which means the first option is our only option.
SSH setup
Lets start by first setting up SSH(reference docs).
Make sure your remote machine is accessible via SSH. I’m using Ubuntu in a virtual machine running OpenSSH server.
Key-based authentication
While you can use basic authentication with username and password(I did for years), once you convert to using SSH key pairs you will realize how much time you wasted by skipping this step.
First check for a valid local SSH key:
cat ~/.ssh/id_ed25519.pub
If you see something following this pattern you have a key already and can skip generating a key:
ssh-ed25519 randomverylongstring [email protected]
If you see something following this pattern you need to generate a key:
cat: /Users/YOUR_USERNAME/.ssh/id_ed25519.pub: No such file or directory
in which case you need to generate a key using:
ssh-keygen -t ed25519 -C "[email protected]"
Then add your key to the host. Replace [email protected] with your actual username and IP address:
ssh-copy-id -i ~/.ssh/id_ed25519.pub [email protected]
Test connection
Open the VSCode command palette and input Remote-SSH: Connect to Host
. You can test your connection first and then I would recommend using Add New SSH Host
to make life a little easier. Then use the default SSH config.
We’re in
We have hacked the mainframe. Now that we’re in the remote system, you can go about your business as normal.
Contain yourself
It’s time to start a docker container and get attached(reference docs). Hopefully at this point you have a Docker container running, otherwise this article is not very useful.
Open the command palette(cmd+shift+p), and input Dev Containers: Attach to Running Container
.
I hope you can contain your excitement.