This article is contributed. See the original author and article here.

How to deploy APIM self-hosted gateway in Windows Server 2019 for test purpose

 
APIM self-hosted gateway is packaged as a x86-64 Linux-based Docker container.

 

In Windows 10, if we have Docker Desktop installed, we can easily switch the Docker to Linux Mode so we can spin up linux containers.
docker_desktop.png

 

However, it takes a few more steps to run linux containers in windows server OS.

 

VM Limitation

We will need v3 series or above Azure VM to enable hyper-v feature. Here I am using D2s_V3 VM to do the demo.

 

Enable Hyper-V

First we need to install Hyper-V.
Run Powershell as administrator and run this command. The installation would ask for a reboot.
 
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
After reboot, run Powershell as administrator and run this command to check if Hyper-V is enabled. 
 
Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V
As you can see here, the state is Enabled.
 

hyperv_installed.png

 

Install Docker

Then we will install docker service.

 

To install Docker, we’ll use the OneGet provider PowerShell module. The provider will enable the containers feature on your machine and install Docker, which will require a reboot.

 

Run the below commands:
 
Install-Module -Name DockerMsftProvider -Repository PSGallery -Force
Install-Package -Name docker -ProviderName DockerMsftProvider
Restart-Computer -Force

 

After the reboot, run docker info to check if the docker is successfully installed.
 
docker info

 

Now let’s try to pull the self-hosted gateway image and run it.
 
docker run -it --rm mcr.microsoft.com/azure-api-management/gateway

 

The docker will complain the operating system does not match.
 
wrong_OS.png


Switch to Linux Mode

 

So we are going to switch the Docker to linux mode.
We will configure the docker to run in experimental mode.
Create or modify a json file at this path.
 
C:ProgramDatadockerconfigdaemon.json
 
We will add or change the ‘experimental’ attribute to true here.
{
  "experimental": true
}
 

experimented_json.png

 

Then we restart docker.
 
restart-service docker

 

Run docker info again, we can see that the LCOW setting is there.
 
docker_info_lcow.png

 

Finally we need to download LinuxKit from Github.
 
Invoke-Webrequest -UseBasicParsing -uri "https://github.com/linuxkit/lcow/releases/download/v4.14.35-v0.3.9/release.zip" -outfile ".release.zip"

 

Unzip it to this path.
 
Expand-Archive ".release.zip" -DestinationPath "C:Program FilesLinux Containers."
 

linux_containers_folder.png

 

 

 

Restart the docker service again.
 
restart-service docker

 

Try the linux container

 

The environment should be good to run linux container now. 
 
docker run -d --rm --name test-apim mcr.microsoft.com/azure-api-management/gateway
 
-d here means detach, which means running the container at the background. 

 

–rm means when we kill the container, docker will remove the container, just to avoid dangling containers here.

 

If we run a docker container list, we can see the self-hosted gateway container is running there.
 
docker container ls
 

run_success.png

 

Terminate the container

 

Run docker kill to terminate the container.
test-apim is the name we just gave this container using –name flag when we ran it.
 
docker kill test-apim
 

Actually Run it

Now we will connect this container to our APIM and make it an actual gateway.

 

We create an gateway in the Gateway blade of APIM Azure Portal.
 

create_gateway_here.png

 
Download the env.conf file from the just created gateway. Put it into the path that we are going to run the ‘docker run’ command.
 
get_env_file_new.png

 

 
docker run -d -p 80:8080 -p 443:8081 --name yixwan-self-hosted-gateway --env-file env.conf mcr.microsoft.com/azure-api-management/gateway
 
The -p here is publish(expose). It exposes container’s 8080 and 8081 port to the host machine’s 80 and 443 port. 

 

Once the container is up, any requests that hit the mapped port will go through this self-hosted gateway. 

 

Make sure in API setting that you have included your gateway.
 
api_setting.png

 

 

Now we will test use curl or any web request tool, we can send request to http://localhost:80 or https://localhost:443 to see if our self-hosted gateway is working.

 

Purpose

 

Currently the LinuxKit on Github is not being developed any more, so this is just a quick way to demonstrate that APIM self-hosted gateway can be deployed in windows server 2019.

Brought to you by Dr. Ware, Microsoft Office 365 Silver Partner, Charleston SC.