VSTS agent & docker commands

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/true
docker 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.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.