[LINUX] Easy build of C ++ code with CMake on Docker


--I want to create a Cpp project for Linux on a Windows PC --I searched for CMake's Docker container but couldn't find it, so I created it this time.

good point

--Set to mount local folder --Editing code on a local folder will immediately reflect the changes in the container --Can be edited with an editor such as VS code without logging in to the container --You can also retrieve the built files without using the docker cp command.

Usage example

-① Put .cpp files and CMakeLists.txt in the cpp folder. --Edit with your favorite editor such as VSCode -② Enter the container and execute the build --③ The built file can be retrieved from the cpp folder in the local environment. image.png

Folder structure

--Place Dockerfile and docker-compose.yml as shown below. --Source code is listed at the bottom of the article --The cpp folder is automatically generated by docker-compose up --Put the source code in this folder


Folder structure
├── Dockerfile
├── docker-compose.yml
└── cpp ← Automatically generated



FROM ubuntu:18.04

RUN apt-get update && \
    apt-get install -y sudo && \
    apt-get install build-essential -y && \
    apt-get install -y vim && \
    apt-get install -y wget && \
    apt-get install -y unzip

# Install compilers.
RUN apt-get install -y gcc && \
    apt-get install -y g++

RUN apt-get install -y cmake 3.10.2

# SET path to compilers.
# https://stackoverflow.com/questions/17275348/how-to-specify-new-gcc-path-for-cmake
ENV CC=/usr/bin/gcc \

# OpenBlas, Lapack
RUN apt-get install -y libopenblas-dev  && \
    apt-get install -y liblapack-dev

# Please use below directory to install cpp libraries.
RUN mkdir ./library

CMD bash



version: '3'

    container_name: cmake_container
      context: .
      dockerfile: Dockerfile
    tty: true
    command: /bin/bash

      - ./cpp:/usr/cpp

How to use

Start container

Local environment

#First launch of container
docker-compose up -d

#Enter the running container
docker-compose run cmake

Source code compilation


#Enter the cpp folder inside the container
cd cpp

#Create and enter the build folder
mkdir build
cd build

#run cmake
cmake ../

#Run make

important point

--Do not extract or install OS-dependent libraries in the cpp folder. --I got the following error message and the container was unmounted. - docker compose error while creating mount source path... --Create another folder in the container for dependent libraries and install it there. --Example) OpenCV


――In the following, we will introduce how to build the source code that says Hello CMake!. --Create the following two files in the cpp folder.

Configuration in the cpp folder

├── main.cpp
└── CMakeLists.txt

--Copy the following contents to each file.


#include <iostream>
#include <string>

int main(void){
    std::cout << "Hello CMake!" << std::endl;
    return 0;


#Project name

#Set CMake version
cmake_minimum_required(VERSION 2.8)

# testCMake.The executable file called out is main.Created from cpp
add_executable(testCMake main.cpp)

――Next, we will start the build work in the container. --Enter the following commands in order to build.


#Enter the cpp folder inside the container
cd cpp

#Create a build folder
mkdir build
cd build

#Build execution
cmake ../

--Let's run the last built program! --If you enter ./testCMake and output Hello CMake !, you are successful.


Hello CMake!


--The Docker image introduced in this article can be downloaded from DockerHub.

--Please refer to this article for how to use CMake.

Recommended Posts

Easy build of C ++ code with CMake on Docker
Easy C / C ++ multilingual binding with CMake + SWIG
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Easy construction]
Using Docker (Hyper-V) with PyCharm on Windows 10 (as of August 2017)
Get the host name of the host PC with Docker on Linux
GPU-enabled build of LibTorch (PyTorch C ++) app on Windows without CMake (only the first time)
docker build python based on alpine
Easy Slackbot with Docker and Errbot
Easy installation of OpenCV on RaspberryPi 3+
Build a deb file with Docker
Build Mysql + Python environment with docker
Build PyPy execution environment with Docker
"Cython" tutorial to make Python explosive: When C ++ code depends on the library. First of all, CMake.
Edit the file of the SSH connection destination server on the server with VS Code
Build load test tool Locust 1.1 on Docker
X86 assembler on Linux (linkage with C)
Build CGI Server running on Python 3 on Docker
Easy introduction of speech recognition with Python
[C] [python] Read with AquesTalk on Linux
Build Python environment with Anaconda on Mac
[Linux] Build a jenkins environment with Docker
Launch Flask application with Docker on Heroku
Build CentOS 8 on ESXi 6.7 with minimal configuration
Build NGINX + NGINX Unit + MySQL environment with Docker
[Linux] Build a Docker environment with Amazon Linux 2
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Trial and error]
Easy Python data analysis environment construction with Windows10 Pro x VS Code x Docker
Build a LAMP environment on your local Docker
Build a C language development environment with a container
Build a WardPress environment on AWS with pulumi
Prepare the execution environment of Python3 with Docker
Build Django + NGINX + PostgreSQL development environment with Docker
Build python environment with pyenv on EC2 (ubuntu)
Build Python development environment with Visual Studio Code
Go (Echo) Go Modules × Build development environment with Docker
Build a python environment with ansible on centos6
I tried Flask with Remote-Containers of VS Code
[Python] Build a Django development environment with Docker
Build Oracle Database 19c on Oracle Linux 8.3 (DB Build Part 2)
Convert the character code of the file with Python3
Static analysis of Python code with GitLab CI
Use Docker development container conveniently with VS Code
Build PyPy and Python execution environment with Docker
[Blender x Python] Think of code with symbols
Make your Python environment "easy" with VS Code
Build a python execution environment with VS Code
GradCAM with 22 lines of code. tf_explain may be easy to use, I recommend it!
An easy way to pad the number with zeros depending on the number of digits [Python]