Optimising Docker for Mac and Elixir
In response to highlight how slow Docker for Mac can be in syncing files, between the host and docker container, we can try and optimise the docker-compose override file used in development, to ensure we sync as little as possible to the host file system.

The minimum set of files we want to sync is the actual source code that is committed into git. Look at your .gitignore for hints on what to remove in your project. For our Elixir service we have dependencies, and build artifacts, that are written to disk, so moving deps and _build folders external to the shared project folder should speed things up.
Let’s set up a docker-compose-override.ymlfile. This file is loaded by default unless you explicitly don’t load it by using the -f flag (eg docker-compose -f docker-compose.yml build) will not use the override file, whereas (eg docker-compose build) will use the developer overlay file (as well as the original).
- Update your source code so you can explicitly set the
deps_pathandbuild_pathattributes:
mix.exs
def project do
[
app: :lab_fulfillment_service,
aliases: aliases(),
build_path: System.get_env("ELIXIR_BUILD_PATH") || './_build',
deps_path: System.get_env("ELIXIR_DEPS_PATH") || './deps',
deps: deps()
]
end
- Update the
docker-compose-override.ymlfile to pass through the location of where to put thebuildanddepsfiles:
app:
volumes:
- .:/opt/app
- elixir-artifacts:/opt/elixir-artifacts
environment:
ELIXIR_BUILD_PATH: /opt/elixir-artifacts/_build
ELIXIR_DEPS_PATH: /opt/elixir-artifacts/deps
volumes:
elixir-artifacts: {}
What are the performance results?
The results show a speed improvement of 2.6 to 8 times. However, this is still slow compared to linux. If you must run Docker for Mac I would still recommend further optimisation investigations. Also note that the optimisation actually made the linux version slower. This suggests that using an explicit volume is slower than the default volume. The full results are here:
╔══════════════════════════════════════╦══════════╦════════════╗
║ Machine ║ One Test ║ Test Suite ║
╠══════════════════════════════════════╬══════════╬════════════╣
║ 1. Docker for Mac ║ 1m41.02s ║ 2m45 ║
║ 1. Docker for Mac (optimised) ║ 0m12.980 ║ 1m02.9s ║
║ 2. VM Linux Docker on Mac ║ 0m6.1s ║ 0m56 ║
║ 2. VM Linux Docker on Mac (optimised)║ 0m12.980 ║ 1m02.9s ║
╚══════════════════════════════════════╩══════════╩════════════╝