avatarAdam Fisher / fisher king (@therightstuff)

Summary

The web content describes how to attach Visual Studio Code (VSCode) to a running Docker container for development and debugging purposes, with a focus on the simplicity and benefits of using VSCode's built-in remote development features.

Abstract

The article titled "How To Attach Visual Studio Code To A Running Docker Container" discusses the author's recent experience with running legacy code on a new M1 MacBook, emphasizing the challenges faced with compatibility and the solution found in Docker. The author explains the initial struggles with setting up testing and debugging within a Docker container, and the discovery of VSCode's remote development capabilities introduced in 2019. The article provides a quick guide on setting up a Docker container for development, including volume mounting, and how to connect VSCode to the running container using the Remote - Containers extension. The process is described as straightforward, with the added benefit of ensuring consistency with the CI/CD pipeline. The author also updates on a method to avoid reinstalling VSCode extensions in the container.

Opinions

  • The author expresses enthusiasm about the ease of setting up VSCode to work with Docker containers, describing it as "automagical."
  • There is a note of frustration regarding the compatibility issues with Apple's M1 MacBook and the challenges of running x64 architecture on it.
  • The author has a positive view of Docker's role in the development process, particularly for ensuring consistency across different environments.
  • The author is impressed by the simplicity of the remote development setup in VSCode, which they had not previously known about.
  • There is an acknowledgment of potential issues with volume mounting in git-bash on Windows machines, with a cautionary note provided.
  • The author provides a pragmatic update on how to avoid reinstalling VSCode extensions in the Docker container, indicating a commitment to improving the development workflow.

How To Attach Visual Studio Code To A Running Docker Container

Yet Another Thing I Didn’t Know VSCode Could Do Automagically

These past two weeks I’ve been primarily preoccupied with getting some old code running on a flashy new M1 MacBook. The new MacBooks are awesome, but as we all well know: Apple products are not designed for peasants like me who value such lowly concepts as backwards compatibility…

Docker to the Rescue

We already use Docker for our CI/CD pipeline, so it was naturally the first thing we tried after losing a few days to battling with Python versions that refuse to build on ARM platforms. Once we got everything building, all tests passing and the deployments successful from within the Docker container running on my MacBook — requiring the --platform linux/amd64 arguments, which actually comes with a warning not to expect too much — I experimented with getting the same thing working over UTM, but was unfortunately met with abysmal failures trying to run a virtual machine with x64 architecture.

I haven’t have the opportunity yet to experiment with other solutions, but UTM was the recommended one (according to a number of articles and forum threads) and running on the same Docker image as our CI/CD pipeline has the additional benefit of ensuring that we’re all seeing and feeling the same successes and failures so by that point I was confident that we’d found the right solution.

Testing and Debugging With Visual Studio Code

Visual Studio Code’s Python test explorer is great, but we now needed to make testing and debugging available via the Docker container. My initial search results showed this to be possible, but turned up some very impractical and complicated ways of setting it up. Fortunately, one of the GitHub issue discussions I came across made reference to a 2019 announcement introducing built-in Visual Studio Code support for working in remote containers!

I was stunned (and elated!) by how simple this was to set up, and I haven’t even begun to delve into the documentation for setting my machine up for long-term abuse, but for the rest of you who didn’t know this functionality exists, here’s a quick guide to getting started:

Setting up a running Docker for development

The important thing is to have a running Docker container that’s configured with everything you need, and it’s probably a good idea to use a volume mount so that your work is preserved (of course there are alternative methods, but this is the easiest way).

[Beware, users with git-bash on a Windows machine: volume mounting is weird]

> docker run --rm --name mypython -it --entrypoint bash -v $(pwd):/root/myreponame python

(For the unfamiliar, the command above will start an interactive bash prompt in a named instance of the latest python Docker image with the current directory mounted as /root/myreponame or ~/myreponame once you’re logged in as root)

Connecting Visual Studio Code to the running container

After installing the Remote - Containers extension, on the bottom left corner of Visual Studio Code you will see a tiny green button with arrow indicators in it.

The green icon with no details indicating that you’re running VSCode on your local device

Click the button, and select “Attach to running container…”. Visual Studio Code will then install itself in the container and open a new window, and the tiny green button at the bottom of the new window will show “Container IMAGE_NAME (CONTAINER_NAME)”.

The green icon with the container details indicating that you’re running VSCode on it remotely

Install whatever extensions you require (it’s possible to avoid doing that more than once, but I haven’t tried it yet), and away you go!

**UPDATE**: I’ve just published Avoiding Visual Studio Code extension reinstalls on running Docker containers for those of you who’d like to do that, it turned out the documentation I linked to above doesn’t handle that use-case :)

Visual Studio Code
Docker
Remote Containers
Debugging
Vscode Remote Container
Recommended from ReadMedium