How to reduce compile time using Docker as dev environment on C++
Tags: docker, cpp, showdev, productivity
Published: 2020-06-21 09:08:52

---
title: How to reduce compile time using Docker as dev environment on C++
published: true
description: 



tags: docker, cpp, showdev, productivity
//cover_image: https://cerealtree.files.wordpress.com/2020/06/docker.png?w=640
---


<p style="text-align: justify;"><img class=" size-full wp-image-752 alignleft" src="https://cerealtree.files.wordpress.com/2020/06/docker.png" alt="hacktober" width="259" height="194" /></p>

<h2></h2>
<h2></h2>
<h2></h2>
<h2></h2>
<h2>Abstract</h2>
<p style="text-align: justify;">In this post I'll show how to avoid to wasting your time rebuilding a fresh new image every time you make a small change in your code. <span class="tlid-translation translation" lang="en"><span class="" title="">The reference language is C ++</span></span> but the proposed solution can be used for  every compiled languages and project which require time consuming building process.</p>

<h2>Problem</h2>
<p style="text-align: justify;">Best practices suggest to use multi-stage Dockerfile copying all the source code in the builder stage, compile it and then copy the binary file on next stage. This approach makes sense in production instead while you're in dev mode and you're forced to use the container to compile and makes test has a great problem.
Every time you make a small change in the source code this means a fresh new Docker image; even if the rebuild uses previous layer and just recompile whole the project this can be a problem if it is time consuming requiring several minutes to recompile.</p>
<p style="text-align: justify;">If you're in dev mode waiting for several minutes every time you make a simple change can be annoying.</p>

<h2>Idea</h2>
<p style="text-align: justify;">It will be great to recompile only the files with the changes just like when you're developing and compiling on your system. In order to do this the source code is stored inside the host's file system and mounted as volume by the container. This guarantee that all the object files are not looses and b reused on next compile processes.</p>
Hence the idea is to have a container with the following features:
<ul>
	<li>have all the necessary to compile</li>
	<li>recompile only the changes on source code</li>
	<li>generate the binary file on host's file system</li>
	<li>there is no need to rebuild the image to generate the binary file.</li>
</ul>
This container will be a "binary generator" and it's purpose  its to generate a new binary file without recompile whole the project. Then use new binary file to create the runtime docker image.
<h2>Solution</h2>
<div>In order to better explain my solution I have created a simple "Hello world" project that you can find <a href="https://github.com/Cereal84/cpp_docker_env_example">here</a>. The tree of the project is the following:</div>
<pre>    .
    ├── core.cpp
    ├── core.hpp
    ├── dev_env
    │   ├── builder
    │   │   ├── docker-compose.yml
    │   │   └── Dockerfile
    │   └── runtime
    │       ├── docker-compose.yml
    │       └── Dockerfile
    ├── docker-compose.yml
    ├── Dockerfile
    ├── main.cpp
    ├── Makefile
    └── README.md
</pre>
<p style="text-align: justify;">The Dockerfile and docker-compose.yml at the root level show a single stage build process that will show the original problem: changing one file will rebuild the the whole process, which is what we are trying to avoid.</p>
<p style="text-align: justify;">In the dev_env directory we can find the proposed solution:</p>

<ul>
	<li style="text-align: justify;">the builder directory contains the Dockerfile and docker-compose to generate the builder container (the one used to generate the binary file).</li>
	<li style="text-align: justify;"> the runtime directory contains all the necessary to build test or runtime image.
Instead the runtime contains all the necessary to build <em>test</em> or <em>runtime</em> image.</li>
</ul>
<h3>Builder directory</h3>
<h4>docker-compose.yml</h4>
<pre>version: '2.3'

services:
   
    binary_builder:
        container_name: "binary_builder"
        image: binary_builder
        build:
            dockerfile: ./dev_env/builder/Dockerfile
            context: ../../
            args:
                src_path: /src
        volumes:
            - ../.././:/src:rw
</pre>
<p style="text-align: justify;">As you can see on the src directory, on container, is mounted the root of the project, source code included.</p>

<h4>Dockerfile</h4>
<pre>    FROM ubuntu:18.04
    ARG src_path=/src
            
    RUN apt-get update                          && \ 
        apt-get install -y                         \
        g++ build-essential
     
     RUN mkdir -p $src_path
     WORKDIR $src_path
     CMD     make
</pre>
<p style="text-align: justify;">The Dockerfile is quite simple it takes the source code from <em>$src_path</em> and the compile it (if necessary) every time it will be started. Another advantage is that it is not necessary to rebuild the builder container to generate a new binary.</p>

<h3>Runtime directory</h3>
The Dockerfile is very simple, use the same base image used in builder level and copy the binary file from the host file system.
<h4>Dockerfile</h4>
<pre>    FROM ubuntu:18.04 as run_time
    COPY  hello_world /hello_world
    WORKDIR /
    CMD ./hello_world
</pre>
The advantage is that to rebuild the runtime container you have to generate a new binary file and copy it on the new image saving time.

 
<h2>Conclusion</h2>
I hope this article has helped you.