Skip to Content
Technical Articles

Create a development container for CDS in Visual Studio Code

The Visual Studio Code Remote – Containers extension lets you use a Docker container as a full-featured development environment. It allows you to open any folder or repository inside a container and take advantage of Visual Studio Code’s full feature set. A devcontainer.json file in your project tells VS Code how to access (or create) a development container with a well-defined tool and runtime stack. This container can be used to run an application or to sandbox tools, libraries, or runtimes needed for working with a codebase. For further details please see Create a development container.

This is especially useful

  • if a you want to ensure that everybody in a project has the same workspace tools and runtimes
  • or if you have to jump back and forth between several projects which require a different setup each

In this tutorial we will be creating such a development container tailored for Core Data Services (CDS) development.

From above description the only perquisites for this are, having the latest

So the first step is to create a new empty project folder (or choose an existing) and open it in Visual Studio Code.

Next we have to select a container configuration definition using the Remote-Containers: Add Development Container Configuration Files command from the Command Palette (F1).

Select%20a%20container%20configuration%20definition

Select a container configuration definition

We choose Node.js and version 12 which is the currently supported LTS by Core Data Services (CDS) but this should change very soon to the next LTS 14.

We should now have a new folder .devcontainer with two configuration files created within our project folder:

  • devcontainer.json which contains all the configuration for our Development Container
  • Dockerfile which is the base image for our workspace

In devcontainer.json we can define the default extensions being installed within our Development Container. All extensions being defined here will only be available within the Development Container and not globally or for any project!

The configuration expects a list of extension identifiers. To get the identifier for an extension we just have to search for this extension in Marketplace.

The identifier will be shown right after the name. E.g. for SAP Cloud Platform core data services plug-in for Visual Studio Code the identifier is sapse.vscode-cds:

Get identifier for extension

As our workspace will be running in docker container we also need to forward the required ports to our host. For cds this will 4004:

{
	"name": "Node.js",
	"build": {
		"dockerfile": "Dockerfile",
		// Update 'VARIANT' to pick a Node version: 10, 12, 14
		"args": {
			"VARIANT": "12"
		}
	},
	// Set *default* container specific settings.json values on container create.
	"settings": {
		"terminal.integrated.shell.linux": "/bin/zsh"
	},
	// Add the IDs of extensions you want installed when the container is created.
	"extensions": [
		"editorconfig.editorconfig",
		"mikestead.dotenv",
		"visualstudioexptteam.vscodeintellicode",
		"dbaeumer.vscode-eslint",
		"sapse.vscode-cds"
	],
	// Use 'forwardPorts' to make a list of ports inside the container available locally.
	"forwardPorts": [
		4004
	],
	// Use 'postCreateCommand' to run commands after the container is created.
	// "postCreateCommand": "yarn install",
	// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
	// "remoteUser": "node"
}

devcontainer.json

Next we have to adjust our Dockerfile to install the required libraries and modules:

  • sqlite3
  • @sap/cds-dk
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.145.1/containers/javascript-node/.devcontainer/base.Dockerfile

# [Choice] Node.js version: 14, 12, 10
ARG VARIANT="14-buster"
FROM mcr.microsoft.com/vscode/devcontainers/javascript-node:0-${VARIANT}

# [Optional] Uncomment this section to install additional OS packages.
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
  && apt-get -y install --no-install-recommends \
  sqlite3

# [Optional] Uncomment if you want to install an additional version of node using nvm
# ARG EXTRA_NODE_VERSION=10
# RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}"

# [Optional] Uncomment if you want to install more global node modules
RUN sudo -u node npm install -g  \
  @sap/cds-dk

Dockerfile

That’s it we are all set!

The final step is to reopen the project in our Development Container using the Remote-Containers: Reopen in Container command:

Reopen in Container

On first open this will build our Development Container.

Once completed everything should be set up ready for CDS development:

Workspace

👾 Enjoy!

 

2 Comments
You must be Logged on to comment or reply to a post.