DEV Community

Unpublished Post. This URL is public but secret, so share at your own discretion.

Unlocking High Performance with VirtioFS in Docker Desktop

Docker Desktop is a popular tool used by developers to build and run containerized applications on their local machines. With the latest release of Docker Desktop, version 3.0, a new feature called VirtioFS was introduced. VirtioFS is a high-performance file system that can be used with Docker containers to improve their performance.

In this post, we will explore what VirtioFS is, how it works, and the benefits of using it with Docker Desktop.

What is VirtioFS?

VirtioFS is a file system designed to work with virtual machines (VMs) and containers. It is built on top of the Virtio virtualization framework, which is used in popular hypervisors like QEMU and KVM. VirtioFS allows virtual machines and containers to share a file system with the host operating system, without the need for a network file system (NFS) or a storage area network (SAN).

With VirtioFS, the file system is mounted on the host and then passed through to the VM or container as a Virtio device. This allows the VM or container to access the file system as if it were a local file system, with no network overhead.

Understanding common developer workflows

A common developer workflow when using technologies like Symfony or React is to edit source code located on the macOS host while running the app itself in a Docker container. The source code is shared between the host and the container by using Docker volumes, with a command like the following:

docker run -v /Users/me:/code -p 8080:8080 my-symfony-app
Enter fullscreen mode Exit fullscreen mode

This allows the developer to edit their source code, save the changes and immediately see the results in their browser. Changes made to files located on the host system must propagate quickly and reliably to the container file system for the developer to optimize productivity and have a good user experience. This is where file sharing performance is absolutely critical.

How does VirtioFS work in Docker Desktop?

Docker Desktop uses VirtioFS to mount the host file system in the container as a shared file system. This allows the container to access files on the host machine directly, without the need for a network share or a volume mount.

Image1

To use VirtioFS in Docker Desktop, you need to enable it in the settings. Once enabled, Docker Desktop will automatically detect if VirtioFS is supported by the host operating system and use it if possible.

Benefits of using VirtioFS with Docker Desktop

VirtioFS brings improved I/O performance for operations on bind mounts. Enabling VirtioFS will automatically enable Virtualization framework. Available in macOS 12.5 and above.
Developers now have the option of using a new experimental file sharing implementation called virtiofs (the current default is gRPC-FUSE). Secondly, improvements have been made to the way that files are synced between the macOS host and Docker VM. During testing with our amazing macOS community of users, we have observed that these changes have reduced the time taken to complete filesystem operations by up to 98%.

For developers, these incredible gains in speed mean less time waiting for filesystem operations to complete (or building project-specific workarounds to improve performance) and more time focusing on innovation!

There are several benefits to using VirtioFS with Docker Desktop. Here are some of the most important ones:

Improved performance

VirtioFS provides significant performance improvements over other file systems like NFS and SAN. This is because VirtioFS uses the Virtio framework, which is optimized for virtual environments. With VirtioFS, the file system is accessed directly from the container, without the need for network overhead.

Performance is vital when application source code is shared between the host and container. For example when a developer uses the Symfony PHP framework, edits the source code and then reloads the page in the browser, the web-server in the container must re-read many PHP files stored on the host. When considering that modern dependency management can easily bring 10k – 100k files into a project (which linearly increases the performance penalty), this can result in poor performance as the host and container keep in sync via the volume.

The recent changes to Docker Desktop for Mac, including the usage of virtiofs, alleviate this problem and bring drastic improvements to file system performance. Specifically, developers working with an early preview of Docker Desktop 4.6 with virtiofs enabled and changes to file syncing included have observed:

  • A 90% improvement in the time taken to complete a 284MB MySQL import (3m 16s to 18s)
  • An 87% improvement in the time taken to run ‘composer install’ in a large codebase (1m 27s to 11s)
  • An 80% improvement in the time taken to boot a monolithic Typescript app (1m 30s to 18s)

Reduced resource usage

Using VirtioFS reduces the need for additional resources like network bandwidth and storage. This is because the file system is shared between the host and the container, without the need for a network share or a volume mount.

Improved security

With VirtioFS, the file system is mounted on the host and then passed through to the container as a Virtio device. This provides an additional layer of security, as the container is not directly accessing the host file system.

Limitations of VirtioFS

While VirtioFS offers many benefits, there are also some limitations to consider. Here are some of the most important ones:

Limited operating system support

VirtioFS is not supported by all operating systems. Currently, it is only supported on Linux systems with a kernel version of 5.4 or later. If you are using an older version of Linux or a different operating system, VirtioFS may not be available.

Limited application support

Not all applications are designed to work with VirtioFS. While many applications will work without any issues, some may not be compatible with VirtioFS. If you are using an application that requires specific file system features, you may need to use a different file system.

Limited container compatibility

VirtioFS is not compatible with all container runtimes. Currently, it is only supported by the Docker container runtime. If you are using a different container runtime, you may need to use a different file system.

Potential stability issues

As with any new technology, there is always the potential for stability issues. While VirtioFS has been tested extensively and is generally considered stable, there may be some issues that arise in certain environments.

How to enable virtiofs

Virtiofs is only available to users of the following macOS versions:

  • macOS 12.2 and above (for Apple Silicon)
  • macOS 12.3 and above (for Intel)

Image3

To enable virtiofs in Docker Desktop:

Ensure that you are using Docker Desktop version 4.6+
Navigate to ‘Settings’ > General

You will see an option "Choose file sharing implementation for your containers" > select "VirtioFS"

Here's a sample code snippet that demonstrates how to use VirtioFS with Docker Desktop:

docker run -it --mount type=virtiofs,source=/path/to/host/files,target=/container/files alpine /bin/sh
Enter fullscreen mode Exit fullscreen mode

This command starts a Docker container and mounts the host file system at /path/to/host/files as a VirtioFS file system in the container at /container/files. The container is based on the Alpine Linux distribution and starts a shell (/bin/sh) inside the container.

You can modify the source and target paths to match the specific files and directories you want to share between the host and the container. Note that the type argument is set to virtiofs to enable VirtioFS.

With this simple code snippet, you can start experimenting with VirtioFS in Docker Desktop and see the benefits for yourself!

Conclusion

VirtioFS is a powerful new technology that offers significant performance improvements for Docker containers. With VirtioFS, containers can access files on the host machine directly, without the need for a network share or a volume mount. This reduces resource usage, improves performance, and enhances security.

While VirtioFS has some limitations, it is a promising technology that is likely to become more widely adopted in the coming years. As more operating systems and container runtimes support VirtioFS, it will become an increasingly important tool for developers and system administrators.

If you are using Docker Desktop, consider enabling VirtioFS to see the benefits for yourself. With VirtioFS, you can unlock new levels of performance and efficiency in your containerized applications.

Top comments (0)