ASP.NET vNext using Docker on AWS


ASP.NET and vNext

You have probably heard that ASP.NET is open source now. Here is what you need to know

Microsoft ASP.NET vNext is a smaller and efficient framework as a result of rebuilding the libraries from ground up.

What's new in vNext

  • Cloud-optimized versions of MVC, Web API, Web Pages, SignalR, and Entity Framework.
  • MVC, Web API, and Web Pages will be merged into one framework, called MVC 6.
  • MVC 6 has no dependency on System.Web. The result is a leaner framework, with faster startup time and lower memory consumption.
  • vNext is host agnostic. You can host your app in IIS, or self-host in a custom process.
  • Dependency injection is built into the framework. Use your preferred IoC container to register dependencies.
  • vNext uses the Roslyn compiler to compile code dynamically. You will be able to edit a code file, refresh the browser, and see the changes without rebuilding the project.
  • vNext is open source and cross platform.
Docker

a href="https://www.docker.com/whatisdocker/"Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications. Consisting of Docker Engine, a portable, lightweight runtime and packaging tool, and Docker Hub, a cloud service for sharing applications and automating workflows, Docker enables apps to be quickly assembled from components and eliminates the friction between development, QA, and production environments. As a result, IT can ship faster and run the same app, unchanged, on laptops, data center VMs, and any cloud.

Enough with the buzzwords, what are we doing?

Today we will be launching an ASP.NET5 application on a docker container inside a Linux box hosted on an Amazon EC2 Instance!

First of all I must thank Ahmet, without his walkthrough I wouldn't have been able to port it over to AWS

I also have a great deal of gratitude to my employer JustEat for giving me the infrastructure, empowering and encouraging me to always be improving and try out new technologies

OK now that the acknowledgements are over with, the first step is to create our EC2 instance. Log on to the AWS console and click on EC2.

Hint: click the images to see their full size step 1

Once there we should see the EC2 dashboard and its here where we will begin creating our EC2 instance.

launch instance

In the AWS world we define our instances with a template which contain the OS, applications etc called an AMI. The first step is to define our AMI. The beauty of AWS is that we can find loads of AMIs that have already been created or “baked” for us. I decided to use an Ubuntu 14.04 instance (ami-06855e71) with docker already pre-installed.

docker images

Please continue to define the instance size, and configuration and security group.

Security groups are basically firewall rules that control the traffic for our instances. Leave it as the default SSH for now.

security groups

The next step is just a summary of the configuration for our instance

summary

The final step is the key value pair, this is a very important step since we will need this key to SSH into the ubuntu instance. Create a new keypair and save it somewhere you will remember because we will need to use it next

key value pair

Ok the instance is launching, now what?

Next go to your instances and we can see that we have an instance initialising and I will name it vNextDocker so it helps me find it later on. We know that the instance is running when the status checks that AWS runs are complete.

status checks

SSH TIME!

So next we need to get the command to connect to our instance. Again AWS makes this extremely easy for us

Filter on the instance and click on connect to get the command connect via ssh

Now we open up our command console and navigate to where we saved our keypair and enter the ssh command, for us this is..

“ssh -i vNextDocker.pem [email protected]"

Of course your public dns will be different but the AWS window should give you the correct command.

connect via console

Now its time to check out the code, we git clone the hello world app using the below command

“git clone https://github.com/samelamin/HelloDockerWorld.git hellodockerworld”

This command creates a “hellodockerworld” folder and checks out the source code inside it

next navigate into the created folder using

“cd hellodockerworld”

and we should see the 2 source files

file lists

Docker Docker Docker!

Docker uses a file called a a href="https://docs.docker.com/reference/builder/"Dockerfile to describe how the application works. You can imagine this file as a list of instructions telling docker how to build this application

So we need to create this docker file. to do this just run

“nano Dockerfile”

This commands opens up an editor and paste this into it

FROM microsoft/aspnet

COPY . /hellodockerworld WORKDIR /hellodockerworld

RUN ["kpm", "restore"]

EXPOSE 5004 ENTRYPOINT ["k", "kestrel"]

The instructions are fairly straight forward.The first line tells Docker to use the offical ASP.NEt on Docker Hub as our base image

  • The COPY tells Docker that we will copy the contents of the current folder to the ./hellodockerworld of the container while the WORKDIR instruction will move to the /hellodockerworld
  • The RUN command basically says use the “K Package Manager” to install the dependencies, you might ask yourself - this is basically nuget - well actually it uses nuget internally hence why the .NET team recently announced that they are renaming the kpm to just nuget
  • The EXPOSE command informs Docker that this image has a service which will be listening at port 5004.(see project.json for more info)
  • The ENTRYPOINT starts the container and keeps it up and running, in our case we are starting listening to requests coming from port 5004
Finally press CTRL+X to exit the editor and Press Y to save the file and name it Dockerfile

Next we build the docker image, all we need to do is run this command

“sudo docker build -t hellodockerworld .”

This command builds an image using the Dockerfile we just created and calls the image hellodockerworld.

Every time we want to redeploy the application we should just rebuild the container. That means we have a brand new application for each change in the application

Once the building of the image is complete we can confirm that the image exists by listing the docker images on the server

“sudo docker images”

docker image list Running the damn thing!

Now that the hard part is done we can start the container using the below command

“sudo docker run -t -d -p 80:5004 hellodockerworld”

  • The -t switch attaches a pseudo-tty to the container (this switch will not be necessary in future versions of ASP.NET 5).
  • The -d switch runs the container in the background, otherwise the web server's standard input/output streams would be attached to our development machine's shell.
  • The -p switch maps port 80 of the VM to port 5004 of the container. In this case, connections coming to port 80 of the VM will be forwarded to our container listening on port 5004.
  • Lastly, hellodockerworld is the Docker image name we are using to start the container. We built this image in the previous step.
Now if we navigate to the public ip address on a browser then we should see our application running right?

Actually no you will find that the request times out, any idea why?

Well its actually the security groups! we have not enabled access via HTTP (port 80) so if you navigate to your instance on the AWS Console

security groups gotcha

next we right click over the security group and edit the inbound rules and allow HTTP

add HTPP

Now if we refresh our browser……

Done

Thats it folks! now its time to build useable applications and rebuild your container. Have fun!



0 comments