Devcontainers are a great way of simplifying the management of your local development environment. If you haven’t heard of devcontainers I’d recommend looking up what they are first in whatever format you prefer (blog post, docs or video) as this post is going to assume you've already got a devcontainer set up and dive straight into detail.
It’s great to not have to manage which version of Go you need for which project, or making sure that you never have conflicting Rubygem versions ever again. But many projects have dependencies other than just those required to compile the software. Many microservices for example will need to talk to other services in order to run end-to-end tests. Maybe you need a database running to ensure transactions work as expected, or maybe you’re working on a central controller and you need a few workers running different versions to ensure compatibility. Chances are that if your service is containerised, so are these dependencies. So we need a way to run a devcontainer environment with as many containers as we want, not just the one.
Fortunately we can achieve this with docker compose. Firstly, create a compose file at .devcontainer/docker-compose.yml like so:
In order to tell devcontainers to use this new docker-compose.yml file, we need to modify our devcontainer.json and replace the “build” setting with docker compose specific properties:
Here is an example:
Once this is complete all we need to do is reopen the environment in a container by pressing ⇧⌘P / Ctrl+Shift+P to open the command palette, then select “Remote-Containers: Reopen in Container”.
This will not only create the devcontainer, but also start all other required services from the docker-compose.yml file. You can prove this is working by trying to access once of the containers from the terminal: