Post

Running a Windows 10 VM with Docker

Image

Virtualisation is an essential tool for developers and IT professionals, enabling the creation of isolated environments for testing, development, and running different operating systems. While Docker is traditionally associated with running Linux containers, it’s possible to run a Windows 10 virtual machine (VM) using Docker. In this blog, I’ll walk you through setting up a Windows VM using Docker and KVM, leveraging the docker-compose.yml file provided.

Prerequisites

Before you start, ensure you have the following installed on your system:

  • Docker: Docker needs to be installed and properly configured. Ensure your Docker installation supports KVM (Kernel-based Virtual Machine) if you’re using Linux.
  • Docker Compose: Docker Compose is necessary to run the docker-compose.yml file.
  • Sufficient System Resources: Make sure your system has enough resources to allocate to the VM, especially if you’re running other containers simultaneously.

The docker-compose.yml File Explained

Let’s break down the docker-compose.yml file that will help us run a Windows 10 VM:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
services:
  windows:
    image: dockurr/windows
    container_name: windows
    environment:
      VERSION: "win10"
      RAM_SIZE: "4G"
      CPU_CORES: "2"
      USERNAME: "User"
      PASSWORD: "password"
    devices:
      - /dev/kvm
    volumes:
      - ./storage:/storage
    cap_add:
      - NET_ADMIN
    ports:
      - 8006:8006
      - 3389:3389/tcp
      - 3389:3389/udp
    stop_grace_period: 2m
    restart: unless-stopped

Key Components

  • Image: The image directive specifies the Docker image to use. In this case, we are using dockurr/windows, a preconfigured image that supports running Windows.

  • Container Name: The container_name field names the container windows, making it easier to manage and reference.

  • Environment Variables:
    • VERSION: Specifies the version of Windows to run, which is set to “win10”.
    • RAM_SIZE: Allocates 4GB of RAM to the VM.
    • CPU_CORES: Assigns two CPU cores.
    • USERNAME and PASSWORD: Set the credentials for accessing the VM.
  • Devices: The /dev/kvm device is necessary for enabling hardware acceleration through KVM, which is crucial for running VMs efficiently.

  • Volumes: The volumes directive maps a local storage directory to the container’s storage, enabling persistent data storage.

  • Capabilities: The cap_add directive adds network administration capabilities to the container, allowing it to manage network interfaces.

  • Ports:
    • 8006:8006: Maps port 8006 for accessing the VM.
    • 3389:3389/tcp and 3389:3389/udp: Exposes the Remote Desktop Protocol (RDP) port for remote access to the VM.
  • Stop Grace Period: This sets a 2-minute grace period before the container is forcefully stopped, allowing the VM to shut down gracefully.

  • Restart Policy: The restart directive ensures the VM container restarts automatically unless stopped manually.

Setting Up the Windows 10 VM

Now that we understand the docker-compose.yml file, let’s get the VM up and running.

Step 1: Create a Directory for Your Project

First, create a directory to house your Docker project files:

1
2
mkdir windows-docker-vm
cd windows-docker-vm

Step 2: Create the docker-compose.yml File

In your project directory, create the docker-compose.yml file:

1
vi docker-compose.yml

Paste the above configuration into this file and save it.

Step 3: Run Docker Compose

With your configuration ready, it’s time to start the VM:

1
docker-compose up -d

The -d flag runs the container in detached mode, allowing you to continue using your terminal.

Step 4: Accessing the Windows 10 VM

Once the container is running, you can access the VM using Remote Desktop Protocol (RDP):

  1. Open your preferred RDP client.
  2. Connect to localhost:3389.
  3. Use the username and password specified in the docker-compose.yml file (User and password respectively).

Step 5: Managing the VM

You can manage the VM container with the following Docker commands:

Stop the VM:

1
docker-compose down

Restart the VM:

1
docker-compose restart

View Container Logs:

1
docker logs windows_10_01

Conclusion

Running a Windows 10 VM using Docker is a practical solution for testing and development purposes, especially when you need an isolated environment without setting up a full-fledged hypervisor. The docker-compose.yml file simplifies the process, making it easy to configure and manage your VM.

I hope this guide has been helpful. Feel free to explore further customisation options and adapt the configuration to your specific needs. Happy virtualising!

This post is licensed under CC BY 4.0 by the author.