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?
I found the relevant documentation: Configure separate containers for multiple projects
To summarize (all quotes from the linked docs):
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/pythonfolder.
Start Visual Studio Code, selectDev Containers: Open Folder in Container...from the Command Palette (F1) or quick actions Status bar item, and select the./Repos/pythonfolder.
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/.devcontainerfolder.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With