Skip main navigation

How to Build, Push and Run Docker Images Using VSTS

.
17
Hi, this is Colin from Northwest Cadence. In this video, I’m going to show you how we can build and publish a .NET core application to a Docker container in our build that we can then run in a release. Here, I’ve opened up the build definition for my Docker test app. I have a couple of commands. The first one runs .net.restore. It does this in the context of the test application where my project lives. I then do the same thing. And this time, I do a .net.publish. And here, I’m specifying framework and runtime as well as the output directory to the published command. These are variables that I’ve set up on the build.
57.2
Here, you can see the runtime and framework variables that I’m using in order to run this application within a Docker container. You’ll see that we’re outputting the published application to the ArtifactsStaging Directory/Site.App. This folder is important for when we build the Docker image. And then I’m using a Docker task which I’ve installed from the Visual Studio marketplace in order to build the Docker image. I’ve had to create two service endpoints in order for this to work. One is to a Docker host which I’ve called cd-dockerhost. And one is to Docker registry. In this case, it’s my Docker registry in Docker Hub. You can read about how to set up these connections using this URL.
106
You’ll see under the Action parameter that there are several actions that this task can perform. In this case, we’re going to build an image. We specify the Docker file. In this case, I’m pointing to the released Docker file. I’m giving it an image name within my repository. And I’m using Build.BuildNumber as part of the tag. So each time I create a build, I’ll get a new image where the tag refers to the build number. Finally, I’m supplying the context. And this is the folder that contains the app. So if I go back to .net.publish, you’ll see we’re putting the application into an app folder. This app folder has been copied using the Copy command in the Docker file.
149.3
This will then build a Docker image for me. In the next step, I’m going to publish that to my Docker registry. So I’m using another Docker task. And this time, the action is Push an Image. I’m going to use the same image name so that it’s pushed to my repository with the same tag that I used when creating my image in the Docker build task. Finally, I’m using a Publish artefact to publish the ArtifactStagingDirectory directory so that I have my compiled code– in this case, the .NET core published application– outside of the Docker container. Of course, this is totally optional.
187.9
Here’s a previous build that ran successfully. You can see that we’ve successfully run .net.restore and .publish, and then built and pushed the image. If I go ahead and look at my repository in Docker Hub, you can see that I have tags for TestApp-1.0.18 as well as 1.0.19. The 1.0.19 tag was the result of the 1.0.19 build. Now that we have a build that’s pushed an image to a registry, let’s see how we can run that during a release.
221.2
In this release, I only have a single task. Again, it’s the Docker task from the marketplace. And this time, the action is Run an Image. I’m using the same endpoints to the Docker host and the Docker registry as we saw in the build definition. I’m also using the same image name. And I’m using, again, BuildABuildNumber to specify which build of this image I want to be running. I’ve linked that in the Artefact section where the DockerTestApp build is the Artefact for this release.
257.5
We can give a name to our container. We can specify any port mappings as well as any other environment variables. We can also supply additional commands for the container if we need to. Let’s go and see one of the previous releases. We can see release two completed successfully.
279.9
If we check the logs, we can see that the Run an Image task completed successfully. So let’s go and navigate to port 5000 on our Docker host. Here, we can see the template project that we’ve been working with running inside a container in our Docker host. In this video, I showed you an overview of how to build, push, and run Docker images using VSTS build and release management.
This article is from the free online

Microsoft Future Ready: Fundamentals of DevOps and Azure Pipeline

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education