Using ccache when building inside of docker

I am working on moving the build for a C++ project into a docker image. The image will be built and pushed by a Jenkins job. Prior to docker, I made heavy use of ccache to speed up my builds on Jenkins, especially in the case of builds where very little changed. The trouble with docker is that the build now runs in an isolated environment, so I can no longer benefit from ccache. Is there a way to build inside of an ephemeral container while still taking advantage of ccache?

  • How to handle docker API /images/create?
  • Windows Image on Docker
  • Testing connection to docker daemon
  • asp.net web api on docker stops
  • Docker + rails 4 :, weird error during : RUN bundle exec rake assets:precompile
  • Docker container networks
  • understanding docker volume : can i create a volume and assign it to the specific container /service?
  • I can not access my Container Docker Image by HTTP
  • resolve internal DNS in linked docker containers
  • Everything inside one docker container or specialized containers
  • Docker installation debian openjdk-7-jre
  • Concourse pending for long time before running task
  • One Solution collect form web for “Using ccache when building inside of docker”

    You can still use ccache in conjunction with your build.

    Create a Data Volume to allow data to persist between compilations/builds using the following command:

    $ docker create -v /mnt/ccache:/ccache --name ccache debian
    

    Then create your container that “mounts” the data container created above using the --volumes-from command line option.

    $ docker run -e CCACHE_DIR=/ccache --volumes-from ccache -it debian
    

    Now you’ll be in the shell of the debian container and can install the required apps and test ccache:

    root@15306d02505a:/# apt-get update && apt-get install -y gcc ccache    
    

    Now at this point you can check the cache, and it will be empty as expected:

    root@15306d02505a:/# ccache -s
    cache directory                     /ccache
    cache hit (direct)                     0
    cache hit (preprocessed)               0
    cache miss                             0
    files in cache                         0
    cache size                             0 Kbytes
    max cache size                       1.0 Gbytes
    

    The data volume will persist, so even after the container is terminated, the cache is still there. Future builds that mount the volume (and specify the -e ENV variable) will utilize the cache.

    Then create a simple app, run it, and check the cache again:

    root@15306d02505a:/# cat > foo.c << __EOF__
     int main(int argc, char **argv)
     {
         return 0;
     }
     __EOF__
    
    root@15306d02505a:/# PATH=/usr/lib/ccache:$PATH gcc -o foo.o -c foo.c
    root@15306d02505a:/# ccache -s
    cache directory                     /ccache
    cache hit (direct)                     1
    cache hit (preprocessed)               0
    cache miss                             1
    files in cache                         2
    cache size                             8 Kbytes
    max cache size                       1.0 Gbytes
    

    You can see the cache is now populated, and further builds will see performance improvements because of it.

    The data volume will persist, so even after the container is terminated, the cache is still there. Future builds that mount the volume (and specify the -e ENV variable) will utilize the cache.

    This blog post does a good job of explaining it:

    Using Ccache with Docker

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.