Docker-Sync for MacOS

If you're in the process of Docker-izing your development environment and you're a MacOS user, you may have noticed that compilation time is significantly longer in your Docker container than on your host system. For smaller projects, it's probably not a huge deal. But if your project contains hundreds or thousands of source files this can turn into a big pain.

One workaround for MacOS users is docker-sync. Here's more information on how exactly docker-sync works.

Installing docker-sync is straightforward. However it does require managing one additional tool when developing. But for the speed improvements on MacOS it's definitely worth it. And if you're only working in one container, you really just start docker-sync once, and then let it run. Even if you have multiple development environments, it's not that onerous to manage.

Installing docker-sync

~ gem install docker-sync

Add docker-sync.yml to Project Folder

Create a file called docker-sync.yml and add it to the top-level of your project folder. The file should contain the following:

version: "2"
syncs:
        myproject: #volume name, you can call this anything, just remember what you called it
        sync_strategy: native_osx
        src: 'path/to/your/project' #this path is specific to your local machine so absolute path is probably best

Sync File

Sync the project files on your host machine with the docker-sync container

~ docker-sync start
ok  Starting native_osx for sync myproject
myproject
unison: stopped
unison: started
success  Sync container started
success  Starting Docker-Sync in the background
~

Run with --mount

When running lager exec commands use the --mount flag and use the project name you set in the docker-sync.yml file (e.g. myproject).

~ lager exec build --mount myproject
-- Configuring done
-- Generating done
-- Build files have been written to: /github/workspace/_release
[68/68] Linking C executable app
text       data     bss     dec     hex filename
43460       128   27344   70932   11514 /github/workspace/_release/app
 ~

Speed Test

Here's a side by side comparison of building the same project inside a Docker container on MacOs. The first test uses docker-sync and the second does not.

With Docker-Sync

      ~ time lager exec build --mount myproject
      -- Configuring done
      -- Generating done
      -- Build files have been written to: /github/workspace/_release
      [68/68] Linking C executable app
      text       data     bss     dec     hex filename
      43460       128   27344   70932   11514 /github/workspace/_release/app
      lager exec cmake-selftest-release --mount myproject  0.36s user 0.11s system 19% cpu 2.429 total
       ~

Without docker-sync

      ~ time lager exec build
      -- Configuring done
      -- Generating done
      -- Build files have been written to: /github/workspace/_release
      [68/68] Linking C executable app
      text       data     bss     dec     hex filename
      43460       128   27344   70932   11514 /github/workspace/_release/app
      lager exec cmake-selftest-release  0.34s user 0.11s system 5% cpu 7.721 total
       ~