📜 ⬆️ ⬇️

ASP.NET Core + Docker: Cooking Together

We are happy to share with you another article from the series of articles on the ASP.NET Core platform (formerly ASP.NET 5). This time Vyacheslav Bobik, a .NET developer from Radario, will continue his story about the platform with a story about using ASP.NET with Docker containers. All articles of the series you can always find here #aspnetcolumn - Vladimir Yunev

image

We know Kam, ASP.NET Core, it is possible (and even necessary) to run not only on Windows, but also on Mac and Linux. What does this give us? For example, we can pack our application with all its dependencies in a docker container and conveniently deploy it on test / work environments. We will not discuss what docker is and how to install it , but instead try to build an image with an ASP.NET Core.

Prepare a test application


We have a choice on what to run our first ASP.NET Core application: on mono or on coreclr . I will choose the second option, as it is fashionable, stylish, youth.
')
By tradition, create an empty ASP.NET Core project:

alt

After creating, let's go to the project.json file, it should contain the following fields:

 .... "dependencies": { "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final", "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final" }, "commands": { "web": "Microsoft.AspNet.Server.Kestrel --server.urls http://*:5004" } , .... 

We are interested in the commands section, namely the web command. When it is executed, the kestrel web server is started, which will be available on port kestrel (???)

Let's check if our application starts locally: for this we first make sure that we have the correct version of DNX , and it is set as default for use.

We perform


 dnvm list 

alt

If you selected the clr runtime version, then you need to switch to coreclr with the command:

 dnvm use 1.0.0-rc1-final -r coreclr -p 

If you don’t have a coreclr version, it’s easy to install by running

 dnvm upgrade -r coreclr 

Let's launch our application: go to the folder where it was created and execute it.

 > dnu restore > dnx web 

The first command installs dependencies, the second one launches our application. Now, if we go to http: // localhost: 5004 , we will see a welcome message. Everything, we are ready to pack our application in a container.

Create a docker image


In order for the docker to collect us an image (image), we need to tell him [the docker] how to do this by means of the commands recorded in the docker file.

Docker creates an obar (image) by reading the commands from the Dockerfile . Using Dockerfile in the project root ( although this does not contradict the best practices in writing Dockerfile ) with the following commands:

 FROM microsoft/aspnet:1.0.0-rc1-update1-coreclr COPY . /app WORKDIR /app RUN ["dnu", "restore"] EXPOSE 5004 ENTRYPOINT ["dnx", "-p", "project.json", "web"] 

FROM - Specify the base image on the basis of which we will build our own
COPY - Copy everything from the folder (.) To the file system of the container, to the folder / app
WORKDIR - Set the working folder for which the RUN, CMD, ENTRYPOINT, COPY and ADD commands will be executed follow this command in the doc file

RUN - Install all dependencies for our application. RUN do a little more than just expelling a team, here it ’s described in more detail.
EXPOSE - We say to the docker that in our image there is a service that will listen to port 5004 (which we specified in the project.json file)

ENTRYPOINT - Specify the command that will be executed when the container is started . Differences from CMD

Now we will collect our image:

 docker build -t first-aspnet-app . 

After a while, when our image is assembled, run it:

 docker run -d -p 80:5004 first-aspnet-app 

-d - switches the running container to the background, and the input / output of the web server would be attached to our shell.
-p - mappit port of the host machine at the exposed port of the container. Ie all requests that come to port 80 will be forwarded to port 5004

Now, if we go to http://localhost:80 we will see a welcome message. Since I work with the docker in windows environment, I interact not directly, but through the docker-machine , so I need to get my docker-machine address on the ip, which can be recognized not by a tricky command

 docker-machine ip 

In my case, the application should be checked at http://192.168.99.100:80 (port 80 can be omitted).

Voila, it works!

alt

Create the right docker image.


However, not everything is so smooth, of course there are pitfalls in the docker. One big such underwater cobblestone is the problem of the PID 1 or goiter process, more here . The guys from phusion (the creators of phusion passenger ) took care of us and made a phusion / baseimage image that solved many problems, which we will use to create an image with an ASP.NET Core.

