Setup Dev Container for VSCode

Prerequisites

  1. Docker

  2. VSCode with Remote Development Extension Pack

Setup

  1. cd <project>

  2. mkdir .devcontainer

  3. touch .devcontainer/devcontainer.json

  4. touch .devcontainer/Dockerfile

devcontainer.json

Example:

 1{
 2    "name": "Dev Container",
 3    "image": "tdc-manager-dev-img",
 4    "extensions": [],
 5    "workspaceFolder": "/workspace", // This enable VSCode to find the folder to open as a workspace
 6    "settings": {
 7        "terminal.integrated.shell.linux": "/bin/bash"
 8    },
 9    "workspaceMount": "source=${localWorkspaceFolder},target=/workspace,type=bind,consistency=cached",
10    "runArgs": [
11        "--net=host",
12        "--privileged",
13        "-e",
14        "VEHICLE_NAME=my-vehicle",
15        "-e",
16        "__RUNTIME_ENV=production",
17        "-v",
18        "${localWorkspaceFolder}/manager_root:/root/tdc_manager",
19        "-v",
20        "${localWorkspaceFolder}/config_root:/etc/app_persist",
21        "-v",
22        "/etc/timezone:/etc/timezone:ro",
23        "-v",
24        "/etc/localtime:/etc/localtime:ro",
25        "-v",
26        "/run/docker.sock:/run/docker.sock",
27        "-v",
28        "/home/tusen/.docker/config.json:/root/.docker/config.json",
29        "-v",
30        "${localWorkspaceFolder}/manager_root/.mongodb:/var/lib/mongodb"
31    ]
32}
  • runArgs is not required, but it is useful to pass environment variables and mount volumes.

  • We mounted docker.sock to be able to run docker commands inside the container.

Dockerfile

Example:

 1FROM ***.com/docker-base/x86_64/os/ubuntu:20.04-tspkg-base
 2# 
 3ENV NODEJS_VERSION=10.15.0
 4# install mongodb
 5RUN apt-get update && \
 6    apt-get install mongodb -y && \
 7    curl -L https://nodejs.org/dist/v${NODEJS_VERSION}/node-v${NODEJS_VERSION}-linux-x64.tar.gz | \
 8    tar -C /usr/local --strip-components=1 -xzf - && \
 9    rm -rf /var/lib/apt/lists/*
10
11# create dir for dev container
12RUN mkdir -p /workspace
  • Dockerfile can be arbitrary.

  • We installed mongodb and nodejs in this example.

Build Image

Execute:

1docker build -t tdc-manager-dev-img .