Docker Wine is a weird way to containerize and run Windows programs on Linux

Switching to Linux has long been regarded as a bold move within the tech community, often accompanied by the notion that straying from Windows and macOS could lead to significant sacrifices. While it may require an investment of time to configure Linux to one’s liking and identify the right distribution that offers the desired level of control, the lingering presence of Windows remains palpable. Many applications are tailored specifically for the dominant operating system, with some existing exclusively within its ecosystem.

The persistent incompatibility between Windows and Linux has posed challenges for both enthusiasts and professionals for decades. Linux simply does not natively understand Windows executables, rendering that .exe file in your Downloads folder unlaunchable at first glance. Traditionally, the solution has been Wine, a clever yet sometimes temperamental compatibility layer that converts Windows API calls into a language that the Linux kernel can comprehend. However, for those already utilizing Docker containers on their Linux systems, a more streamlined approach exists.

What does Docker Wine do?

A roundabout way to solve a specific situation

To run Windows applications on Linux, one could resort to the heavy-duty option of a Windows virtual machine, which inevitably compromises performance and consumes system resources. While this method guarantees results, Docker users have access to a more efficient alternative. Docker Wine is essentially a Docker image that comes pre-configured with a fully functional Wine setup. Unlike a standard Wine installation on your host machine, Docker Wine operates within a self-contained, isolated Linux environment, specifically designed to deploy a Windows compatibility layer.

This setup can be likened to a Russian nesting doll of operating systems, where your chosen Windows application operates within a Wine compatibility layer, all housed inside a Linux container that runs on your Linux host machine. It may seem complex, but for certain users, it’s an ideal solution. You might wonder how this differs from a conventional Wine installation using the classic sudo command; the answer lies in isolation. A standard Wine installation creates a directory in your home folder, functioning like a Windows boot drive. This directory accumulates various libraries, DLLs, and registry entries from every Windows program you attempt to run. If one application requires a specific library version that conflicts with another, you could face a significant headache.

Docker Wine cleverly avoids this pitfall. Each time you launch a Windows application using Docker Wine, you operate within a pristine container that disappears without leaving a trace once the application closes. The host system remains entirely unaffected; any changes made within the container are fully reversible, akin to a sandbox, and free from conflicts with other programs that may also rely on the Wine interface layer.

Docker Wine for GUIs

The most common use case

While the theory behind this setup is straightforward, practical application requires familiarity with both Linux and Docker. For instance, to run a GUI application like the classic Notepad++ installer, the command would differ from the straightforward wine npp.exe used in standard Wine installations. Instead, Docker Wine necessitates a more elaborate command:

docker run --rm 
-v /tmp/.X11-unix:/tmp/.X11-unix 
-v $HOME:/home/wine/host 
-e DISPLAY=$DISPLAY 
scottyhardy/docker-wine wine /home/wine/host/Downloads/npp.exe

This command initiates a container that is set to be removed once the application exits. The subsequent line establishes a connection between the host display server and the container, allowing for GUI interaction. The $HOME command mounts the Wine directory to the specified path, granting the containerized Wine access to your files, enabling you to open and save documents as if using a native application. The Docker image scottyhardy/docker-wine is specified, and the final command executes within the container, running the Notepad++ installer located in the mounted HOME directory. Consequently, Notepad++ and its dependencies are installed within a temporary Wine environment that exists solely for the duration of the container’s operation. Even if Wine is installed on your primary Linux machine, the local ~/.wine prefix remains undisturbed.

Docker Wine also works for command-line tools

For the proper Linux experience extension

This method of utilizing Wine within a container is equally effective for executing Windows-based command-line utilities. In this case, the setup does not require display server sharing, allowing for the operation of text-based file converters like Pandoc.

docker run --rm 
-v $PWD:/data 
scottyhardy/docker-wine wine /data/Pandoc.exe --input /data/file.in --output /data/file.out

This command mounts the current working directory ($PWD) into the container’s /data directory, offering a safer alternative to mounting the entire home directory, thereby limiting the container’s access to only the necessary files. The tool executes, performs the file conversion, and the container subsequently vanishes.

Winetricks for that secret sauce

For Windows apps that demand more

Many Windows applications rely on specific components such as .NET frameworks, fonts, or libraries, which are typically available in a standard Windows installation or virtual machine. To replicate this environment in Docker Wine, a Winetricks script is essential. Running Winetricks inside a container allows for the creation of a persistent Wine environment. This script can be executed when establishing a Docker volume to store the Wine prefix between sessions, resulting in a custom and persistent Wine environment that remains entirely isolated from the host system.

A niche use case

Docker Wine may appear as an unconventional solution to bridge the compatibility divide, seemingly tailored for Linux users who appreciate complexity for its own sake. While native Wine might be preferable for those requiring consistent access to the same Windows application, Docker Wine serves as a practical interim solution for occasional use without resorting to a virtual machine or a separate Windows installation. It seamlessly integrates into a container-based workflow, preserving the integrity of your development machine. For power users needing to test multiple Windows applications with conflicting dependencies, the ability to create a fresh, disposable Wine environment for each application proves invaluable. This tool caters to those who prioritize absolute isolation and reproducibility above all else.

Winsage