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.
- Get started with Docker on Azure for Microsoft developers and/or Linux noobs
- Unattended install of a Visual Studio Team Services (a.k.a. VSO) Build Agent
- Using OneGet PackageManager in Windows 10 and Windows 2016 Server Container
- Running a Visual Studio Build vNext agent in a Docker container
- Create a Windows Docker Host and connect to it without Visual Studio
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)
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
COPY . /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
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:
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
COPY run.bat /agent
Your directory in which you execute the Docker build command should look like this
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!
Happy building !
You can find the sources for this post on my github repo