Skip to main content

VS Code Dev Containers Development Environment Setup

Feature Introduction

The VS Code Dev Containers extension allows you to use Docker containers as full-featured development environments. It enables you to run applications locally while isolating your code from the local filesystem, providing:

  • Environment Consistency: Ensure team members work in the same development environment, avoiding issues caused by environment differences.
  • Quick Setup: New members can quickly start and run the project without configuring complex development environments locally.
  • Multi-project Isolation: Easily switch between different projects, each with its own independent development environment.
  • Local File Access: The development environment inside the container can seamlessly access code from the local filesystem.

Usage Steps

1. Install Required Software

Before getting started, ensure you have installed and configured the following software locally:

  • Visual Studio Code: Latest version.
  • Docker Desktop: Latest version for Windows, macOS, or Linux.
  • Remote - Containers Extension: Search for and install ms-vscode-remote.remote-containers in the VS Code extension marketplace.

2. Configure .devcontainer and .vscode

KWDB community provides a Docker-based development environment image kwdb/devcontainer along with corresponding devcontainer.json and launch.json configuration files.

tip

If you want to customize the development environment, you can also base your customization on this image.

In your project root directory, create a folder named .devcontainer and add the following files:

  • devcontainer.json: Defines the development container configuration, such as the Docker image to use, extensions, port forwarding, etc.

Download devcontainer.json

devcontainer.json
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/javascript-node
{
"name": "KWDB",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "kwdb/devcontainer",

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
"settings": {},
"extensions": [
"streetsidesoftware.code-spell-checker",
"ms-vscode.cpptools",
"ms-vscode.cpptools-extension-pack",
"golang.go@0.37.0"
]
}
},

"workspaceMount": "source=${localWorkspaceFolder}/,target=/home/inspur/src/gitee.com/kwbasedb,type=bind,consistency=cached",
"workspaceFolder": "/home/inspur/src/gitee.com/kwbasedb",

"containerEnv": {
// Set environment variables here. More info: https://aka.ms/devcontainers-remote-env
"GOPATH": "/home/inspur",
"GO111MODEL": "off"
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [
26257, // kwbase SQL port
8080 // kwbase HTTP/RESTful port
],

// Use 'portsAttributes' to set default properties for specific forwarded ports.
// More info: https://containers.dev/implementors/json_reference/#port-attributes
"portsAttributes": {
"26257": {
"label": "kwbase SQL"
},
"8080": {
"label": "kwbase HTTP/RESTful"
}
},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
"remoteUser": "root",
"privileged": true
}

In your project root directory, create a folder named .vscode and add the following files:

  • launch.json: Defines debug configurations, such as launch arguments, environment variables, etc.

Download launch.json

launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Attach AE V1.2",
"type": "cppdbg",
"request": "attach",
"program": "${workspaceRoot}/install/bin/kwdbts_server",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
},
{
"name": "(gdb) Attach kwbase",
"type": "cppdbg",
"request": "attach",
"program": "${workspaceRoot}/install/bin/kwbase",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
},
{
"name": "(Go) Launch kwbase V2.0",
"type": "go",
"request": "launch",
"cwd": "${workspaceRoot}/install/bin",
"mode": "exec",
"env": {"KWDB_ROOT":"${workspaceRoot}/install",
"LD_LIBRARY_PATH":"/usr/local/lib:${workspaceRoot}/install/lib"},
"program": "${workspaceRoot}/install/bin/kwbase",
"args": ["start",
"--insecure",
"--listen-addr=0.0.0.0:20257",
"--http-addr=0.0.0.0:20080",
"--store=/home2/dev2/kwbase_data/n31",
"--join=0.0.0.0:20257"
],
"dlvFlags": [
"--check-go-version=false"
]
},
{
"name": "(gdb) Launch kwbase V2.0",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/install/bin/kwbase",
"args": ["start",
"--insecure",
"--listen-addr=0.0.0.0:20257",
"--http-addr=0.0.0.0:20080",
"--store=/home2/dev2/kwbase_data/n31",
"--join=0.0.0.0:20257"
],
"stopAtEntry": false,
"cwd": "${workspaceRoot}/install/bin",
"environment": [{"name":"KWDB_ROOT", "value":"${workspaceRoot}/install"},
{"name":"LD_LIBRARY_PATH", "value":"${workspaceRoot}/install/lib"}],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
//{"text": "set detach-on-fork off"},
//{"text": "set follow-fork-mode child"},
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
},
{
"name": "(Go) Attach to Process",
"type": "go",
"request": "attach",
"mode": "local",
"processId": 0
}
]
}

3. Start the Development Container

  1. Open the Project: Open your project folder in VS Code.
  2. Start the Container:
    • Click the small arrow icon in the lower left corner of VS Code (normally the panel will automatically pop up without clicking the icon).
    • Select "Reopen in Container" from the command palette that appears.
    • VS Code will build and start the development container based on the configuration in .devcontainer.

The first start may take some time as it needs to download the Docker image and build the environment.

4. Develop Inside the Container

Once the container is started, VS Code will automatically connect to the container environment. You can edit code, use the terminal, and debug applications as if you were working locally. All operations are executed inside the container, but your code files are still stored in the local filesystem.

5. Debug Inside the Container

In launch.json, we have pre-configured multiple debug configurations, including launch and attach debugging for both Go and C++. You can use these configurations by following these steps:

  1. Click the Run and Debug button on the left side of VS Code (or use the keyboard shortcut Ctrl+Shift+D)
  2. Select the appropriate debug configuration from the top dropdown menu
  3. Click the green run button or press F5 to start debugging

All debug configurations have been optimized for the container environment and can be used directly.

FAQ

Q1: How do I access local files inside the container?

A1: VS Code Dev Containers automatically mounts your project folder into the container. You can directly access and modify these files inside the container.

Q2: How do I add more VS Code extensions to the development environment?

A2: Add the extension IDs you need to the extensions array in the devcontainer.json file.

Q3: How do I troubleshoot container build failures?

A3: Check if the configuration in devcontainer.json is correct. Check the VS Code output log, which usually provides detailed error messages.

  • forwardPorts: Forwards ports from inside the container to the local machine, making it convenient to access applications in the local browser.
  • postCreateCommand: Commands that run automatically after container creation, such as installing dependencies.
  • mounts: In addition to the project folder, you can mount other local directories into the container.

For more advanced configurations, please refer to the VS Code Dev Containers Official Documentation.