With the method of honestly copying commands from the Microsoft Docker file , we get our own:

 FROM phusion/baseimage:latest ENV DNX_VERSION 1.0.0-rc1-update1 ENV DNX_USER_HOME /opt/DNX_BRANCH ENV DNX_RUNTIME_ID ubuntu.14.04-x64 RUN apt-get -qq update && apt-get -qqy install unzip curl libicu-dev libunwind8 gettext libssl-dev libcurl3-gnutls zlib1g && rm -rf /var/lib/apt/lists/* RUN curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_USER_HOME=$DNX_USER_HOME DNX_BRANCH=v$DNX_VERSION sh RUN bash -c "source $DNX_USER_HOME/dnvm/dnvm.sh \ && dnvm install $DNX_VERSION -alias default -r coreclr \ && dnvm alias default | xargs -i ln -s $DNX_USER_HOME/runtimes/{} $DNX_USER_HOME/runtimes/default" RUN LIBUV_VERSION=1.4.2 \ && apt-get -qq update \ && apt-get -qqy install autoconf automake build-essential libtool \ && curl -sSL https://github.com/libuv/libuv/archive/v${LIBUV_VERSION}.tar.gz | tar zxfv - -C /usr/local/src \ && cd /usr/local/src/libuv-$LIBUV_VERSION \ && sh autogen.sh && ./configure && make && make install \ && rm -rf /usr/local/src/libuv-$LIBUV_VERSION \ && ldconfig \ && apt-get -y purge autoconf automake build-essential libtool \ && apt-get -y autoremove \ && apt-get -y clean \ && rm -rf /var/lib/apt/lists/* ENV PATH $PATH:$DNX_USER_HOME/runtimes/default/bin COPY . /app WORKDIR /app RUN ["dnu", "restore"] EXPOSE 5004 ENTRYPOINT ["dnx", "web"] 

What to do in the distance we already know:

 docker build -t aspnetcore-phusion . 

 docker run -d -p 80:5004 aspnetcore-phusion 

If you, like me, did not stop the container created at the beginning of the article, then the docker will tell us that port 80 is already taken:

 docker: Error response from daemon: failed to create endpoint loving_kowalevski on network bridge: Bind for 0.0.0.0:80 failed: port is already allocated. 

Stop our previously launched container and launch a new one.

 docker stop loving_kowalevski docker run -d -p 50:5004 aspnetcore-phusion 

Every time we run the container, the docker generates a random name for it. How exactly - you can see here .

Conclusion


In this short review article, we created a pair of containers with a simple ASP.NET Core application. Of course, in reality, applications are usually more complicated, and there are more containers.

In my opinion, ASP.NET Core applications running on coreclr are not yet production ready, since not all library familiar to us can be launched on coreclr. For example, for mongodb there is no driver yet, and autofac is still in alpha. Of course, you can find a way out to mono , but we are here with you about coreclr . However, progress does not stand still, libraries are ported, and soon we will see real cross-platform applications on ASP.NET Core.

To authors


Friends, if you are interested in supporting the column with your own material, please write to me at vyunev@microsoft.com to discuss all the details. We are looking for authors who can interestingly tell about ASP.NET and other topics.

about the author


Bobik Vyacheslav


Developer Company Radario

Announcement! Deep Intense at the DevCon 2016 Conference


image

We are pleased to announce the holding of a deep intensive course on ASP.NET Core at the conference DevCon 2016 . This course will take place on the second day of the conference and will take a full day of the conference for 6 hours.

Development of modern web applications on the open platform ASP.NET 5
As part of this intense, participants will take part in an exciting and adventurous journey dedicated to developing web applications on the latest ASP.NET platform 5. We will go all the way from zero to a full-fledged application deployed in the cloud. And on the way, participants will be able to stop to explore the internal structure of ASP.NET, work with relational and NoSQL databases using the Entity Framework 7, develop applications on the MVC framework, build models, views and business logic, create a REST API, and organize continuous development processes. and testing using Git and Visual Studio Online, as well as deploying using Azure and Docker containers. At the end of the trip, all participants will pass the dedication and become deserved knights of ASP.NET.

Registration for the DevCon 2016 conference is already open! Sign up here .

Source: https://habr.com/ru/post/278173/


All Articles