如何使用 CLion 在 Docker 容器中 code/run 程序?
How to code/run programs in a Docker container using CLion?
我是 CLion 的新手
我发现 CLion 支持 Docker。
我想执行以下操作:
(Refer my Linux OS as A.)
(Refer the docker container within A as B.)
The library and environments are set up in B.
The CLion IDE is running in A.
I want to code/debug the program that runs in B using the IDE in A.
这是 CLion 支持的功能吗?
我看了他们的文档,但是我找不到实现的方法
谢谢
编辑:截至 2021.3 预览版,CLion 内置了对 docker 的支持。
详情见。
截至 2018 年底,Remote Development 在 CLion 中的设置非常容易。我们所要做的就是设置一个 docker 容器作为我们的“远程主机”。
我参考了 https://github.com/shuhaoliu/docker-clion-dev 上的指南并做了一些修改。这是对我有用的:
(可选)如果您没有安装 CLion 的 Docker 插件,请按照 these instructions.
获取它
步骤 1 - Docker文件
修改 this Dockerfile 以安装项目所需的任何依赖项。将 Docker 文件添加到您的项目。
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"]
步骤 2 - Docker撰写
在与之前docker文件相同的目录中,创建一个docker-compose.yaml文件。
# 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
步骤 3
确保 Dockerfile
和 docker-compose.yml
文件在同一目录中。
选项 3A(使用 CLion Docker 插件)
右键单击 docker-compose.yml
文件和 select Run
。
一两分钟后,容器应该被创建并可以从 Clion 的 Docker 选项卡中查看。
选项 3B(没有 CLion Docker 插件)
从包含 Dockerfile
和 docker-compose.yml
文件的目录,运行:
docker-compose up -d
第 4 步 - 配置工具链
打开设置->构建、执行、部署->工具链并创建一个新的远程主机 工具链。
在 Credentials 字段中单击右侧的小文件夹,然后输入在 Docker 文件中创建的调试器用户的凭据。
在上面的示例中,用户名是“debugger”,密码是“pwd”。
第 5 步 - CMake 配置文件
现在我们必须设置 CMake 配置文件以使用我们新的远程主机工具链。
导航至设置->构建、执行、部署-> Cmake并创建一个新的配置文件。唯一必要的更改是 selecting 在上一步中创建的工具链。
第 6 步 - Running/Debugging 程序
在 CMake 选项卡中,确保您拥有新创建的 CMake 配置文件selected。
CMake 项目加载到容器后,您应该可以 select 在 CLion 右上角的 运行 配置切换器中使用您想要使用的 CMakeProfile。
希望如果一切顺利,您现在应该能够 运行 并在 docker 容器中调试代码!
如果事情没有完全按照计划进行,这里有一些参考可以帮助我让事情顺利进行:
这可能不是很常规的方法,但实际上您可以在 docker 容器中 运行 CLion 本身。如果解析你的代码库需要一些你只在 docker 而不是在你的系统上的库(我这样做是为了与 ROS 一起工作),这可能很有用。此外,您不需要搞乱 "remote developement" 长加载和同步时间,调试器也可以工作 "out of the box"。
可以找到此类设置的示例 here。
从 CLion 2021.3 EAP 开始,有一个 built in Docker Toolchain。
直接取自CLion blog post:
- Go to Settings/Preferences | Build, Execution, Deployment | Toolchains.
- Add a new toolchain, select Docker type.
- Create a docker server and select it in the toolchain settings.
- Select one of the available Docker images.
Now you can create CMake profiles using this Docker toolchain:
查看 full blog post 了解有关如何在不同平台上设置和优化 docker 工具链的更多详细信息和提示!
我是 CLion 的新手
我发现 CLion 支持 Docker。 我想执行以下操作:
(Refer my Linux OS as A.)
(Refer the docker container within A as B.)
The library and environments are set up in B.
The CLion IDE is running in A.
I want to code/debug the program that runs in B using the IDE in A.
这是 CLion 支持的功能吗? 我看了他们的文档,但是我找不到实现的方法
谢谢
编辑:截至 2021.3 预览版,CLion 内置了对 docker 的支持。
详情见
截至 2018 年底,Remote Development 在 CLion 中的设置非常容易。我们所要做的就是设置一个 docker 容器作为我们的“远程主机”。
我参考了 https://github.com/shuhaoliu/docker-clion-dev 上的指南并做了一些修改。这是对我有用的:
(可选)如果您没有安装 CLion 的 Docker 插件,请按照 these instructions.
获取它步骤 1 - Docker文件
修改 this Dockerfile 以安装项目所需的任何依赖项。将 Docker 文件添加到您的项目。
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"]
步骤 2 - Docker撰写
在与之前docker文件相同的目录中,创建一个docker-compose.yaml文件。
# 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
步骤 3
确保 Dockerfile
和 docker-compose.yml
文件在同一目录中。
选项 3A(使用 CLion Docker 插件)
右键单击 docker-compose.yml
文件和 select Run
。
一两分钟后,容器应该被创建并可以从 Clion 的 Docker 选项卡中查看。
选项 3B(没有 CLion Docker 插件)
从包含 Dockerfile
和 docker-compose.yml
文件的目录,运行:
docker-compose up -d
第 4 步 - 配置工具链
打开设置->构建、执行、部署->工具链并创建一个新的远程主机 工具链。
在 Credentials 字段中单击右侧的小文件夹,然后输入在 Docker 文件中创建的调试器用户的凭据。
在上面的示例中,用户名是“debugger”,密码是“pwd”。
第 5 步 - CMake 配置文件
现在我们必须设置 CMake 配置文件以使用我们新的远程主机工具链。
导航至设置->构建、执行、部署-> Cmake并创建一个新的配置文件。唯一必要的更改是 selecting 在上一步中创建的工具链。
第 6 步 - Running/Debugging 程序
在 CMake 选项卡中,确保您拥有新创建的 CMake 配置文件selected。
CMake 项目加载到容器后,您应该可以 select 在 CLion 右上角的 运行 配置切换器中使用您想要使用的 CMakeProfile。
希望如果一切顺利,您现在应该能够 运行 并在 docker 容器中调试代码!
如果事情没有完全按照计划进行,这里有一些参考可以帮助我让事情顺利进行:
这可能不是很常规的方法,但实际上您可以在 docker 容器中 运行 CLion 本身。如果解析你的代码库需要一些你只在 docker 而不是在你的系统上的库(我这样做是为了与 ROS 一起工作),这可能很有用。此外,您不需要搞乱 "remote developement" 长加载和同步时间,调试器也可以工作 "out of the box"。
可以找到此类设置的示例 here。
从 CLion 2021.3 EAP 开始,有一个 built in Docker Toolchain。
直接取自CLion blog post:
- Go to Settings/Preferences | Build, Execution, Deployment | Toolchains.
- Add a new toolchain, select Docker type.
- Create a docker server and select it in the toolchain settings.
- Select one of the available Docker images.
Now you can create CMake profiles using this Docker toolchain:
查看 full blog post 了解有关如何在不同平台上设置和优化 docker 工具链的更多详细信息和提示!