For the purpose of development in Docker containers, CLion provides full Docker integration via the dedicated Docker toolchain. Watch this video to learn more:
Sample Dockerfile
To help you get started with Docker development in CLion, we created an example Dockerfile for the case of Ubuntu base image. You can copy this file to your project and adjust for your needs or just use it as a reference.
The example file includes the following lines and sections:
In the comments at the top, you can find the commands for building the container.
The FROM ubuntu:20.04 line refers to the container's base image.
The apt-get part installs all the toolchain dependencies into the container. Here you can adjust the tools and their versions.
Depending on your platform and your Docker setup, you may need to run it using sudo.
This command will build the Ubuntu base image with proper toolchain dependencies.
Create a Docker toolchain
Go to Settings / Preferences | Build, Execution, Deployment | Toolchains.
Click and select Docker:
Click the gear button to the Server field to add a Docker image:
note
Out of the options listed in Connect to Docker daemon with, only the local one (first) can be used in CLion as a toolchain. For remote Docker, we recommend using full remote mode.
You can also configure a Docker server in Settings / Preferences | Build, Execution, Deployment | Docker and then select it in the toolchain settings.
Select the Docker Image and wait until the tools detection finishes.
Use the Container Settings field to provide additional container settings, such as port and volume bindings:
tip
See Docker plugin integration for description of the Services tool window and Docker-specific IDE actions.
Build, run, debug with a Docker toolchain
After configuring a Docker toolchain, you can select it in CMake profiles or in Makefile settings. Alternatively, move the toolchain to the top of the list to make it default.
The project folder will mounted to the Docker container and building, running, and debugging will be performed in it. CLion will start the container and shut it down after the command is executed. The project folder will be mounted into the /tmp/ProjectFolder directory in the container.
Improve Docker toolchain performance on Windows
To get better performance on Windows, we recommend using Docker with the WSL 2 backend.
In the Docker desktop application, navigate to Settings | Resources | WSL Integration and enable integration with your WSL distribution (for example, ubuntu-20.04).
Place the project sources into the WSL filesystem (for example, \\wsl$\ubuntu-20.04\tmp\llvm), then open it in CLion and configure a Docker toolchain.
Alternative workflow: develop in Docker using Full Remote mode
note
Normally, we recommend that you use the Docker toolchain as described above. This alternative approach may be helpful in rare cases, for example, when you face performance issues on Windows and WSL 2 backend is not available for some reasons.
In this case, Docker-based toolchains are configured via full remote mode. The container should be running with an SSH daemon.
Depending on your platform and your Docker setup, you may need to run it using sudo.
This command will build the Ubuntu base image with proper toolchain dependencies, set up SSH, and create the user.
2. Run the container
Use the next command, docker run:
docker run -d --cap-add sys_ptrace -p127.0.0.1:2222:22 --name clion_remote_env clion/remote-cpp-env:0.5
In this line, -d runs the container as a daemon and --cap-add sys_ptrace adds the ptrace capability, which is necessary for debugging.
The -p part specifies a port mapping. It exposes the default SSH port inside the container (22) as port 2222 on the host environment. You can specify any available port numbers here.
(Optional) You can create mapped volumes using the -v flag: -v /local/path/to/project:/remote/path/to/project
Last step of building and running the container is the ssh-keygen command, which clears any cached SSH keys. This is important since localhost ports are only temporarily mapped and can be reused by different containers.
In the Credentials field, set up the SSH configuration:
Host - localhost
Port - 2222
User name / Password - as specified in the Dockerfile
After establishing the connection, CLion attempts to detect the toolchain. Since the tools were installed into default locations, they will be detected automatically.
If you change the apt-get part of the Dockefile to install the tools into other locations, provide the paths in the Make, C Compiler, C++ Compiler, and Debugger fields.
If you get an error message "CMake 3.15 or higher is required", it means that the container has an earlier version of CMake than your local project. You can change the version back at the top of the CMakeLists.txt file. As a more complicated alternative, build CMake of a higher version inside the container or use a different base image.
After the files get transferred into the container, you will be able to select the profile in the Run/Debug configuration switcher to build, run, or debug your code inside the container using the specified toolchain.
tip
By default, your project code is transferred into the container at default locations. To change the default paths, use the Mappings tab of the deployment entry.