Running Azurite emulator in the background on Windows startup
Tags: windows, azurite, programming
Published: 2023-04-21 19:55:41

Azurite is an open-source emulator that enables you to test your applications using Azure Blob, Queue Storage, and Table Storage in a local environment without any cost. 
Azurite replaces the [Azure Storage Emulator](https://learn.microsoft.com/en-us/azure/storage/common/storage-use-emulator) and is regularly updated to ensure support for the latest versions of the Azure Storage APIs.

However, Azurite has one significant drawback compared to the outdated Azure Storage Emulator - it cannot be run in the background without keeping a console/terminal window open at all times.
The obsolete Azure Storage Emulator could be run in the background without opening a new window by running it from the command line:
```
"C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" start
```
You could create a `start-azure-storage-emulator.cmd` script and put the command inside that script, then copy/move it to the Windows Startup folder and voila - the emulator was running in the background and you could forget about it. 
Unfortunately **this is not possible for Azurite** 😥


## Options for starting the Azurite emulator

### Option 1 - use Docker
One way is to use Docker and run Azurite inside docker:
```bash
docker run -d --restart always --name azurite -p 127.0.0.1:10000:10000 -p 127.0.0.1:10001:10001 -p 127.0.0.1:10002:10002 -v c:/dev/data/Azurite:/workspace mcr.microsoft.com/azure-storage/azurite azurite -l /workspace --blobHost 0.0.0.0 --blobPort 10000 --queueHost 0.0.0.0 --queuePort 10001 --tableHost 0.0.0.0 --tablePort 10002
```
The important thing here is to bind the volume: `-v c:/dev/data/Azurite:/workspace` where the data is stored, so that it is not lost when the container or Docker is stopped.

But sometimes you can't or don't want to use Docker... What options do you have then? 

### Option 2 - Run Azurite manually 
Run Azurite whenever you need it and keep the console window open all the time.

```batch
azurite -l "C:\dev\data\Azurite" --blobHost 0.0.0.0 --blobPort 10000 --queueHost 0.0.0.0 --queuePort 10001 --tableHost 0.0.0.0 --tablePort 10002
```

![azurite - run manually output log](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1gixj5aenm8r5xud5qh8.png)


### Option 3 - Adding Azurite to the Windows startup
This is the option I want to focus on in this article.

#### Installation
```bash
npm install -g azurite
```

#### Create batch script
Create a `C:\dev\scripts\azurite-startup.cmd` file, open it in notepad and paste the following content:
```batch
start /B /MIN azurite -l "C:\dev\data\Azurite" --blobHost 0.0.0.0 --blobPort 10000 --queueHost 0.0.0.0 --queuePort 10001 --tableHost 0.0.0.0 --tablePort 10002 > "C:\dev\data\Azurite\azurite.log" 2>&1
```
The `-l, --location` option is important here - this is the location of the workspace, where Azurite will store all data.

`> "C:\dev\data\Azurite\azurite.log" 2>&1` means you want to redirect standard output and errors to the specified file.

You don't need to specify the `*Host` and `*Port` parameters if you don't want to change the default hosts and ports.

#### Create VBScript

Create a `C:\dev\scripts\azurite-win-autostart.vbs` file, open it in notepad and paste the following content:
```vb
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\dev\scripts\azurite-startup.cmd" & Chr(34), 0
Set WshShell = Nothing
```

#### Windows auto-startup

To run the VBScript file on startup, place it (or it's shortcut `right click -> Create shortcut`) in the **Windows Startup** folder. To do this, follow these steps:

1. Press the `Win + R` to open the Run dialog box
2. Type `shell:startup` and press Enter
3. Copy the VBScript file you created earlier into the Startup folder.

Restart your computer and the batch script should start without a console window.

#### Find running Azurite process(es)
If Azurite is running in the background, you won't be able to find or stop the process easily. 

You can find it using a PowerShell command:

```powershell
Get-Process -name "cmd","node" -ErrorAction Ignore | Where-Object { $_.CommandLine -like '*azurite*' -and $_.CommandLine -like '*--blobPort*' }
```

An even better way is to create functions and/or aliases. 

Open the PowerShell $PROFILE file in Notepad: `notepad $PROFILE` and paste the following content at the end:

```powershell
function get-azurite-processes { Get-Process -name "cmd","node" -ErrorAction Ignore | Where-Object { $_.CommandLine -like '*azurite*' -and $_.CommandLine -like '*--blobPort*' } }

Set-Alias -Name azp -Value get-azurite-processes -Description "Get Azurite background processess (node and cmd)"

function stop-azurite-processes { get-azurite-processes | Stop-Process }
```

This will find the `cmd` and `node` processes responsible for running Azurite.

![get-azurite-processes powershell function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7tamuyvwh30w51qx6ek3.png)

To stop Azurite, you can run the function you created in the previous step: 
```powershell
stop-azurite-processes
```