Running a VS Team Services (VSO) Build Agent in a Windows Docker Container

A while ago I wrote a post on how to run a Visual Studio Online (now called Visual Studio Team Services) Build Agent in a Docker Container on Linux. I got some good feedback on this post and now it is time to do the same trick again but only on Windows.

Yes ! You read it correctly, a Docker Container on Windows !.

Since Windows Server Core 2016 CTP 3, Docker is natively supported on the windows platform. We can run containers here the same way as we do on Linux.

I have already did some pre-blogposting before this post so this post is more a wrap-up to finally run the agent in a Docker Container. For reference please take a look at these blog posts I wrote before to get you up to speed.

Especially the post around Create a Windows Docker Host and connect to it without Visual Studio is important to read to get started.

Set up the build agent

After I had setup my Windows Docker Host in the cloud it was time to execute the same scenario as I did on  the Linux machine

  • Set up a Docker host
  • Create a Docker image with the Visual Studio Build Agent inside
  • Run the Docker container and automatically start the agent

A lot of work already has been done by finding out how to create a Docker Host and start an agent interactively, the challenge was, how can I put this in Docker container and make it reusable.

Create a Base Docker image

First we need to prepare some files to upload to our image. IN our case the VSTS build agent files. We download the zip from VSTS and extract it to a folder (e.g. c:\temp\docker\agent)

image

To create a windows based Docker image, we first create a Docker file that builds on the Windows Server Core base image. We also want the Docker image to contain some variables that contain information for our build agent. We use Environment variables for this purpose.

We do not want that our Docker image contains sensitive information so we do not put it in the real values. But if you do not provide A value for the environment variable in the Docker file, the Environment variable will not be created.

Our Docker file for our VSO base image looks like this

FROM windowsservercore

ENV vso_username=”user”
ENV vso_password=”password”
ENV vso_url=”
https://server.visualstudio.com”
ENV vso_agentname=$COMPUTERNAME
ENV vso_agentpool=default

COPY . /agent
WORKDIR /agent

This creates an image based on the windowsservercore base image, adds some environment variables and copies the contents of our agent directory into a directory [agent] in the docker container. As you can see we see [.] behind the copy command. This means the current directory. We need to run the Docker build command from within this dir. Save the Dockerfile in the Agent directory (in the github repo this file is called DockerfileBase, so rename first!(

Let’s now build this image so we can start using this image with some environment variables as our base image.

Open a command prompt and navigate to the [agent] directory and run the following Docker command

docker –tlsverify build  -t rvo/vsobuildagent:Base .

After this command an image with name rvo/vsobuildagent has been created. When we run

docker – tlsverify  images

image

Now we can create a second Docker image that builds upon our own image and installs and configures the build agent. Because we cannot run a COMMAND line using environment variables, I create a run.bat file that does exactly this. This file looks like this:

@echo off
SET url=%vso_url%
SET password=%vso_password%
SET username=%vso_username%
SET pool=%vso_agentpool%
SET agent=%vso_agentname%
echo URL [%url%]
echo Username [%username%]
echo Pool [%pool%]
echo Agent [%agent%]
.\agent\VsoAgent.exe /configure /serverUrl:%url% /name:%agent% /Poolname:%pool%
/Login:%username%,%password%;AuthType=Basic /force /workfolder:”c:\agent\_work”
/runningasservice:N /noprompt /force

This run.bat uses the environment variables and call the unattended install of the VSTS build agent as described in my earlier blogpost

Now, we should upload this run.bat file into a new image that builds upon our earlier created image. So create a Docker file that looks like this.and execute the docker build command

FROM rvo/vsobuildagent:Base
COPY run.bat /agent
WORKDIR /agent
ENTRYPOINT run.bat

Your directory in which you execute the Docker build command should look like this

image

then run

docker –tlsverify build –t rvo/vsobuildagent:v1 .

The container with this file is now also created.

Finally we should RUN the Docker container by sending the parameters we want. Simply run this command

docker –tlsverify run -t -i -e vso_username=username-e vso_password=password -e vso_url=https://accountname.visualstudio.com -e vso_agentname=agentname rvo/vsobuildagent:v1 cmd

This automatically starts up the agent!

image

Happy building !

You can find the sources for this post on my github repo

8 Responses to “Running a VS Team Services (VSO) Build Agent in a Windows Docker Container”

  1. Morten Østergaard February 17, 2016 at 8:58 am

    Link to “Unattended install of a Visual Studio Team Services (a.k.a. VSO) Build Agent” seems to be broken

  2. Are there any special network requirements when hosting a Build Agent in a Docker container using an on-prem TFS? I have followed your post but getting the following error:

    ErrorConnectingToTheServer
    VS30063: You are not authorized to access

    When running the same configuration command manually outside the container it works as expected.

    Cheers,
    Ryan

    • No, not that I know of. Can you Ping the TFS server from within the container ?

      • Thanks for the reply.

        Yes I’m able to ping the TFS server and can also do a invoke-webrequest via powershell with authentication successfully.

        I noticed when connecting to my own Visual Studio Online it authenticated properly.

        I decided to spawn another server and install TFS15 (instead of production TFS2015). With the new TFS15 agent and using PAT authentication I’m able to connect to the new TFS server.

        I still find it a bit weird but at least I’m able to continue.

      • Good to hear! Thanks for the update

Trackbacks/Pingbacks

  1. Visual Studio – Developer Top Ten for Feb 19th, 2016 - Dmitry Lyalin - February 19, 2016

    […] Running a VS Team Services (VSO) Build Agent in a Windows Docker Container by Rene van Osnabrugge […]

  2. Visual Studio – Developer Top Ten for Apr 25th, 2016 - Dmitry Lyalin - April 25, 2016

    […] Running a VS Team Services (VSO) Build Agent in a Windows Docker Container by Rene van Osnabrugge […]