# MongoDB Cluster Docker Compose on Windows WSL2: Setup Guide and Limitations

> Set up a MongoDB cluster with Docker Compose on Windows WSL2. Learn about essential limitations and avoid common storage issues for a smooth deployment.

- Repository: [Jin/mongodb-cluster-docker-compose](https://github.com/minhhungit/mongodb-cluster-docker-compose)
- Tags: how-to-guide
- Published: 2026-03-07

---

**Running the minhhungit/mongodb-cluster-docker-compose setup on Windows requires WSL2 backend enabled in Docker Desktop to avoid VirtualBox shared-folder limitations that break MongoDB's memory-mapped storage.**

Deploying a sharded MongoDB cluster using Docker Compose on Windows presents unique challenges due to filesystem and virtualization differences. The `minhhungit/mongodb-cluster-docker-compose` repository provides a fully automated sharded cluster configuration, but Windows users must navigate specific compatibility layers to ensure the cluster initializes correctly.

## Why WSL2 Matters for MongoDB Cluster Docker Compose on Windows

MongoDB relies heavily on memory-mapped files for storage operations, which creates specific constraints when running inside Docker containers on Windows hosts.

### The VirtualBox Shared-Folder Problem

Older Docker Desktop installations on Windows used VirtualBox as the virtualization backend. VirtualBox shares folders with the host using its "shared folders" mechanism, which **cannot handle MongoDB's memory-mapped files**. When containers attempt to map their data directories to the Windows filesystem through VirtualBox, the cluster fails to start or exhibits storage-related errors such as `fsync on directories` failures.

As documented in the repository's [`readme.md`](https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/main/readme.md) (lines 36-45), this limitation prevents the containers from properly mounting data directories when using VirtualBox-based Docker Desktop.

### How WSL2 Solves the Storage Issue

WSL2 runs a lightweight Linux kernel inside a Hyper-V VM and provides a true Linux filesystem. Docker Desktop can **directly use the WSL2 distribution**, eliminating the VirtualBox sharing problem entirely. When using the WSL2 backend, MongoDB's memory-mapped files work correctly because the containers access an ext4 filesystem rather than a VirtualBox shared folder.

## Configuring Docker Desktop for WSL2 Integration

To run the MongoDB cluster Docker Compose setup on Windows, you must enable WSL2 integration in Docker Desktop.

Enable WSL2 integration through the Docker Desktop interface:

1. Open **Docker Desktop → Settings → Resources → WSL Integration**
2. Enable integration for your preferred WSL2 distribution
3. Click **Apply & Restart**

The repository documentation references this configuration in [`readme.md`](https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/main/readme.md) (lines 27-31), noting that this step is essential for Windows users.

### Resource Allocation Considerations

A sharded MongoDB cluster with three replica sets plus config servers consumes considerable RAM. Adjust Docker Desktop's resource allocation to avoid out-of-memory kills:

- Navigate to **Settings → Resources**
- Allocate at least **4-6 GB of RAM** minimum (8 GB recommended for production-like workloads)
- Ensure sufficient CPU cores (2-4 cores recommended)

## Critical Windows-Specific Considerations

Beyond the virtualization backend, Windows presents additional compatibility challenges for the MongoDB cluster setup.

### Line Ending Incompatibilities

Scripts in the `scripts/` directory may be saved with Windows CRLF line endings when cloned on Windows. MongoDB's shell cannot parse these files, resulting in "syntax error: unterminated string literal" errors during initialization.

Convert script files to Unix LF line endings before running the cluster:

**Using Notepad++:**
1. Open the script file
2. Navigate to **Edit → EOL Conversion → Unix (LF)**
3. Save the file

**Using PowerShell:**

```powershell

# Convert CRLF to LF for all .sh files recursively

Get-ChildItem -Recurse -Filter *.sh | ForEach-Object {
    (Get-Content $_ -Raw) -replace "`r`n","`n" | Set-Content $_ -NoNewline
}

