Start multiple containers in VS Code's Remote Container to switch between tasks

Introduction

VSCode's Remote Container is useful, isn't it? It is best to be able to try various languages without polluting the environment of the local PC.

However, even though I just want to try it quickly, it is troublesome to set it for each language.

In this post, I'll show you how to connect to multiple containers with just one launch.

It is also written in the official document, so if you want to know more, click here. https://code.visualstudio.com/docs/remote/containers-advanced#_connecting-to-multiple-containers-at-once

Demo code overview

Here, we will deal with golang and python containers. . ├── golang │ ├── .devcontainer.json │ └── Dockerfile ├── python │ ├── .devcontainer.json │ └── Dockerfile └── docker-compose.yml

Configuration file details

  1. docker-compose.yml The parent of the code managed by remote-container is described in docker-compose.yml.

All you have to do is specify the directory in build.context and write the relative path from there to the Dockerfile in the dockerfile.

If you don't like the directory structure, you are free to change it.

docker-compose.yml


version: "3"
services:
  golang-container:
    build:
      context: golang 
      dockerfile: Dockerfile
    volumes:
      - .:/workspace:cached
    environment:
      TZ: "Asia/Tokyo"
    command: sleep infinity

  python-container:
    build:
      context: python 
      dockerfile: Dockerfile
    volumes:
      - .:/workspace:cached
    environment:
      TZ: "Asia/Tokyo"
    command: sleep infinity
  1. .devcontainer.json The settings for each container are described in .devcontainer.json.

To connect the parent-child relationship, the names of dockerComposeFile and service are the same as those defined in docker-compose.yml.

By specifying directories separated by language with workspaceFolder, you can prevent other languages from being displayed in the VS Code window. (It is noisy because a warning appears when it is displayed)

There are various extensions, so you can customize them as you like.

json:.devcontainer.json


{
    "name": "Dev Container Golang Env",
    "dockerComposeFile": "../docker-compose.yml",
    "service": "golang-container",
    "workspaceFolder": "/workspace/golang",
    "extensions": [
      "golang.go",
      "donjayamanne.githistory",
      "eamodio.gitlens",
      "codezombiech.gitignore",
      "mhutchie.git-graph",
      "esbenp.prettier-vscode",
      "coenraads.bracket-pair-colorizer",
      "ionutvmi.path-autocomplete"
    ],
    "terminal.integrated.shellArgs.linux": [
        "-l"
    ],
    "shutdownAction": "stopCompose"
}
  1. Dockerfile Describe the settings you want to use for each container.

Since it is applied in the same way as a normal Dockerfile, it is convenient to install the library as well.

Here, we have installed the git completion tool that everyone loves.

Dockerfile


FROM golang:buster

WORKDIR /

RUN apt-get update \
 && apt-get install -y --no-install-recommends \
      git \
      openssh-client \
      procps \
      ca-certificates \
      curl \
      unzip \
      gnupg \
      vim \
      wget \
 && rm -rf /var/lib/apt/lists/*

# git
RUN wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -O ~/.git-completion.bash
RUN chmod a+x ~/.git-completion.bash
RUN echo "source ~/.git-completion.bash" >> ~/.bashrc
RUN wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh -O ~/.git-prompt.sh
RUN chmod a+x ~/.git-prompt.sh
RUN echo "source ~/.git-prompt.sh" >> ~/.bashrc

How to start multiple containers

  1. Open the VS Code command palette and select Remote-Containers: Open Folder in Container ...
  2. Select the directory you want to open (this time golang or python directory)

If you operate as described above by starting Remote Container normally, it will create not only the selected container but also other containers listed in the settings.

To switch, just select the directory you want to open, as in steps 1 and 2. The container is up at the first startup, so you can switch windows and operate immediately.

If you want a separate window, you can create a new window in VS Code and follow steps 1 and 2 from there.

Summary

I showed you how to launch multiple containers in VS Code's Remote Container. There were two containers here, golang and python, but you can add them as you like.

It might be useful when managing multiple languages in git's private repository. It seems that it can also be used for operation verification of different language versions.

The source code is available on github. https://github.com/fumiyakk/demo-multiple-remote-containers

python is elaborately crafted to use poetry. I will introduce the development environment of python that combines poetry and black.

Recommended Posts

Start multiple containers in VS Code's Remote Container to switch between tasks
[January 2020] Let's start the explosive "development container" with VS Code's Remote Container in earnest.
[Java] How to switch between multiple versions of Java
[Django] Use VS Code + Remote Containers to quickly build a Django container (Docker) development environment.
How to develop containers on remote servers with VS Code Remote-Containers