Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create one dev container in VS Code and use it for multiple projects?

I find myself working on a multitude of projects in VS Code, each with separate codebases in their own repositories, for which I want to use similar / identical Dev Containers.

For example, several Python + Ansible projects where I want a Python 3 Dev Container with a few common Python packages and Ansible.

It seems wasteful / redundant to keep redefining basically identical .devcontainer files in each of these projects, and then have multiple almost-identical Docker containers littering my host machine.

How can I define and maintain a single Dev Container that each of these projects can reference and use?

like image 538
Dan J Avatar asked Jun 28 '26 08:06

Dan J


1 Answers

I found the relevant documentation: Configure separate containers for multiple projects

To summarize (all quotes from the linked docs):

  1. Create a top-level folder beneath which your project folders will live.

Imagine you had your source code across multiple repositories in a single folder for a given toolset.
Let's set up a container for use with all of the Python projects in the ./Repos/python folder.
Start Visual Studio Code, select Dev Containers: Open Folder in Container... from the Command Palette (F1) or quick actions Status bar item, and select the ./Repos/python folder.

  1. Add Dev Container config files to the top-level folder (I repurposed existing files from my projects, or you can use VS Code's tooling to create them).

Now pick a starting point for your dev container. You can either select a base dev container definition from a filterable list, or use an existing Dockerfile or Docker Compose file if one exists in the folder you selected.
After picking the starting point for your container, VS Code will add the dev container configuration files to the ./Repos/python/.devcontainer folder.

  1. Build and connect to the container. If you used VS Code's tooling to add the config files, it should build and connect automatically. If you added your own files, you might need to use the Reopen folder in container action.

  2. Now, open one of your projects inside the container. It'll be in a subfolder of the container's workspace path, e.g. /workspaces/python/my-cool-project.

Once connected use File > Open... / Open Folder... to select one of the folders under ./Repos/python.
In a moment, VS Code will open the folder inside the same container. In the future, you can use the Remote Explorer in the Activity Bar to open this sub-folder in the container directly.

  1. Note that bolded sentence (emphasis mine): you'll need to open each of your project sub-folders in the container once so that they show up underneath the container in Remote Explorer. After that, you can use Remote Explorer (or VS Code's recent folders list) to open your desired project in the common Dev Container directly!
like image 115
Dan J Avatar answered Jun 30 '26 07:06

Dan J



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!