Building OpenHAB-Addons in a container using VS Code Remote Containers

I’ve been wanting to start working on a binding for quite a while. But since I’m a .Net developer by trait the whole Java environment tends to feel a bit too much, too even get started.
But lately I’ve been doing a lot of cross platform dotnet core development using docker containers, and I ran into this:

Where you can also easily build java applications:

So you can build your OpenHAB addon without the need of even a JDK on your local machine. :+1:
But to use it, you need a .devcontainer folder with a dockerfile which has everything to build OpenHAB addons.
I’ve editted Microsofts example to this:

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See for license information.


# Avoid warnings by switching to noninteractive
ENV DEBIAN_FRONTEND=noninteractive

# This Dockerfile adds a non-root user with sudo access. Use the "remoteUser"
# property in devcontainer.json to use it. On Linux, the container user's GID/UIDs
# will be updated to match your local UID/GID (when using the dockerFile property).
# See for details.

# Maven settings
ARG MAVEN_SHA=c35a1803a6e70a126e80b2b3ae33eed961f83ed74d18fcd16909b2d44d7dada3203f1ffe726c17ef8dcca2dcaa9fca676987befeadc9b9f759967a8cb77181c0
ENV MAVEN_HOME=/usr/local/share/maven
COPY maven-settings.xml ${MAVEN_HOME}/ref/

# Configure apt and install packages
RUN apt-get update \
    && apt-get -y install --no-install-recommends apt-utils dialog 2>&1 \
    # Verify git and needed tools are installed
    && apt-get -y install \
        git \
        iproute2 \
        procps \
        curl \
        apt-transport-https \
        gnupg2 \
        lsb-release \
        wget \
    # Create a non-root user to use if preferred - see
    && groupadd --gid $USER_GID $USERNAME \
    && useradd -s /bin/bash --uid ${USER_UID} --gid ${USER_GID} -m $USERNAME \
    # [Optional] Add sudo support for the non-root user
    && apt-get install -y sudo \
    && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME\
    && chmod 0440 /etc/sudoers.d/$USERNAME \
    # Install Maven
    && mkdir -p ${MAVEN_HOME} ${MAVEN_HOME}/ref \
    && curl -fsSL -o /tmp/apache-maven.tar.gz${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
    && echo "${MAVEN_SHA} /tmp/apache-maven.tar.gz" | sha512sum -c - \
    && tar -xzf /tmp/apache-maven.tar.gz -C ${MAVEN_HOME} --strip-components=1 \
    && rm -f /tmp/apache-maven.tar.gz \
    && ln -s ${MAVEN_HOME}/bin/mvn /usr/local/bin/mvn \
    # Install OpenHAB
    && wget -qO - '' | apt-key add - \
    && echo 'deb stable main' | tee /etc/apt/sources.list.d/openhab2.list \
    && apt-get update \
    && apt-get install -y openhab2 \
    # Clean up
    && apt-get autoremove -y \
    && apt-get clean -y \
    && rm -rf /var/lib/apt/lists/*

# Switch back to dialog for any ad-hoc use of apt-get

It seems to be working, building a bundle works, although I’m having some issues with git detecting a lot of changes. I think it’s a windows vs unix line ending problem.
After I fix that I’ll write up a complete manual. But I’m sharing this already since it’s a quick way to start working on an OpenHAB addons:

  • Install Visual Studio Code
  • Install Docker for Desktop
  • Add plugins to VSCode
  • Clone the openhab-addons repo
  • Start working in the development container

And it’s the same if your working on Windows or Linux.

1 Like

Can I ask what local environment you are using (OS and docker version)? I tried going this route as well (as opposed to the Code-Server approach) but I did not manage to get it running. IIRC it was because an “enterprisey” docker version was required and the version I had installed on my ubuntu box wasn’t enough.

As it happens, my reason was also to get a .Net dev environment running. I managed to do so using Code-Server instead and it works fine. However, because of license issues, the .Net debugger will not run on Code-Server (doh)

I’m using Windows 10 Pro 64bit (1909), with Docker Desktop (stable).
I know you always do need Docker Enterprise on Windows Server operating systems, but not on Linux I believe.

Ok, thanks. On linux, you need Docker CE or EE but it’s not clear what you get by just saying apt-get install docker like I did. At least it seems I’m missing some parts like a docker group. But I guess I’ll have to try again. Remove Containers do seem to have a few advantages over Code-Server.

Just want to say I got it working - looks great. Seems an improvement over Code-Server because you get the real VS Code view instead of just a browser window. For simple OpenHab item/rule editing I would say that doesn’t matter, but for addon coding and debugging it makes a big difference.

I got confused about the docker versions but I can confirm that Remote Container work with a plain docker install on linux with an added ‘docker’ user group. No user registration or special install required.