Wednesday, 11 January 2017

Publishing a ASP.NET MVC Site or WebApi to a Docker Machine locally in Windows on Windows Server.

It happens sometimes you will want to develop a single project application. One of those, proof of concepts you do to learn new stuff. The problem comes when you have to stop doing Micky Mouse applications and move to a different world where environments and continuous integration becomes the core of the development.

This is where a new position has risen to help the devs, they are called the DevOps. The issue with DevOps, it is sometimes they have too much work to to, so they can be solving your own problems. Especially if you are doing continuous integration in Azure or Amazon AWS.

This is where Docker becomes a big player, we have a tool it is sophisticated enough to do the job, and it is simple enough to be used by devs. Docker comes from the Java and Linux world, so I thought it will be interesting to try to create a Docker image to run .NET stuff.

So step by step…. the first step will be to create a MVC or WebApi application, ASP.NET 4 or ASP.NET Core whatever you prefer.


Step 2: Right click in your project (in my case web) –> Publish…

Step 3: Click on custom and enter DockerDeployment and click OK



Step 4: In the Publish method with the drop down list select File System and in target location, select c:/docker



Step 5: Click Next->select release->select Publish



Step 6: It is time to play with Docker. Install Docker (the beta version) 
and right click on the task bar, then select “Switch to Windows containers…”



Step 7: Create a file called “Dockerfile” in c:\docker and paste this (see below). This code will get the code from your deployment and will put it in the image microsoft/aspnet copying the data to the root folder

FROM microsoft/aspnet

WORKDIR /inetpub/wwwroot
COPY .  /inetpub/wwwroot

Step 8: Open powershell with admin permissions, and go to the C:\docker folder and execute this command:

This will execute the Dockerfile script and will create a new instance


Step 9: It is time to run your instance, just type:



Step 10: You can list the images by doing a Docker ps . Get the first 3 digits of the CONTAINER ID, in this case 989



Step 11: It is time to know your URL. just type the following and you will get the URL of your docker machine
docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" 989



Step 12: If you want to access to your local machine you can do things like this (where 989 is the Container Id)

Access to MS-DOS: docker exec -i –t 989 cmd


To Create a new Site, use this DockerFile (we called the web Webgenerator):
FROM microsoft/aspnet
SHELL ["powershell"]

RUN Install-WindowsFeature NET-Framework-45-ASPNET ; \  
    Install-WindowsFeature Web-Asp-Net45

COPY WebGenerator WebGenerator 
RUN Remove-WebSite -Name 'Default Web Site'  
RUN New-Website -Name 'WebGenerator' -Port 80 \  
    -PhysicalPath 'c:\WebGenerator' -ApplicationPool '.NET v4.5'

CMD ["ping", "-t", "localhost"]  

No comments: