Git Product home page Git Product logo

docker-clion-dev's Introduction

Debugging C++ in a Docker Container with CLion

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.

Introduction

All application code, as well as its dependencies, will be installed, compiled, and ran within the container. Then, we launch a gdbserver to allow remote debugging outside of 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.

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 in docker-compose.yml.

Prerequisites

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.

Usage

To debug the example, follow the following steps. If you have any problem, please refer to the official tutorial before opening an issue.

  1. Import the project into CLion using the provided CMakeLists.txt.

  2. Build the container.

    docker build -t liuempire/docker_clion_dev .
  3. Launch the container with docker-compose.

    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.

  4. Configure the container toolchain in Clion. Go to Settings/Preferences | Build, Execution, Deployment | Toolchains, configure the container as a remote host. The username is debugger. The password is pwd. The completed configurations should be similar to the following: Toolchain configuration

  5. Configure the container CMake settings in CLion. Go to Settings/Preferences | Build, Execution, Deployment | CMake, add a container CMake profile:CMake configuration

  6. Check the file mapping settings in Clion. Before each run, code will be rsync-ed to the container at a temporary location. The following configuration should be generated automatically after the previous two steps:Deployment configuration

  7. In your Clion, you should be able to select Debug-Local container in before execution.

  8. Add breakpoints in Clion. Happy debugging!

To stop the container, run docker-compose down.

Customization

  • Add dependency installation scripts to Dockerfile.
  • Replace CMakeLists.txt with your customized project CMakeLists.txt.

docker-clion-dev's People

Contributors

craftytrickster avatar shuhaoliu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

docker-clion-dev's Issues

What should the mapping be for local debugging? Is there a way to avoid rsync-ing?

Thanks for sharing your setup, it's really helpful.
I have the above two questions regarding CLion configuration, from this part of the README:

https://github.com/shuhaoliu/docker-clion-dev#usage

Check the file mapping settings in Clion. Before each run, code will be rsync-ed to the container at a temporary location.

How should you configure the local and "remote" mapping for local debugging (source is on your dev machine)? (Partially answered in #4)

Is there a way to avoid rsync-ing, considering the source is already on the local dev machine?

How edit the dockerfile and yml file to show GUI?

I want to use clion on win10 with Remote Docker Container debugging Env(OpenCV 4) Of Ubuntu 18.04.I install Docker in my local machine(win10). Thanks to https://github.com/shuhaoliu/docker-clion-dev, I use your Dockerfile and yml file:

It works well with many C++ library,but I want to use OpenCV4 Library,I want to run this CPP file:

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[]) {
    Mat src = imread("/code/001.png");

    if (src.empty()) {
        printf("cannot load images!\n");
        return -1;
    }
    namedWindow("show_photo", WINDOW_AUTOSIZE);
    imshow("show_photo", src);

    return 0;
}

It shows that: **(show images:314): Gtk-WARNING : 09:56:09.309: cannot open display:

I found some information,But they are all about entering the command line, how to modify the dockerfile and yml file?? Please master programmers help me!

My IDE is Clion on Win10,I already install Xming on win10 and knows the X0.hosts .

Less transfers ?

Hello
Firs of all thanks for writing down this great solution :)

I have a lot of problems with "Deployment" sometimes files not uploaded etc. (Check options under "Deployment" to fix that)

So I'm building using local docker so I thought that I can reuse mounted path in docker instead transferring code to /tmp via SFTP.

I just added code path in exclude paths. So this way it won't transfer extra data to generate cmake-build folder.

Seems to work...

root SSH login does not work in some scenarios

I discovered that this line in the Dockerfile works:

RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config

However, for me the entry in the /etc/ssh/sshd_config file itself is still commented out (not sure if it's always a problem) and as a result still gets "access denied" when trying to SSH as root. i.e.:

$ grep -i "PermitRootLogin yes" /etc/ssh/sshd_config
#PermitRootLogin yes

An easy fix is to either update the existing sed command or add a second sed command such as:

RUN sed -i 's/#PermitRootLogin/PermitRootLogin/' /etc/ssh/sshd_config

If the entry is not commented out, the secondary sed command has no effect. Then I was able to successfully SSH once the file looked like this:

$ grep -i "PermitRootLogin yes" /etc/ssh/sshd_config
PermitRootLogin yes

Mostly posting here in case anyone else runs into this. I will submit a PR if I get a chance. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.