Remote Development in Docker Containers on a Virtual Machine Using Visual Studio Code
The modern development ecosystem is evolving at an astonishing rate. One of the most significant shifts is the move towards containerized applications using platforms like Docker. Simultaneously, the push for cloud computing has led many developers to rely on virtual machines (VMs) as their primary development environment.
But how do you effectively develop inside a Docker container that’s housed on a VM? And, more specifically, how do you do this using a popular code editor like Visual Studio Code (VS Code)? In this article, we will explore a step-by-step approach to achieve this feat.
Setting the Stage: The Tools
- VS Code: Microsoft’s lightweight but powerful code editor. It comes with a plethora of extensions that can enhance its capabilities.
- Remote Development: An extension pack that lets you open any folder in a container, on a remote machine, or in WSL and take advantage of VS Code’s full feature set.
Step-by-Step: Remote Development in Docker Containers on a VM
To work inside a Docker container that’s running on a different Ubuntu VM using VS Code, Here’s a step-by-step guide:
Set Up the Remote — SSH Extension:
Install the “Remote — SSH” extension in VS Code.
Make sure you have SSH access to the Ubuntu VM where Docker is running. If you don’t, you will need to set that up first.
SSH Into the Ubuntu VM:
Click on the green bottom-left corner icon in VS Code.
Choose Remote-SSH: Connect to Host….
Enter the SSH details for your Ubuntu VM and connect.
Attach to the Docker Container:
Now that you’re in a VS Code session inside the Ubuntu VM, you can treat it like you’re local on that VM. Click on the green bottom-left corner icon again.
Choose Remote-Containers: Attach to Running Container….
and select the Docker container you wish to connect to.
Work Inside the Docker Container:
Your VS Code window should now be attached to the Docker container, and you can work inside it as if you were local.
Closing Thoughts
This dual-hop approach — first tunneling into a VM and subsequently into a Docker container — underscores the flexibility and prowess of VS Code as a development tool. Whether you’re navigating intricate cloud architectures or striving for consistency in development environments, this method ensures that you can work seamlessly, regardless of where your containers reside.