Developing KDE with Docker

2017-07-23

Docker can help you to quickly set up a development environment for working on a software project. This is great for getting new contributors up to speed quickly. I've recently discovered how nice this is and want to share how I use Docker.

KDE projects often have desktop services, mimetypes and plugins. Ideally, these are installed system-wide. During development it is not convenient to change your main environment just to test your software. This is where Docker comes in. Docker is the most popular Linux container system. It gives you a system that is separate from your main system and which you can rewind when you break it.

Dolphin in KDE neon

The simplest way to get going is to run a fresh Plasma system. KDE neon provides Docker images.

After installing Docker, you can get a system going like this;

# give the docker application access to X
xhost +si:localuser:$USER
# run a throwaway session with dolphin
docker run --rm \
    -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix \
    -ti \
    kdeneon/plasma:dev-stable dolphin

--rm

cleans up the container after use.

-ti

makes the session interactive.

-e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix

gives the container access to your X session.

an empty home
Dolphin running in a throwaway Docker container. No home directory was set up.

Dolphin in openSUSE

Instead of kdeneon/plasma:dev-stable you might want to use a different distribution. You can build you own images by creating your own Dockerfile. Here is a simple Dockerfile for a Docker image for dolphin based on openSUSE.

FROM opensuse

# opensuse image is minimal, so add plasma and dolphin
RUN zypper --non-interactive install plasma5-desktop dolphin \
  && zypper clean

# set up an environment for a user
RUN useradd --create-home user
USER user
ENV KDE_FULL_SESSION=true
ENV XDG_RUNTIME_DIR=/run/user
WORKDIR /home/user

CMD ["bash", "-l"]

To create a docker image, write this text into a file name Dockerfile and run

xhost +si:localuser:$USER
docker build -t opensuse-dolphin .
docker run --rm \
    -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix \
    -ti \
    opensuse-dolphin dolphin

Now a docker image called opensuse-dolphin was created and run. At each uppercased commands like RUN a snapshot is created. The last step is to tag the final snapshot with the name opensuse-dolphin. That name can be used to run a throwaway container with dolphin.

dolphin running in an openSUSE container
Dolphin running in a throwaway Docker container based on openSUSE.

Adding a home

To do any coding, you'd like to provide files to your container and have the ability to store files. This is possible. You can give a container access to a local folder with the -v flag, for example -v $HOME/calligra-dev/home:/home/user which mounts your folder $HOME/calligra-dev/home to the folder /home/user in the container.

I like to manage this container home with git, so I can start with a clean folder by running git clean -fd.

mkdir -p $HOME/calligra-dev/home
cd $HOME/calligra-dev/home
git init
echo 'git clean -fd' > .bash_profile
echo -e 'src\nbuild\ninstall' > .gitignore
git add .bash_profile .gitignore
git commit -a -m 'Initial commit'

So the development environment is managed by Docker on the system level and by git on the level of work directory.

Developing Calligra in Docker

After this introduction, I'm showing the Dockerfile that I'm using for Calligra with QtCreator or KDevelop and the clazy quality checker. Clazy has not been packaged so the Dockerfile needs to have a build step for it.

FROM kdeneon/plasma:dev-stable

USER root

RUN apt-get update && apt-get dist-upgrade -y

RUN apt-get install -y --no-install-recommends \
  cmake extra-cmake-modules g++ gettext git kdoctools-dev kross-dev \
  libboost-all-dev libeigen3-dev libetonyek-dev libfontconfig1-dev \
  libfreetype6-dev libgit2-dev libgsl-dev libkf5activities-dev \
  libkf5archive-dev libkf5kcmutils-dev libkf5kdelibs4support-dev \
  libkf5notifications-dev libkf5notifyconfig-dev libkf5parts-dev \
  libkf5wallet-dev libkf5xmlgui-dev libodfgen-dev libpoppler-qt5-dev \
  libqca-qt5-2-dev libqt5opengl5-dev libqt5svg5-dev libqt5x11extras5-dev \
  librevenge-dev libwpd-dev libwpg-dev libwps-dev ninja-build pkg-config \
  sudo

# requirements for clazy
RUN apt-get install -y --no-install-recommends \
  clang llvm-dev libclang-3.8-dev

# build and install clazy
RUN git clone git://anongit.kde.org/clazy \
  && cd clazy \
  && cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Release -GNinja \
  && ninja install

# dependencies for development
RUN apt-get install -y --no-install-recommends \
  cmake-curses-gui \
  less vim strace qtcreator kdevelop valgrind gdb

USER neon
CMD ["/bin/bash", "-l"]
docker run -v $HOME/calligra-dev/home:/home/neon \
  -v /tmp/.X11-unix:/tmp/.X11-unix \
  -e DISPLAY=$DISPLAY \
  -e XDG_CURRENT_DESKTOP=$XDG_CURRENT_DESKTOP \
  --network none \
  -ti calligra-clazy

Parallel development environments

Docker is easy to set up and lets you have tight control over your development and testing environment. You can have many environments present on your system and reset and start and stop them quickly. Finally, here is a screenshot of my desktop running development environments for Calligra 2 and Calligra 3 in parallel. Both are running in QtCreator. This is a screenshot from my Akademy presentation on Calligra.

Two versions of Calligra running in Docker containers
Two versions of Calligra running in Docker containers.

Helping contributors to get started

Getting started in KDE can be challenging. Docker images or Dockerfiles for complete development environments can get new contributors started more quickly. In KDE, neon already uses Docker images. Jonathan Riddell is giving a presentation about that this Akademy. Because this is so easy, I predict that we'll see more Dockerfiles being published by KDE projects.

Comments

Post a comment

Lightweight than a virtual machine?

From the description, this seems to be more similar to the Python's virtualenv than a heavyweight virtual machine. Is my assumption correct?

If so, then Docker sounds like the best option for me. I want to test waters of KDE development before jumping in, but don't want to pollute my current stable setup of OpenSUSE Leap 42.3. However, development would likely require latest devel packages, which Leap may not provide.

Reply to this comment