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.
- Microsoft Windows 10 Enterprise N (10.0.10586 Build 10586) x64
- Visual Studio Community 2017 RC 15.0.26020.0
- Dotnet Version: 1.0.0-preview4-004233 (this SDK is based on MSBuild, so no Project.json in the created application)
- 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.
Lets do a Git clone to map remote repository to local machine.
Git clone https://github.com/DreamingDevs/DockerDotnetCoreApp.git
Create a new ASP.Net Core 1.0 Application using following commands.
dotnet new -t web
Add the following code to _Layout.cshtml to display the description of OS it is running at runtime.
Build and run the application by running following commands.
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.
COPY . /app
RUN [“dotnet”, “restore”]
RUN [“dotnet”, “build”]
ENV ASPNETCORE_URLS http://*:5000
ENTRYPOINT [“dotnet”, “run”]
docker build . -t dockerdotnetcoreapp
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).
Run the image as shown below.
docker run –d –p 8080:5000 –t dockerdotnetcoreapp
Lets commit the code to GitHub repository.
git add .
Commit and push files to GitHub using following commands.
git commit –m “Initial”
git push origin master
Now we should have all code in GitHub as shown below.
Now lets login to https://cloud.docker.com/ and navigate to Nodes section.
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.
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.
Click Create. Fill in the required details like Name. Select Password as the Authentication Type.
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.
we need to add an inbound rules to configure 2375, 6783/tcp, 6783/udp and 80 ports. Find the Network Security Group.
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.
Now add ports as shown below.
Add 80 port as well which will allow us to browse deployed application from Virtual Machine.
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.
We will get an alert to cache the server’s host key. Click No.
Login to the console by providing the username and password (which we provided at the time of VM creation).
Now lets install Docker Cloud agent on VM. First lets become root on VM.
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
It will download and install Docker Cloud Agent. Once completed we will have following message.
Now we will create an association from Docker cloud to GitHub. Go to Cloud Settings tab at Docker Cloud portal.
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.
Enter the name of Docker repository and select the GitHub repository, finally click Create.
After successful repository creation, we will configure automated builds to be deployed on Azure Ubuntu Virtual Machine. Navigate to Builds tab.
Click on Configure Automated Builds. Select Build Location to Build on my own nodes. Click Save and Build.
On Successful build we will have following information (below images show my previous experimental builds, please ignore the failed and cancelled builds).
On successful build, We should find the Docker image in our Docker hub account.
Now we will create a Service in Docker Cloud which will hold the container created from our Docker image.
Click Create. Select the image which we created as shown below.
In the general settings, we need to enable AUTOREDEPLOY and add 80 port to the machine.
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.
We should see our container up and running.
When we visit the VM IP Address, we should see our site up and running.
Now lets add following code change in _layout.cshtml and make a git push.
<p>Hi, this is a change!</p>
git add .
git fetch origin
git merge origin/master
git commit -m “Change”
git push origin master
The entire continuous integration and deployment workflow will get triggered and site will be updated in Ubuntu Virtual Machine.
That’s it for now. Happy Coding and Stay Tuned!!!