We often use Docker in development for a variety of reasons: to work on the production-like environment, to isolate the application, etc. In particular, I use Docker for Mac to develop applications that are intended to be run on Linux. Along with bash scripts to set up the environment, it allows not to work with VMs (well, virtually, because Docker for Mac runs in the VM that is not exposed to an end-user) and also gives me a bunch of isolated lightweight working environments. Unfortunately, there is a downside: poor IDE support. And while IDEs like PyCharm handle that pretty well with its Remote Interpreters feature, CLion have not gone that far yet. However, there is the Remote Projects feature that allows us to work on any machine that exposes SSH and ports required by debuggers and any other software. In this tutorial, I will review the Docker-based approach. However, you can omit the Docker part and go with the same setup for your VM or remote server.

Prerequisites

  • CLion 2018.3
  • Docker

Building your Dockerfile

First of all, we need to define what will you need to install into your development environment:

  • rsync if your host is macOS or Linux;
  • make, cmake, gdb and compilers – those are essential;
  • SSH server;
  • Optionally you can add valgrind and any other dependency required by your process.

So let’s write a simple Dockerfile:

FROM ubuntu:bionic
RUN apt-get update && \
    apt-get install -y build-essential cmake gdb openssh-server python
# This allows you to log in as the root user and your password will be `root`
RUN echo 'root:root' | chpasswd && \
    mkdir /var/run/sshd && \
    echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
EXPOSE 22 63342
# -D flag runs sshd in foreground
CMD ["/usr/sbin/sshd", "-D"]

Build it, tag it, run it: docker run -p 2222:22 -p 63342:63342 --security-opt seccomp:unconfined yourimagetag. seccomp:unconfined is required to run a debugger.

To verify that you have access to SSH run ssh -p 2222 root@localhost, you should be able to log in with the password root. If you can, then you can move to the next step.

You can also combine this approach with Docker multi-stage builds to separate your environments.

CLion setup

This part mostly repeats the official guide to CLion Remote Project.

  1. Add a new toolchain. Near the Name field select the Remote host option. Then enter the SSH credentials of the running container and wait until CLion detects all the software.

    CLion remote toolchain setup

  2. Add a new CMake configuration. You just need to copy the existing one and change the toolchain to the one you have added in the previous step.

    CLion custom toolchain configuration

  3. Finally, wait for some time until CLion builds the index for the remote environment and switch to it in Run Configurations.

    Selecting the appropriate run configration

Conclusion

Congrats, now you are all set for a painless Docker/remote/VM-based C++ development! This is a pretty good approach until full-featured Docker support arrives in CLion. Now when you want to develop your dockerized application you just need to run your container and wait for CLion to connect to it.

Acknowledgements