Toolchains in Docker
In CLion, you can work with Docker-based toolchains using full remote mode. For this, the container should be running with an SSH daemon.
To help you get started with CLion toolchains in Docker, we created an example Dockerfile. Copy this file to your project and adjust to meet 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, running, and stopping the container.
FROM ubuntu:18.04line refers to the container's base image.
apt-getpart installs all the toolchain dependencies into the container. Here you can adjust the tools and their versions.
sshsection sets up the SSH for CLion to connect into.
usersection creates a user into the container.
Building and running the container
1. Build the container
docker buildline from the top of the Dockerfile:docker build -t clion/remote-cpp-env:0.5 -f Dockerfile.remote-cpp-env .
Depending on your platform and your Docker setup, you may need to run it using
This command will install the Ubuntu image and 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,
-druns the container as a daemon and
--cap-add sys_ptraceadds the
ptracecapability, which is necessary for debugging.
-ppart 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.
3. Clear cached SSH keys
Last step of building and running the container is the
ssh-keygencommand, which clears any cached SSH keys. This is important since localhost ports are only temporarily mapped and can be reused by different containers.ssh-keygen -f "$HOME/.ssh/known_hosts" -R [localhost]:2222"
Using the Remote Development workflow
At this point, the container is running with an SSH server daemon, and you can connect into it using CLion’s standard Remote Development features.
Create a Remote Host toolchain and the corresponding CMake profile
Follow the general instructions on creating a remote toolchain.
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-getpart of the Dockefile to install the tools into other locations, provide the paths in the Make, C Compiler, C++ Compiler, and Debugger fields.
Create a CMake profile that uses the remote toolchain. Wait for the project to reload.
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.