使用docker配置CLion远程开发环境

使用docker配置CLion远程开发环境

Posted by WW on June 8, 2022

在开发过程在难免会碰到以下问题,比如安装过多依赖污染系统环境而且重装系统又过于折腾,或者系统无法安装特定依赖。 此时,docker的隔离环境便可解决以上问题,随时可以创建一个任意版本的系统环境。 要把docker和CLion结合起来使用,还需要配置docker的ssh服务,CLion通过ssh 远程调试。 当然,对于一个远程主机只要配置好了ssh,也可以使用CLion来远程调试。

参考文章使用 Clion 调试 Docker 内的 C++ 代码

配置docker

编写docker-compose.yml

coder 文件夹中放 Dockerfile shell文件夹中放 cmake.shentrypoint.sh

version: '2'
services:
  coder:
    build:
      context: coder
	  args: 
        #http_proxy : "" # no proxy
        #https_proxy : "" # no proxy
        http_proxy: "http://172.17.0.1:8889" #ok no need to map port
        https_proxy: "http://172.17.0.1:8889" #ok no need to map port
        #http_proxy: "http://host.docker.internal:8889" # fail
        #https_proxy: "http://host.docker.internal:8889"# fail
        #http_proxy: "http://localhost:8889"# fail
        #https_proxy: "http://localhost:8889"# fail
    container_name : coder
    security_opt: # options needed for gdb debugging
      - seccomp:unconfined
      - apparmor:unconfined   
    user: waxz
    working_dir: /home/waxz
    #network_mode: host
    #network_mode: bridge
    ports:
      - "2222:22"
    cap_add:
      - ALL
      
    volumes:
      - /tmp/.X11-unix:/tmp/.X11-unix:rw
      - $HOME/.Xauthority:/home/waxz/.Xauthority:rw
      #- /tmp/.docker.xauth:/tmp/.docker.xauth:rw
      - /dev/usb:/dev/usb
      - ./shell:/opt/shell
      # - ./shell/.bashrc:/home/waxz/.bashrc
      - ./share:/home/waxz/share
      - ./share/.tmux.conf:/home/waxz/.tmux.conf
      - /home/waxz/CLionProjects:/home/waxz/CLionProjects


    entrypoint: /opt/shell/entrypoint.sh
    
    stop_signal: SIGKILL
    pid: "host"
    ipc: host
    privileged: true
    read_only: false
    stdin_open: true
    tty: true
    #networks:
      #- default

编写 Dockerfile

基础镜像根据需要更改

FROM gramaziokohler/ros-panda-planner

RUN apt-get -y update && apt-get install -y openssh-server rsync gdb
  

RUN useradd -ms /bin/bash waxz && echo 'waxz:1234' | chpasswd && adduser waxz sudo 

RUN mkdir /var/run/sshd
#RUN echo 'root:THE_PASSWORD_YOU_CREATED' | 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
RUN env | grep _ >> /etc/environment && env | grep PATH >> /etc/environment

编写 cmake.sh

CLion远程调用cmake时,可能由于缺少环境变量导致找不的依赖从而编译失败

#!/bin/bash


source /opt/ros/kinetic/setup.bash 
export ROS_MASTER_URI=http://localhost:11311
quoted_args="$(printf "${1+ %q}" "$@")" # Note: this will have a leading space before the first arg
# echo "Quoted args:$quoted_args" # Uncomment this to see what it's doing
#echo "quoted_args ${quoted_args}"
bash -c "cmake  $quoted_args"

编写 entrypoint.sh

容器启动时自动运行脚本,启动ssh服务器

#!/bin/bash
source /opt/ros/kinetic/setup.bash
echo 1234 | sudo -S  sh -c "sudo /usr/sbin/sshd -D"

运行容器

docker-compose up

docker-compose run --service-port coder

或者不启动 sshd,直接进入 bash

docker-compose run --service-port --entrypoint bash  coder

ssh 连接测试

ssh waxz@localhost -p 2222 -vvv

CLion

远程部署配置文档

Create a toolchain with remote credentials

  • Go to Settings => Build, Execution, Deployment => Toolchains
  • click + and select Remote Host from the drop-down menu to create a new toolchain.
  • Set Credentials field. In the dialog that opens, create an SSH configuration and provide the credentials for accessing you remote machine. Existing SSH configurations are available from the drop-down list.

  • 设置远程主机的cmake工具,可以使用默认路径,如果要设置编译期间的环境变量可以使用/opt/shell/cmake.sh
  • 可以将工具链设置为默认

    remote toolchain configured successfully

(CMake) Create the corresponding CMake profile

  • create a new CMake profile, and connect it to your remote toolchain using the Toolchain field:

    CMake profile for the remote toolchain

    Alternatively, set the remote toolchain as default and select Use default.

  • Apply the changes.

    Check and adjust the deployment configuration

  • 根据需要修改路径 CLion automatically configures the paths for your project code synchronization. Use the Mappings tab to change the default mappings (for example, to set a particular remote directory for the copied sources instead of the default tmp folder):

    path mapping for file synchronization

  • 使用docker时可以选择不同步文件 For this, open the Connection tab and change the connection type to Local or mounted folder, then set up the path mappings (for Docker, specify the mapped volumes).

    No source synchronization

Set environment variables

环境变量可以采用以上cmake.sh的方法。 也可以在 .bashrc 中修改。

  • To configure environment variables for the remote OS, specify them in the beginning of the .bashrc file, before the # If not running interactively, don't do anything line.

    If your primary shell is not bash, follow the instructions for that particular shell or add PermitUserEnvironment yes to the sshd_config file, restart the sshd service, and then configure the variables in ~/.ssh/environment.