```

The repository README documents this issue with an illustration in [`readme.md`](https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/main/readme.md) (lines 49-53).

### File Paths and Scripts

The [`docker-compose.yml`](https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/main/docker-compose.yml) file mounts the `scripts/` directory into containers. When using WSL2, ensure you run Docker Compose commands from within the WSL2 filesystem (e.g., `/home/user/project`) rather than the Windows mounted path (`/mnt/c/...`) for optimal performance, though the cluster will function from either location when WSL2 integration is enabled.

## Step-by-Step Deployment on Windows WSL2

Follow this workflow to deploy the MongoDB cluster on Windows with WSL2:

1. **Enable WSL2 integration** in Docker Desktop (Settings → Resources → WSL Integration)

2. **Clone the repository** into your WSL2 filesystem:
   ```bash
   git clone https://github.com/minhhungit/mongodb-cluster-docker-compose.git
   cd mongodb-cluster-docker-compose
   ```

3. **Fix line endings** (if you cloned using Windows Git):
   ```powershell
   # Run from PowerShell in the project directory

   Get-ChildItem -Recurse -Filter *.sh | ForEach-Object {
       (Get-Content $_ -Raw) -replace "`r`n","`n" | Set-Content $_ -NoNewline
   }
   ```

4. **Start the cluster**:
   ```bash
   docker-compose up -d
   ```

5. **Verify the router is ready**:
   ```bash
   docker exec -it router-01 bash -c "
     until mongosh --port 27017 --eval 'db.adminCommand({ping:1})' &> /dev/null; do
       echo 'Waiting for mongos...'; sleep 5;
     done
     echo 'Mongos is up – sh.status():';
     mongosh --port 27017 --eval 'sh.status()'
   "
   ```

The [`scripts/entrypoint-route.sh`](https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/main/scripts/entrypoint-route.sh) file handles the initialization logic, waiting for replica set primaries before launching the `mongos` router and adding shards to the cluster.

## Summary

- **WSL2 is required** for MongoDB cluster Docker Compose on Windows because VirtualBox shared folders cannot handle MongoDB's memory-mapped files.
- **Enable WSL2 integration** in Docker Desktop (Settings → Resources → WSL Integration) before deploying.
- **Convert line endings** from CRLF to LF for all `.sh` scripts in the `scripts/` directory to prevent MongoDB shell syntax errors.
- **Allocate sufficient resources** (4-6 GB RAM minimum) in Docker Desktop to prevent out-of-memory kills of shard and config server containers.
- **Key files**: [`docker-compose.yml`](https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/main/docker-compose.yml) defines the services, [`scripts/entrypoint-route.sh`](https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/main/scripts/entrypoint-route.sh) handles router initialization, and [`readme.md`](https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/main/readme.md) contains Windows-specific warnings.

## Frequently Asked Questions

### Can I run the MongoDB cluster on Windows without WSL2?

No, you cannot reliably run this MongoDB cluster Docker Compose setup on Windows without WSL2. Older Docker Desktop versions using VirtualBox cannot handle MongoDB's memory-mapped files through shared folders, causing storage errors and startup failures. WSL2 provides a true Linux kernel with a compatible filesystem that supports MongoDB's storage requirements.

### How do I fix "syntax error: unterminated string literal" when running the init scripts?

This error occurs when shell scripts have Windows CRLF line endings instead of Unix LF endings. Convert the files using Notepad++ (Edit → EOL Conversion → Unix) or PowerShell: `Get-ChildItem -Recurse -Filter *.sh | ForEach-Object { (Get-Content $_ -Raw) -replace "`r`n","`n" | Set-Content $_ -NoNewline }`. The scripts in the `scripts/` directory must use LF endings for MongoDB's shell to parse them correctly.

### What resource allocation should I set in Docker Desktop for this cluster?

Allocate at least 4-6 GB of RAM (8 GB recommended) and 2-4 CPU cores in Docker Desktop Settings → Resources. The sharded cluster runs multiple replica sets, config servers, and router containers simultaneously, consuming significant memory. Insufficient resources cause containers to exit with out-of-memory errors during shard initialization or under load.

### Where are the key configuration files located in the repository?

The main orchestration file is [`docker-compose.yml`](https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/main/docker-compose.yml) at the repository root, defining all services including routers, config servers, and shards. The `scripts/` directory contains initialization logic: [`entrypoint-route.sh`](https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/main/entrypoint-route.sh) handles router startup and shard registration, while `entrypoint-shard*.sh` files initialize individual replica sets. Windows-specific setup instructions and WSL2 configuration guidance are documented in [`readme.md`](https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/main/readme.md).