vscode can only use one service as "the workspace" where the IDE runs. Just like when working locally you are on the IDE and the other services run in other containers.

None of your current services seem "good" for being the IDE workspace, so you would have to add that one. That would be "just like" your host machine, but in the container.

You can use multiple compose files so you can avoid changing your current docker-compose.yml while being able to add your new service.

So, part I:

  1. Create a second docker-compose.yml file (maybe: docker-compose.workspace.yml)
  2. Add one single service to that file, maybe call it "workspace". vscode Remote part will run there. What image? You may use one vscode's precooked ones.
  3. On .devcontainer.json point to both files and define the workspace service:
...
  "dockerComposeFile": [
    "docker-compose.yaml",
    "docker-compose.workspace.yaml"
  ],
  "service": "workspace",
...

Ok. So that gives you the workspace container and everything else on the side. That gets us to the other part of the question:

Is it possible to interact with Docker engine on the host from inside the container? So I can simply have a dev.sh script that can simply up the rabbitmq and influxdb dependencies and then launch whatever microservice I want to run?

First, if you want the docker & docker-compose commands you have to install the packages. Some images have them builtin, others not. You may use your own image, etc.

But that is not enough. You workspace container is ignorant of the host's docker. But it is easy enough to fix. Just add a volume mount:

/var/run/docker.sock:/var/run/docker.sock

On the workspace service. That way vscode will "see" your host's docker and operate with it.

Beware that it is still the host's docker, so you may get into trouble with paths, etc depending on what you do.

Answer from marc.fargas on Stack Overflow
๐ŸŒ
Visual Studio Code
code.visualstudio.com โ€บ docs โ€บ devcontainers โ€บ create-dev-container
Create a Dev Container
November 3, 2021 - Create a devcontainer.json, which describes how VS Code should start the container and what to do after it connects. Make and persist changes to the dev container, such as installation of new software, through use of a Dockerfile.
๐ŸŒ
Visual Studio Code
code.visualstudio.com โ€บ docs โ€บ devcontainers โ€บ containers
Developing inside a Container
November 3, 2021 - This makes the image self-contained since these settings are automatically picked up when the image is referenced - whether directly, in a FROM in a referenced Dockerfile, or in a Docker Compose file. This helps prevent your Dev Container config and image contents from getting out of sync, and allows you to push updates of the same configuration to multiple repositories through a simple image reference. This metadata label is automatically added when you pre-build using the Dev Container CLI (or other specification supporting utilities like the GitHub Action or Azure DevOps task) and includes settings from devcontainer.json and any referenced Dev Container Features.
Top answer
1 of 3
26

vscode can only use one service as "the workspace" where the IDE runs. Just like when working locally you are on the IDE and the other services run in other containers.

None of your current services seem "good" for being the IDE workspace, so you would have to add that one. That would be "just like" your host machine, but in the container.

You can use multiple compose files so you can avoid changing your current docker-compose.yml while being able to add your new service.

So, part I:

  1. Create a second docker-compose.yml file (maybe: docker-compose.workspace.yml)
  2. Add one single service to that file, maybe call it "workspace". vscode Remote part will run there. What image? You may use one vscode's precooked ones.
  3. On .devcontainer.json point to both files and define the workspace service:
...
  "dockerComposeFile": [
    "docker-compose.yaml",
    "docker-compose.workspace.yaml"
  ],
  "service": "workspace",
...

Ok. So that gives you the workspace container and everything else on the side. That gets us to the other part of the question:

Is it possible to interact with Docker engine on the host from inside the container? So I can simply have a dev.sh script that can simply up the rabbitmq and influxdb dependencies and then launch whatever microservice I want to run?

First, if you want the docker & docker-compose commands you have to install the packages. Some images have them builtin, others not. You may use your own image, etc.

But that is not enough. You workspace container is ignorant of the host's docker. But it is easy enough to fix. Just add a volume mount:

/var/run/docker.sock:/var/run/docker.sock

On the workspace service. That way vscode will "see" your host's docker and operate with it.

Beware that it is still the host's docker, so you may get into trouble with paths, etc depending on what you do.

2 of 3
9

You would need to add Docker as a feature to your devcontainer.json:

"features": { "ghcr.io/devcontainers/features/docker-outside-of-docker:1": {} }

This will allow access to the docker socket on the host.

Working example: devcontainer.json

