Azure DevOpsDevOpsDocker

Running a self-hosted Azure DevOps Linux agent in Docker

azure devops agent logo

As you probably know, when queuing builds (Azure DevOps pipelines) on Azure DevOps, the build is delegated to the first available build agent in agent pool. These build agents can be Microsoft Hosted (Azure DevOps) or Self-hosted. First ones are cover majority of build scenarios, they are already prefabricated and ready to use in Azure DevOps. The latter ones are custom build agents and can be used if there are some special software needs (e.g. license, special software of some custom configurations, restricted on-premise resources, etc…) or special hardware or any other requirements.

If you read my blog post How to setup private Linux Azure DevOps Agent or How to setup private Mac Azure DevOps Agent you already know what I am talking/writing about. In those two blog posts I presented how to setup Linux or MacOs build agent on-premises. I quickly showed how to compile simple c code on a GCC compiler. You can check the demo code which I used for those two blog posts (and for this one also) code here: https://github.com/josipx/Jenx.LinuxAzureDevOps.Demo.

Similarly, I will show the same problem-solution in this blog post, but this time with Docker container. First of all, let’s check some documentation.

Documentation

Microsoft has awesome documentation and instructions how to host Linux Azure DevOps agent in Docker container is no exception. Thus, I just follow instructions in this link: https://docs.microsoft.com/en-us/azure/devops/pipelines/agents/docker?view=azure-devops#linux.

Let’s dig in

For this purpose, I will create custom Docker image from custom Dockerfile file. Therefore, I create empty folder with two files inside: Dockerfile and start.sh and copy following code (follow this link for more information).

This Dockerfile is basically the same as in official instructions, except I added two lines, i.e. lines 19 and 20. In my case, I need GCC compiler on my docker image to compile my awesome C application hosted on GitHub: https://github.com/josipx/Jenx.LinuxAzureDevOps.Demo.

start.sh script is the same as in official documentation. Just for consistency, I will copy content of this file here.

This script handles Docker container startup/bootstrap sequence. It handles input parameters on docker run command and all related startup tasks to start internal Azure DevOps agent.

Finally, when these two files are on place, I build docker container from Dockerfile and run container with correct parameters.

Building Linux Azure DevOps agent Docker image

I my console I hit this command in order to build docker image from Dockerfile:

docker build azure devops agent

As a result, on my local Docker I have newly created image!

docker image list

Get Azure DevOps ready to work with agent

Before starting of the my build agent docker container, I just need a few additional information from Azure DevOps, like:

  • Url to Azure DevOps root <AZP_URL>
  • PAT (Personal Access Token) <AZP_TOKEN>
  • Azure DevOps Pool name <AZP_POOL>

Let’s quickly show how can I get all these parameters. When I log into Azure DevOps portal first I select my project. This URL is the AZP_URL parameter.

how to get azure devops project url

Agent Pools and Agents are listed in Azure DevOps Settings page. I get information for AZP_POOL parameter here.

how to get agent pools in azure devops

Personal Access Tokens (AZP_TOKEN parameter) can be accessed/created in User Personal Profile page:

azure devops user profile
azure devops personal access token

After I have all startup parameters, I can start Docker container. I do this by running:

start azure devops linux image in docker

In my Azure DevOps agent listing, I have new build agent registered and ready to use, as seen below:

azure devops pool agents

So, my On-Premise-Docker-hosted agent is up, running and registered to Azure DevOps. Let’s test it by building my demo application. I select my testing build pipeline.

azure devops build pipeline

Pipeline tasks/sequence are: first, I get code from GitHub, compile it with GCC compiler and then I copy binaries to Artifacts repository. To build my app, I just need to Queue pipeline and Azure DevOps services will use first available agent (my Docker registered agent, because it’s the only active one in this pool) to build my app.

azure devops queue new build

My build went OK, so my output is all green:

azure devops build summary

I have binaries collected in my Artifact repository.

azure devops artifacts

If I run this binary on my Linux machine, I have this output.

compiled application in test

All good!

Conclusion

Docker containerization is relatively simple and efficient way to run applications. In this blog post I presented how to run Linux Azure Build agent in Docker container. I also presented how to run and to integrate agent into Azure DevOps. A demo app is also pulled from GitHub, compiled and binaries pushed into Azure Artifacts folder. This blog post and demo contains all crucial parts of simple building of application hosted on GitHub and build on on-premise build system hosted in Docker container.

Happy coding!

2 thoughts on “Running a self-hosted Azure DevOps Linux agent in Docker

  1. Thank you for sharing this article, my agent is running only one job, after that, it went idle o idle state and not picking any jobs.

  2. Hello, you did build a great article here have to say, I have a small issue with this VSTS agent containerization in Linux VM, I have written a Stackoverflow post with all the details that I’ll like to share with you in case you could help me with this, thanks in advance

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.