How to code/run programs in a Docker container using CLion?

As of late 2018, Remote Development in CLion is pretty easy to set up. All we have to do is set up a docker container as our "Remote Host".


I used the guide at https://github.com/shuhaoliu/docker-clion-dev as a reference and made a few changes. Here is what worked for me:

(Optional) If you do not have the Docker plugin for CLion installed, get it by following these instructions.

Step 1 - Dockerfile

Modify the this Dockerfile to install any dependencies your project needs. Add the Dockerfile to your project.

FROM ubuntu:cosmic

########################################################
# Essential packages for remote debugging and login in
########################################################

RUN apt-get update && apt-get upgrade -y && apt-get install -y \
    apt-utils gcc g++ openssh-server cmake build-essential gdb gdbserver rsync vim 

RUN mkdir /var/run/sshd
RUN echo 'root:root' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

# 22 for ssh server. 7777 for gdb server.
EXPOSE 22 7777

RUN useradd -ms /bin/bash debugger
RUN echo 'debugger:pwd' | chpasswd

########################################################
# Add custom packages and development environment here
########################################################

########################################################

CMD ["/usr/sbin/sshd", "-D"]

Step 2 - Docker Compose

In the same directory as the previous docker file, create a docker-compose.yaml file.

# From: https://github.com/shuhaoliu/docker-clion-dev/blob/master/docker-compose.yml

version: '3'

services:
  gdbserver:
    build:
      context: ./
      dockerfile: ./Dockerfile
    image: clion_dev
    security_opt:
      - seccomp:unconfined
    container_name: debug
    ports:
      - "7776:22"
      - "7777:7777"
    volumes:
      - .:/home/debugger/code
    working_dir: /home/debugger/code
    hostname: debug

Step 3

Ensure that the Dockerfile and docker-compose.yml files are in the same directory.

Option 3A (With CLion Docker Plugin)

Right-click the docker-compose.yml file and select Run.

enter image description here

After a minute or two the container should be created and be viewable from Clion's Docker tab.

enter image description here

Option 3B (Without CLion Docker Plugin)

From the directory containing the Dockerfile and docker-compose.yml files, run:

docker-compose up -d

Step 4 - Configure Toolchain

Open Settings->Build, Execution, Deplyment -> Toolchains and create a new Remote Host Toolchain.

In the Credentials field click the small folder on the right side and enter the credentials for the debugger user created in the Dockerfile.

In the example above the username is "debugger" and the password is "pwd".

Remote toolchain

Step 5 - CMake Profile

Now we must set up a CMake profile to make use of our new Remote Host toolchain.

Navigate to Settings->Build, Execution, Deplyment -> Cmake and create a new profile. The only necessary change is selecting the toolchain created in the previous step.

Cmake Profile

Step 6 - Running/Debugging the Program

From the CMake tab, make sure that you have the newly created CMake profile selected.

enter image description here

After the CMake project loads into the container, you should be able to select the CMakeProfile you would like to use in the run configuration switcher in the top right corner of CLion.

enter image description here

Hopefully if everything went well you should now be able to run and ebug code in a docker container!


If things didn't quite go according to plan, here are some references that helped me get things working:

  • docker-clion-dev Guide
  • CLion Remote Project Guide
  • Clion CMake Profiles
  • CLion CMake Toolchains

Tags:

Docker

Clion