Windows VM Inside a Linux Docker Container
Tags: devops, windows, cicd, docker
Published: 2023-10-21 13:05:27

When it comes to creating reproducible and isolated development environments, two leading technologies reign: Docker and Vagrant. Docker facilitates containerization by bundling applications and dependencies together, making the applications platform-independent. On the other hand, Vagrant is a tool for virtual environments, allowing the creation and management of virtual machines (VMs). Running a Vagrant VM, particularly a Windows VM, inside a Docker container creates a unique combination that can offer remarkable benefits, especially in the field of Continuous Integration (CI) and Continuous Deployment (CD). :repeat_one: :rocket:

Today, we'll tap into an existing GitHub repository that demonstrates how to implement a Windows VM within this Docker/Vagrant setup. The repository can be found at the following URL: [https://github.com/vaggeliskls/windows-in-docker-container](https://github.com/vaggeliskls/windows-in-docker-container)

## Prerequisites :bookmark_tabs:
Before proceeding, the following tools should be installed on your system:

- [Docker](https://docs.docker.com/engine/install/)
- [Docker-compose] (https://docs.docker.com/compose/install/)

## How to Use It :hammer_and_wrench:

1. Create an environmental file **.env** with VMs specifications 

```
# Vagrant image settings
MEMORY=8000 # 8GB
CPU=4
DISK_SIZE=100
```

2. Create a `docker-compose.yml` file

```
version: "3.9"

services:
  win10:
    image: ghcr.io/vaggeliskls/windows-in-docker-container:latest
    env_file: .env
    stdin_open: true
    tty: true
    privileged: true
    cap_add:
      - NET_ADMIN
      - SYS_ADMIN
    volumes:
      - /sys/fs/cgroup:/sys/fs/cgroup
    devices:
      - /dev/kvm
      - /dev/net/tun
    ports:
      - 3389:3389
```
3. Run: `docker-compose up -d`

![Windows vm screenshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mkvvl922ql58nox2sge9.png)

## Remote Desktop :desktop_computer:
For debugging purposes or testing, you can always connect to the VM with remote desktop software products. Some software that used when developed was:

- Linux: [rdesktop](http://www.rdesktop.org/) IP:3389 or [remina](https://remmina.org/)
- macOS: [Windows remote desktop](https://apps.apple.com/fr/app/microsoft-remote-desktop/id1295203466?mt=12)
- Windows: build in Remote Windows Connection

## User login :bust_in_silhouette:
The default users based on vagrant image are

- Administrator
 - Username: Administrator
 - Password: vagrant
- User
 - Username: vagrant
 - Password: vagrant

## Conclusion :wave:
Running a Vagrant Windows VM inside a Docker container merges the benefits of Docker's platform-independent containerization with Vagrant's controlled, easy-to-manage development environments. Although such a setup won't fulfill every use case, understanding how Docker and Vagrant operate and interact is highly beneficial for DevOps professionals.

Remember, different configurations exist and this setup can be tweaked to suit specific application requirements. Be aware that running a VM inside a Docker container can be resource-intensive, so always check your system's specifications before proceeding. :memo:

Happy coding!

## References :books:
- [Windows Vagrant Tutorial](https://github.com/SecurityWeekly/vulhub-lab)
- [Vagrant image: peru/windows-server-2022-standard-x64-eval](https://app.vagrantup.com/peru/boxes/windows-server-2022-standard-x64-eval)
- [Vagrant by HashiCorp](https://www.vagrantup.com/)
- [Windows Virtual Machine in a Linux Docker Container](https://medium.com/axon-technologies/installing-a-windows-virtual-machine-in-a-linux-docker-container-c78e4c3f9ba1)
- [Windows GitHub action runner](https://github.com/vaggeliskls/windows-github-custom-runner)
- [Windows in container](https://github.com/vaggeliskls/windows-in-docker-container)