๐ŸŒ
GitHub
github.com โ€บ microsoft โ€บ vscode-dev-containers โ€บ blob โ€บ main โ€บ container-templates โ€บ docker-compose โ€บ .devcontainer โ€บ docker-compose.yml
vscode-dev-containers/container-templates/docker-compose/.devcontainer/docker-compose.yml at main ยท microsoft/vscode-dev-containers
# This is where VS Code should expect to find your project's source code and the value of "workspaceFolder" in .devcontainer/devcontainer.json ยท - ..:/workspace:cached ยท ยท # Uncomment the next line to use Docker from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker-compose for details.
Author ย  microsoft
๐ŸŒ
Visual Studio Code
code.visualstudio.com โ€บ docs โ€บ devcontainers โ€บ tutorial
Dev Containers tutorial
November 3, 2021 - The Dev Containers extension uses the files in the .devcontainer folder, namely devcontainer.json, and an optional Dockerfile or docker-compose.yml, to create your dev containers.
๐ŸŒ
Flaviodelgrosso
flaviodelgrosso.com โ€บ blog โ€บ vscode-devcontainers
Mastering VS Code Dev Containers with Docker
February 4, 2024 - Create docker-compose.yaml: Craft a docker-compose.yaml file to define services. For instance, pairing the dev container with a PostgreSQL database: ... version: '3' services: dev-container: image: your-custom-image volumes: - ./workspace:/workspace networks: - db-network command: sleep infinity db: image: postgres networks: - db-network environment: POSTGRES_PASSWORD: yourpassword networks: db-network: Update devcontainer.json: Modify devcontainer.json to reference the Docker Compose file:
๐ŸŒ
Visual Studio Code
code.visualstudio.com โ€บ docs โ€บ containers โ€บ docker-compose
Use Docker Compose
November 3, 2021 - Develop a multi-container app running in containers using Docker Compose and Visual Studio Code.
Find elsewhere
๐ŸŒ
Visual Studio Code
code.visualstudio.com โ€บ remote โ€บ advancedcontainers โ€บ connect-multiple-containers
Connect to multiple containers
November 3, 2021 - If you want to extend your Docker Compose file for development, you should use a single docker-compose.yml that extends both services (as needed) and is referenced in both devcontainer.json files.
๐ŸŒ
Top Tech Tips
toptechtips.github.io โ€บ 2023-05-17-docker-compose-multiple-dev-containers
How to use VS Code Dev Containers with your docker compose deployment for efficient development and deployment
May 17, 2023 - We use this for deploying our stack ... python 2a. It has its own .devcontainer which contain configs for when we want to set up backend as a stand-alone dev/docker container...
๐ŸŒ
Reddit
reddit.com โ€บ r/vscode โ€บ devcontainer workflow vs docker-compose with attach to container?
r/vscode on Reddit: devcontainer workflow vs docker-compose with attach to container?
December 19, 2022 -

I am getting started with devcontainer on a project that has two containers, (1) node app and (2) postgres. I have a docker-compose file working for the setup and now trying devcontainers. When I run the docker-compose file within vscode, I cannot manage it with the docker-compose CLI the way I usually do. I don't seem to have as much control over things. But, I can run the containers using the docker-compose CLI and just use vscode to attach to a running container.

What are the pros/cons to these two setups? (1) devcontainer running docker-compose vs (2) docker-compose and vscode attach to running container?

