Note: Source code is modified from Cornell CS 5450 course material.
Update December 2018: this repository has been updated after the Clion 2018.3 release, which adds native remote debugging support. Check out the official guide It makes a debugger's life much easier. HOORAY!
This repository provides a minimal C++ project setup and the Dockerfile that allows developers to debug code in a Docker container using JetBrain Clion IDE.
Debugging in a container has many benefits, especially if you are developing Linux applications on OS X.
- It allows a consistent environment for both development and deployment;
- Developers are free from installing dependencies on their development machine. Some packages cannot be easily installed on OS X.
- The container can be launched on your development machine, or on a remote server.
All application code, as well as its dependencies, will be installed, compiled, and ran within the container.
Clion supports remote debugging feature since 2018.3.
The container has a long running ssh server process, such that the container can be placed on a remote host. 本质上即在Docker容器内运行ssh server,ssh server默认端口22,进行容器端口映射为7776后,即可从本地进行远程ssh连接(类似远程调试服务器那样)。 例如:ssh debugger@localhost -p 7776
The container exposes 2 ports. 7777 is for
gdbserver
connection. 22 for the ssh server. To avoid trouble when the container is launched on the development machine, the container 22 port is mapped to host 7776 port. This can be changed to any arbitrary number indocker-compose.yml
.
On your development machine, you must have a CLion IDE (2018.3 or above) installed,
On the host machine of your container (which can be the development machine), the latest Docker CE installation would be sufficient.
To debug the example, follow the following steps. If you have any problem, please refer to the official tutorial before opening an issue. 下面1-3步目的是创建带ssh server功能的Docker容器,可以不用完全按照这个步骤来,只要创建成功即可。创建成功后,可在命令行通过ssh命令测试是否能成功连接。4-6步是重点配置过程。
Modify this Dockerfile to install any dependencies your project needs. Add the Dockerfile to your project. The default dockerfile provides essential building tools when developing c++.
In the same directory as the previous docker file, create a docker-compose.yaml file. You can also use the default docker-compose file.
Ensure that the Dockerfile
and docker-compose.yml
files are in the same directory.
Right-click the docker-compose.yml
file and select Run
.
After a minute or two the container should be created and be viewable from Clion's Docker tab.
From the directory containing the Dockerfile
and docker-compose.yml
files, run:
docker-compose up -d
After this step, the container is running with an ssh server daemon. Clion will automatically run/test/debug via an ssh connection. The folder where docker-compose.yml
locates will be the mapped to /home/debugger/code
within the container. CLion will not use this mapped directory.
Open Settings->Build, Execution, Deplyment -> Toolchains and create a new Remote HostToolchain.
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".
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.
From the CMake tab, make sure that you have the newly created CMake profile selected.
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.
Hopefully if everything went well you should now be able to run and ebug code in a docker container!
Some references may also be helpful:
- Add dependency installation scripts to
Dockerfile
. - Replace
CMakeLists.txt
with your customized projectCMakeLists.txt
.