Containers are brilliant in that they provide a rather lightweight mechanism of setting up desired software in your build agent without installing it on host machine or in a virtual machine. In this post, I will explain setting up a Docker container on a Windows 10 machine, install all the desired software for build agent and running a build to compile an ASP.net core application on it.
This post is split into following areas
- Setting up Docker on a Windows 10 machine
- Create a Docker image to run Build Agent.
- Configure and run a Build Agent on Docker.
- Run a Build on the newly setup Build Agent.
Set up a Docker container on a Windows 10 machine
docker run -it hello-world
Create a Docker image to run Build Agent
docker run Ubuntu:16.04.It would look for the instance locally and if it's not found download it from Docker hub. Once the command is complete, the Docker Ubuntu 16.04 image will appear in your “docker images” command
We now install Node.js, npm and vso agent onto the image and store it as another image. To do that the first step is to run the Ubuntu:16.04 image as a container. To do that, run the following command.
docker run -t -i Ubuntu:16.04You will now be on command prompt of root. To run the desire software, run the following commands one after another
apt-get updateMy Ubuntu 16.04 instance didn't have https protocol installed, so installed it by running
apt-get install npm nodejs
apt-get install nodejs-legacy
npm install vsoagent-installer –g
apt-get install apt-transport--httpsWe will also need to install git on it
apt-get install git
sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ xenial main" > /etc/apt/sources.list.d/dotnetdev.list'
apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
apt-get install dotnet-dev-1.0.0-preview2-003131At this stage, the container contains all the software that you need to run a vso build agent.
Configure and run Build Agent
The only thing left to do now is to configure the build agent. The agent installer allows us to easily create multiple agents on a single server. Each agent will run inside its own folder. We create a folder called MyBuildAgent1 and uses it to run the agent. To do this run the following commands
mkdir MyBuildAgent1All commands so far has been run using the root user. We don't want to run the build agent to run as root, so will create a user called buildagentuser and switch to it to use it. Below are the command that you need to run.
chown -R buildagentuser /MyBuildAgent1
The build agent is almost ready to run. At this stage we want to commit this container to save the image. This way we can use it again.
Step out of Docker container by pressing CTRL+P followed by CTRL+Q. You will be back to command prompt. Now type in "docker ps" to view the list of containers running. You will see a response like following
Here, the ubuntu:16.04 running container has the Id "aea7e12541d5". We will commit this container to create a new image. To do this, type in the following on your command prompt
docker commit -a "Hamid Shahid" -m "Basic vso .net core build agent" aea7e12541d5 basicvsogent:v1If you are following instructions, please use your container id. You can verify it by running "docker images". You will see the new image in the list of images
Since, we just "stepped out" of Docker container we were working now, we will now reattach it and run the build agent. To do this type in
docker attach aea7e12541d5Now start the build agent by running
node agent/vsoagentYou will be prompted about your VSO url and the credentials to connect to. I used the following options in the prompt
The user you specify must have the service "Service Account" role in the agent pool you specified. In my case, I had added them to the VSO group "Agent Pool 1 Service Account Users".
Now that my build agent is now running, I will create now create a simple build definition to run the build. We had already created a simple .Net core Asp.net application.
Run a build on the new build agent
This is so awesome. Now that we have the Docker image captured as well, we can start other build agents and distribute our build load across containers. This is far more efficient than running virtual machines as build agents.