Continuous Integration Using TeamCity And Docker For ASP.NET Core

Note

Please read the previous posts before continuing with this one. I have discussed the basics of Docker and shown how to deploy simple and multiple applications using Docker. In this post, I will use the code from the first post and demonstrate the continuous integration with TeamCity.

Problem

How to use Docker and TeamCity to implement continuous integration for ASP.NET Core applications.

Solution

First, we need to set up TeamCity. Since I like Docker, I’ll setup TeamCity within Docker too, using the below docker-compose.yml. Just run docker-compose up from the path where this compose file is located,

This will pull the images and start TeamCity on localhost:8111. Go through the simple steps to configure TeamCity.

Once TeamCity is running, configure an Agent. Click the “Agents” link (top menu), go to Unauthorized, click on the agent from a list, and click "Authorize Agent" button.

Next, create a new project by clicking Projects link (top menu).

Choose “From a repository URL” and paste the link into your repository.

Type the name for your build configuration. I’ve left it as default.

Next, you’ll be asked to configure the build steps. Click the link “configure build steps manually”.

Next, choose “Command Line” as Runner Type and add a custom script that uses Docker to build images.

Note

This assumes that the Docker file is in the root of solution folder. See the source code on GitHub for reference.

Save and click "Run" button on the top-right to test the build step.

Once the build is complete, you could view the logs.

Run the docker images command to view the image created by TeamCity.

Run a container based on this image.

docker run --rm -it -p 8080:80 naushadt25/fiver:ci-1  

Browse to http://localhost:8080/api/movies to test the application.

Now, make changes to your application and commit. TeamCity will kick off a new build.

And once the build is complete, you’ll have another image.

You now have multiple versions of your applications to test, run, and deploy. Below, I ran the two versions on two different ports.

When I was playing with this, I accidentally broke the tests for my application and TeamCity didn’t build (as a result, no image was created).

With a very little effort, we are able to package multiple versions of our application, thanks to Docker.

Note

For more complex scenarios, you could use a docker-compose.yml to run your applications rather than docker run command.

Source Code

GitHub