Create a VS Code Devcontainer for Working with RDF

There are loads of reasons why you might want to make use of a devcontainer. And even more ones for why you would want to work with RDF. This little post helps you do both. If you’re interested in working with RDF and Apache Jena, setting up a VS Code devcontainer can quickly get you to a point where you can work with your RDF data directly out of VS Code. In this blog post, I will guide you through the process of setting up a devcontainer with all the necessary tools and plugins.

Step 1: Install Visual Studio Code and Docker and setup basic devcontainer

To get started, make sure you have Visual Studio Code installed on your machine. You can download it from the official website: https://code.visualstudio.com. Additionally, you will need Docker installed to run the devcontainer. Docker provides a lightweight and consistent environment for development. Lastly you need to install the devcontainers plugin (ms-vscode-remote.remote-containers) for vscode from the marketplace. Once you have them installed and running, right click in bottom left corner of VS Code to create a new devcontainer.

A context menu will appear. Select the “New Dev Container” option.

Scroll down to the Ubuntu option, and select it:

Give your container a name and click on “Create Dev Container”

That’s enough to get going in an Ubuntu container. Pretty simple, huh?

Step 2: Create a Devcontainer Configuration

Once you have Visual Studio Code and Docker set up, it’s time to tailor the devcontainer configuration to run the tools we need. Open your project in Visual Studio Code and navigate to the .devcontainer directory (create one if it doesn’t exist). Inside this directory, create a file named devcontainer.json and add the following configuration:

{
	"name": "My RDF Container",
	"build": {
		"dockerfile": "Dockerfile",
	},
	"settings": {},
	"extensions": [
		"stardog-union.vscode-stardog-languages",
        "Zazuko.vscode-rdf-sketch"
	],
}

Let’s break down this configuration:

  • "name": The name of your devcontainer.
  • "build": The Docker file to use as the base for the devcontainer. In this case, we are building an image starting from a foundation based on Ubuntu 22.04.
  • "extensions": The list of Visual Studio Code extensions to install in the devcontainer. Here, we are including some useful extensions for working with RDF and XML.

Step 3: Create a Docker container

The next step is to alter the docker container to allow the Jena CLI tools to be used.

FROM mcr.microsoft.com/devcontainers/base:jammy
ARG INSTALL_ZSH="false"
ARG UPGRADE_PACKAGES="false"

ARG USERNAME=aabs
ARG USER_UID=1000
ARG USER_GID=$USER_UID
COPY library-scripts/*.sh library-scripts/*.env /tmp/library-scripts/
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
    # && /bin/bash /tmp/library-scripts/common-debian.sh "${INSTALL_ZSH}" "${USERNAME}" "${USER_UID}" "${USER_GID}" "${UPGRADE_PACKAGES}" "true" "true"\
    # ****************************************************************************
    # * Install the JDK and wget
    # ****************************************************************************
    && apt-get -y install --no-install-recommends wget default-jdk \
    && apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/* /tmp/library-scripts

    # ****************************************************************************
    # * Install the Apache Jena command line 
    # ****************************************************************************
RUN mkdir -p /tmp/jena-tmp \
    && cd /tmp/jena-tmp \
    && wget --no-check-certificate https://dlcdn.apache.org/jena/binaries/apache-jena-4.9.0.tar.gz \
    && tar xzvf apache-jena-4.9.0.tar.gz \
    && echo "export JENA_HOME=/tmp/jena-tmp/apache-jena-4.9.0" >> /home/vscode/.profile \
    && echo 'export PATH=$PATH:$JENA_HOME/bin' >> /home/vscode/.profile \
    && rm -rf /tmp/jena-tmp/apache-jena-4.9.0.tar.gz \
    && rm -rf /tmp/jena-tmp/src-examples

Step 4: Run the VS Code in the new container

Step 5: Test the Jena tools

You now have the Jena tools installed and available on the terminal of the devcontainer. Open the terminal and run a Jena SPARQL query on your data. Here’s me running it on some data of mine: