VSTS agent & docker commands

Manuel Riezebosch
Jul 26, 2017 · 2 min read

After setting up our own VSTS agent with an on-premise TFS it is time to actually build something.

You can run docker tasks inside the agent-container by binding the docker sock from the host to the container.

docker run -ti --name build-agent -v /var/run/docker.sock:/var/run/docker.sock microsoft/vsts-agent:ubuntu-16.04-tfs-2017-u1-docker-17.03.0-ce-standard tail -f /dev/null

The nice thing is this even works with Docker for Windows, where strictly speaking the docker daemon is actually running inside a VM!

I have used the Docker Integration extension for adding docker tasks to my build definition.

Share data between the two containers

To share the data from the agent (e.g. sources) with the container you’re starting (to build, test, publish or whatever) from within that agent-container you’ll need a separate data volume container.

docker create -v /vsts/agent/_work --name build-agent-data vsts-agent:ubuntu-16.04-tfs-2017-u1-docker-17.03.0-ce-standard /bin/truedocker run -ti --name build-agent -v /var/run/docker.sock:/var/run/docker.sock --volumes-from build-agent-data microsoft/vsts-agent:ubuntu-16.04-tfs-2017-u1-docker-17.03.0-ce-standard tail -f /dev/null

Here I use the same image as the build agent (because image layers are shared).

Source: https://damnhandy.com/2016/03/06/creating-containerized-build-environments-with-the-jenkins-pipeline-plugin-and-docker-well-almost/

Execute a docker command

In your build definition you can now execute a docker command like this:

run --rm --volumes-from build-agent-data microsoft/dotnet:sdk dotnet restore $(Build.SourcesDirectory)

I could only use the “Run a Docker command” action because that only enables you to specify additional command options.

Word of warning

You should probably create separate data volume containers for each agent, or you modify the work directory and include the agent name. Otherwise two agents working on the same build definition simultaneously will interfere.

Manuel Riezebosch

Written by

Tweet, tweet, tweedle

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade