Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Docker Desktop Best Practices for Code Sharing

Docker Desktop Best Practices for Code Sharing

This item in japanese

Lire ce contenu en français

In a recent article, Docker engineer Stephen Turner shared a few best practices to help developers understand file sharing between a Docker container and its local host and how its performance varies across OSes.

Docker Desktop provides two mechanisms to share a part of the local file system with a container, namely, bind mount and named volumes. Additionally, you can copy a part of your file system inside the container's own file system.

Each approach serves the same goal of sharing files between the local file system and the container's (as well as among distinct containers'). However, each mechanism comes with its own balance of pros and cons which makes them suitable for different use cases.

Copying files to a container has the major drawback that all data copied over, including any modification to it, will be lost when the container is removed. This is definitely not a desired outcome if you want to share code your need to edit.

As to the other two options, as Turner explains, bind mounts are the most flexible solution, but their performance is inferior to shared volumes.

[...] it’s in the nature of virtualization that there is always an unavoidable small overhead in crossing the host-VM boundary. It’s only tiny, but in a development environment with a huge source tree and lots of reads and writes, it adds up, and can visibly affect performance.

It is important to notice that this issue only exists on macOS and Windows machines running Docker, since Linux has direct access to the bind-mounted file system.

Based on this, Turner suggests a few criteria to understand when to use bind mounts or shared volumes.

Bind mounts are definitely the way to go when you want to share code that you need to edit. They could be a problem, though, for very large repositories where you have to carry through a high number of read/write operations. In these cases, you could give a try to mirroring, using third-party solutions such as Mutagen and docker-sync.

Bind mounts, on the other hand, are definitely not a good option for things like databases, dependencies trees or libraries, caches and log files. For all those cases, you will be better off using shared volumes.

This state of things could change, says Turner, thanks to Virtiofs, which is specifically designed for sharing files between a host and a VM. Virtiofs is already available in a Docker Desktop preview for macOS.

Turner's article contains many more details than can be summarized here, so do not miss if you are interested.

About the Author

Rate this Article