Continuous Integration and Deployment of ASP.Net Core 1.0 Docker Image to Azure Ubuntu Virtual Machine using GitHub and Docker Cloud

In this tutorial, we are going to see how to get continuous integration and continuous deployment of a Docker image created for ASP.Net Core 1.0 Application to an Azure Ubuntu Virtual Machine. We are going to use GitHub as version control repository where our application code along with Docker file resides. We will use Docker Cloud to automatically build images on every code push to the GitHub repository. Finally we will configure Docker cloud to build the latest image and deploy the Docker container to an Azure Ubuntu Virtual Machine.

To get started I have setup following development environment.

  1. Microsoft Windows 10 Enterprise N (10.0.10586 Build 10586) x64
  2. Visual Studio Community 2017 RC 15.0.26020.0
  3. Dotnet Version: 1.0.0-preview4-004233 (this SDK is based on MSBuild, so no Project.json in the created application)
  4. Docker tools for Windows https://docs.docker.com/docker-for-windows/.

Download and Install Git SCM Tools for Windows. Create a GitHub repository as show below.

image

image

Lets do a Git clone to map remote repository to local machine.

Git clone https://github.com/DreamingDevs/DockerDotnetCoreApp.git

image

Create a new ASP.Net Core 1.0 Application using following commands.

dotnet new -t web

dotnet restore

Add the following code to _Layout.cshtml to display the description of OS it is running at runtime.

<p>@System.Runtime.InteropServices.RuntimeInformation.OSDescription</p>

Build and run the application by running following commands.

dotnet build

dotnet run

image

Create a Docker file (without extension) at the root of the project (where project.json is located). Make sure the file name is Dockerfile.

NOTE: The approach of creating a Docker Image (with a base image defined in Docker file) which is demonstrated below is much better than the one which was demonstrated by me in other tutorial (Create and Deploy Docker Container of ASP.Net Core 1.0 Application to Azure Linux Web Application). Below approach will give the ability to build and run the ASP.Net Core code at runtime, while the previous approach works with pre-build binaries which are already available. Having said the pros and cons, both approaches can be used subjectively depending on requirements.

Below Dockerfile will use the microsoft/dotnet:1.0-sdk-msbuild base image from Docker hub and copies all the contents to the working directory /app. Then dotnet restore and dotnet build commands will be executed. 5000 port of the container will be exposed and configures the ASP.Net Core application to run under 5000 port. Finally it executes dotnet run.

FROM microsoft/dotnet:1.0-sdk-msbuild

WORKDIR /app
COPY . /app

RUN [“dotnet”, “restore”]
RUN [“dotnet”, “build”]

EXPOSE 5000/tcp
ENV ASPNETCORE_URLS
http://*:5000

ENTRYPOINT [“dotnet”, “run”]

image

docker build . -t dockerdotnetcoreapp

image

It will restore all the dependencies and then continues to run the ASP.Net Core Application as shown below. Do not worry about the warning related to PackageReference (as part of VS 2017, we will have a meta package reference for Microsoft.NETCore.App in csproj file under package references section, which is not required).

image

Run the image as shown below.

docker run –d –p 8080:5000 –t dockerdotnetcoreapp

image

image

Lets commit the code to GitHub repository.

git status

git add .

git status

image

Commit and push files to GitHub using following commands.

git commit –m “Initial”

git push origin master

image

image

Now we should have all code in GitHub as shown below.

image

Now lets login to https://cloud.docker.com/ and navigate to Nodes section.

image

Click on Bring your own node. Docker cloud is capable of building images and creating containers in its own cloud. But for this tutorial we are going to use Azure Ubuntu Virtual Machine host the Docker Container.

image

Keep this browser window as – is. Now lets create a Ubuntu Virtual Machine in Azure portal and open the ports which are required by Docker (Check above image, its says We recommend you open incoming port 2375 in your firewall for Docker Cloud to communicate with the Docker daemon running in the node. For the overlay network to work, you must open port 6783/tcp and 6783/udp). Select Ubuntu Server 16.04 LTS on Azure portal.

image

Click Create. Fill in the required details like Name. Select Password as the Authentication Type.

image

For Size, I have selected DS1_V2 Standard 1 Core 3.5GB RAM. I left out all the other default settings and created the virtual machine.

The overview of the Virtual Machine displays the IP Address, we need this IP Address to connect to the VM.

image

we need to add an inbound rules to configure 2375, 6783/tcp, 6783/udp and 80 ports. Find the Network Security Group.

image

Click on the Network Security Group (associated with our newly created virtual machine), Select Inbound Security Rules. Click Add to add a new Inbound Rule.

image

Now add ports as shown below.

image

image

image

Add 80 port as well which will allow us to browse deployed application from Virtual Machine.

image

Now download and install Putty SSH and Telnet client on your local environment. Run Putty Client and enter the VM IP Address (can be found from overview tab of virtual machine) as shown below and click Connect.

image

We will get an alert to cache the server’s host key. Click No.

image

Login to the console by providing the username and password (which we provided at the time of VM creation).

image

Now lets install Docker Cloud agent on VM. First lets become root on VM.

sudo –s

image

Now run the command which we got from Docker cloud when we tried to bring our own node.

curl -Ls https://get.cloud.docker.com/ | sudo -H sh –s Our-Hash

image

It will download and install Docker Cloud Agent. Once completed we will have following message.

image

Now we will create an association from Docker cloud to GitHub. Go to Cloud Settings tab at Docker Cloud portal.

image

Click on Connect icon (as shown above). It will redirect to GitHub portal for authorization, provide password and get authorized.

Now lets create a repository at Docker cloud and connect it with GitHub. Navigate to Repositories tab.

image

Enter the name of Docker repository and select the GitHub repository, finally click Create.

image

After successful repository creation, we will configure automated builds to be deployed on Azure Ubuntu Virtual Machine. Navigate to Builds tab.

image

Click on Configure Automated Builds. Select Build Location to Build on my own nodes. Click Save and Build.

image

On Successful build we will have following information (below images show my previous experimental builds, please ignore the failed and cancelled builds).

image

image

On successful build, We should find the Docker image in our Docker hub account.

image

Now we will create a Service in Docker Cloud which will hold the container created from our Docker image.

image

Click Create. Select the image which we created as shown below.

image

In the general settings, we need to enable AUTOREDEPLOY and add 80 port to the machine.

image

image

Now click on Create and Deploy. It will create a service and will create container in the service. Finally it deploys the container to our VM.

image

We should see our container up and running.

image

When we visit the VM IP Address, we should see our site up and running.

image

Now lets add following code change in _layout.cshtml and make a git push.

<p>Hi, this is a change!</p>

git status

git add .

git status

image

git fetch origin

git merge origin/master

git commit -m “Change”

git push origin master

image

The entire continuous integration and deployment workflow will get triggered and site will be updated in Ubuntu Virtual Machine.

image

That’s it for now. Happy Coding and Stay Tuned!!!

You may also like...