๐ŸŒ
Atomic Spin
spin.atomicobject.com โ€บ 2021 โ€บ 06 โ€บ 16 โ€บ docker-development-container
Using Docker as a Dev Environment with VS Code: Part 2
July 16, 2021 - Annoyingly, Docker wonโ€™t automatically create external volumes/networks for us through docker-compose. The external designation basically tells the Docker CLI that it doesnโ€™t need to worry about these things. This means you need to create them independently before your containers start up. To do this, I use a bash script to create my volume and network if they donโ€™t already exist. Then, I tell VS Code to run that script via the devcontainer.json file:
๐ŸŒ
Docker Hub
hub.docker.com โ€บ r โ€บ microsoft โ€บ vscode-devcontainers
microsoft/vscode-devcontainers - Docker Image
You may need to choose the From a predefined container configuration definition... option if your project has an existing Dockerfile or Docker Compose file. Answer any questions that appear. See the definition's README for configuration options. A link is available in the .devcontainer/devcontainer.json file added to your folder.
๐ŸŒ
DEV Community
dev.to โ€บ jannisdev โ€บ create-a-devcontainer-vscode-4pkh
Create a Devcontainer (VSCode) - DEV Community
January 14, 2024 - โ“ What's a devcontainer ๐Ÿ— Create your own devcontainer ๐Ÿ”— Devcontainer with docker-compose ... Tagged with docker, node, vscode, tutorial.
๐ŸŒ
GitHub
github.com โ€บ microsoft โ€บ vscode-dev-containers
GitHub - microsoft/vscode-dev-containers: NOTE: Most of the contents of this repository have been migrated to the new devcontainers GitHub org (https://github.com/devcontainers). See https://github.com/devcontainers/template-starter and https://github.com/devcontainers/feature-starter for information on creating your own! ยท GitHub
November 30, 2023 - At its simplest, all you need is a .devcontainer/devcontainer.json file in your project that references an image, Dockerfile, or docker-compose.yml, and a few properties.
Starred by 4.7K users
Forked by 1.4K users
Languages ย  Shell 78.1% | Dockerfile 8.2% | JavaScript 7.0% | Jupyter Notebook 4.4% | Python 0.9% | C# 0.4%
Top answer
1 of 1
8

Full working example (combined from answers mentioned in question and other SO):

  • Linux as host
  • Go as example language
  • zsh, oh-my-zsh, .zsh_history from host Linux

.devcontainer/devcontainer.json:

// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.217.4/containers/go
{
    "name": "Go",
    "service": "workspace",
    "workspaceFolder": "/home/vscode/woskpaces/go-example/",
    "dockerComposeFile": [
        "docker-compose.yml",
        "docker-compose.workspace.yml"
    ],
    // Set *default* container specific settings.json values on container create.
    "settings": {
        "go.toolsManagement.checkForUpdates": "local",
        "go.useLanguageServer": true,
        "go.gopath": "/go",
        "go.goroot": "/usr/local/go"
    },
    // Add the IDs of extensions you want installed when the container is created.
    "extensions": [
        "golang.go"
    ],
    // Use 'forwardPorts' to make a list of ports inside the container available locally.
    // "forwardPorts": [],
    // Use 'postCreateCommand' to run commands after the container is created.
    // "postCreateCommand": "go version",
    // Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
    "remoteUser": "vscode"
}

.devcontainer/docker-compose.workspace.yml:

version: '3'
networks:
  myNetwork:
    name: myNetwork
services:
  workspace:
    build:
      context: ./
      dockerfile: Dockerfile
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ..:/home/vscode/woskpaces/go-example/
      - ~/.zshrc:/home/vscode/.zshrc
      - ~/.oh-my-zsh/:/home/vscode/.oh-my-zsh/
      - ~/.zsh_history:/home/vscode/.zsh_history
    depends_on:
      - kafka
    tty: true           # <- keeps container running
    networks:
      - myNetwork

.devcontainer/docker-compose.yml:

version: '3'
networks:
  myNetwork:
    name: myNetwork
services:
  zookeeper:
    image: confluentinc/cp-zookeeper:latest
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    ports:
      - 22181:2181
    networks:
      - myNetwork
    tmpfs: "/datalog"
  
  kafka:
    image: confluentinc/cp-kafka:latest
    depends_on:
      - zookeeper
    ports:
      - 29092:29092
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:29092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
    networks:
      - myNetwork
    depends_on:
      - zookeeper

.devcontainer/Dockerfile:

# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.209.6/containers/go/.devcontainer/base.Dockerfile

# [Choice] Go version (use -bullseye variants on local arm64/Apple Silicon): 1, 1.16, 1.17, 1-bullseye, 1.16-bullseye, 1.17-bullseye, 1-buster, 1.16-buster, 1.17-buster
ARG VARIANT="1.17-bullseye"
FROM mcr.microsoft.com/vscode/devcontainers/go:0-${VARIANT}

# [Choice] Node.js version: none, lts/*, 16, 14, 12, 10
ARG NODE_VERSION="none"
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi

# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
#     && apt-get -y install --no-install-recommends <your-package-list-here>

# [Optional] Uncomment the next lines to use go get to install anything else you need
# USER vscode
# RUN go get -x <your-dependency-or-tool>

# [Optional] Uncomment this line to install global node packages.
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1
๐ŸŒ
Development Containers
containers.dev โ€บ implementors โ€บ json_reference
Dev Container metadata reference
Instead, container orchestrator formats can be referenced when needed to manage multiple containers and their lifecycles. Today, devcontainer.json includes scenario specific properties for working without a container orchestrator (by directly referencing an image or Dockerfile) and for using Docker Compose as a simple multi-container orchestrator.
๐ŸŒ
Visual Studio Code
code.visualstudio.com โ€บ remote โ€บ advancedcontainers โ€บ overview
Advanced container configuration
November 3, 2021 - The Visual Studio Code Dev Containers extension lets you use a Docker container as a full-featured development environment. It allows you to open any folder inside (or mounted into) a container and take advantage of Visual Studio Code's full ...
๐ŸŒ
BrickCrafted
jacksontong.github.io โ€บ 2024 โ€บ 12 โ€บ 31 โ€บ setting-up-a-vscode-devcontainer-using-docker-compose.html
Setting Up a VSCode Devcontainer Using Docker Compose | BrickCrafted
December 31, 2024 - dockerComposeFile: Links to your docker-compose.yaml file. service: Specifies the primary container (the Go development container). workspaceFolder: Sets the folder inside the container to map your project files. customizations: Installs VSCode extensions for Go development and configures the ...
๐ŸŒ
LogRocket
blog.logrocket.com โ€บ home โ€บ using dev containers with vs code for an easier dev setup
Using dev containers with VS Code for an easier dev setup - LogRocket Blog
June 4, 2024 - Finally, we will create a directory named app-frontend in the .devcontainer directory and create docker-compose.yaml and devcontainer.json in the new app-frontend directory.