Configure an Azure DevOps self-hosted Windows agent in Docker

Author: Akhil M Anil || DevOps Engineer

An agent is computing infrastructure with installed agent software that runs job to perform a task/ a set of tasks.

There are different types of agents available for the users such as:

  • Microsoft Hosted Agents
  • Self-hosted Agents
  • Scale Set Agents

Self-hosted Agents

An agent that we set up and manage on our own to run jobs is a self-hosted agentSelf-hosted agents give us more control to install dependent software needed for our builds and deployments. Also, machine-level caches and configuration persist from run to run, which can boost speed. We can install the agent on Linux, macOS, or Windows machines. We can also install an agent on a Docker container.

This Article covers configuring an agent on a Docker container and use the agent to run job to perform tasks.

Run a self-hosted agent in Docker

We can set up a self-hosted agent in Azure Pipelines to run inside a Windows Server Core (for Windows hosts), or Ubuntu container (for Linux hosts) with Docker. This is useful when we want to run agents with outer orchestration, such as Azure Container Instances.

Both Windows and Linux are supported as container hosts. Windows containers should run on a Windows vmImage. To run our agent in Docker, we will need to pass a few environment variables to docker run, which configures the agent to connect to Azure Pipelines or Azure DevOps Server. Finally, we customize the container to suit our needs. Tasks and scripts might depend on specific tools being available on the container's PATH, and it's our responsibility to ensure that these tools are available.

In this article we will configure Windows agent in container host.

Steps:

  • Provisioning a Windows VM in Azure
    • Open portal.azure.com and click on resources. Select Virtual Machines. Here for this tutorial, I have used Windows Server 2019 Datacenter V2. 

  • Install Docker in Windows VM
    • Connect to VM through RDP.
    • For Installing docker in Windows VM, Open Powershell as administrator and run the following commands
      • Install-WindowsFeature containers -Restart
      • Install-Module -Name DockerMsftProvider -Repository PSGallery -Force
      • Install-Package -Name docker -ProviderName DockerMsftProvider
      • Restart-Computer -Force
      • Install-Package -Name Docker -ProviderName DockerMsftProvider -Update -Force
      • Start-Service Docker

  • Create a self-hosted Agent pool in ADO project
    • go to dev.azure.com and create a new project/use an existing one.
    • navigate to project settings and click on Agent pool.
    • click on Add pool and create a self-hosted agent pool.

  • Configure Agent in Docker container and add to Agent pool.
    • Create a new folder and open the folder in PowerShell.
    • Create a dockerfile, save the following content in the file with name Dockerfile(no extension needed) and save to the folder.
FROM mcr.microsoft.com/windows/servercore:ltsc2019

WORKDIR /azp

COPY start.ps1 .

CMD powershell .\start.ps1

    •  Create a PowerShell script file with the following contents in file with name start.ps1 and save to the same folder.
    • If we want to install software for the agent, we can install the software as well.
    • Here I will guide to install and configure Azure Cli and dotnet SDK and use the pipeline script for validation of these software in our docker agent.
    • Download Azure Cli and dotnet SDK and copy the exe/msi files to the folder which we already created.
    • Update the dockerfile with the following code: 

COPY azure-cli-2.41.0.msi .

RUN powershell -c Start-Process -Wait -FilePath '.\azure-cli-2.41.0.msi' -ArgumentList "/quiet" 

COPY dotnet-sdk-6.0.402-win-x64.exe .

RUN powershell -c Start-Process -Wait -FilePath '.\dotnet-sdk-6.0.402-win-x64.exe' -ArgumentList "/quiet"

    • The final Dockerfile will be: 

Find the full codes here: Azure-DevOps/docker-agent

    •  Now we need to build the dockerimage, for that run the following command in the same folder:
      • docker build -t dockeragent:latest .
    • This command builds the Dockerfile in the current directory.
    • The final image is tagged dockeragent:latest. We can easily run it in a container as dockeragent.

    • Now we are going to run the container, for the we need to have some parameters which are needed for auto configuring:
      • PAT token for auth
      • Agentpool name
      • Agent name
    • Run the following command to the container. This installs the latest version of the agent, configures it, and runs the agent.
      • docker run -e AZP_URL=<Azure DevOps instance> -e AZP_TOKEN=<PAT_Token> -e AZP_POOL=<Agentpool_Name> -e AZP_AGENT_NAME=<Agent_Name> dockeragent:latest

    • The agent is now configured, and we can see the agent in the agent pool.

    • Let's run a sample pipeline with the docker agent.


The pipeline is successful, and we can see the software are also detected!

Connect me via:

LinkedIn: Akhil M Anil | LinkedIn

References: 

Comments

Popular posts from this blog

Install Java silently using powershell in Azure Windows VM

List of Repositories - Authorize Rest API calls using Job Access Token