Creating a .NET Core project

If you already have an existing .NET Core project you are more than welcome to use it. Otherwise create a new empty ASP.NET Core application by opening your command line and typing:

 
 
 
 

Shell

 
1
2
3
dotnet new web --framework netcoreapp1.1 --name docker-example
cd docker-example
dotnet restore

This will create a new directory with an empty ASP.NET Core application and install all dependencies. Alternatively you can do this directly in Visual Studio if you prefer that. Once they’re installed we can see if it is working by compiling and running it:

 
 
 
 

Shell

 
1
dotnet run

Once it is saying Now listening on: http://localhost:5000 navigate to http://localhost:5000 in your browser and you should see a Hello World message on the screen. You can shut down the application now by pressing Ctrl+C in your command-line.

Creating a Dockerfile

Now that we have an application we need to “dockerize” it by creating a DockerfileDockerfile is used to describe a Docker application, also called a container. It describes what is necessary to get your application running. This includes which operating system to use, setting up the directories, copying your application code into the container etc.
Luckily we do not have to reinvent the wheel for every application we use Docker for. We can always derive our Dockerfile from another existing image as a base image. There are hundreds of thousands of container images on Docker Hub that we could use as base image.

For our project we will be using the microsoft/dotnet image as our base image. Microsoft provides different versions of this image that are partially denoted with suffixes such as runtime or sdksdk is an image that will come with the full .NET Core SDK which means you could create or build applications with this image. This is useful for development but not for production. The more lightweight version of the image is tagged with runtime. It only includes the bits necessary in order to run a .NET Core app. It ultimately results in a smaller sized container image with 251 MB compared to 879 MB for the version 1.1.1. In this blog post we will be using the runtime image.

Create a new file called Dockerfile (no file extension) inside your .NET Core project directory and add the following line to it:

 
 
1
2
FROM microsoft/dotnet:1.1.1-runtime
LABEL name "docker-example"

This will define our base image as the 1.1.1-runtime version of the microsoft/dotnet image. The base image will handle things from setting up the operating system to installing the runtime.

Next we need to copy the files necessary to run our application into a folder in the container. This can be done using the COPY command in the Dockerfile. We will be compiling our application later into the out folder of our project therefore we need to copy them from that location to our working directory in this case called /app. Add the following lines in your Dockerfile:

 
 
1
2
WORKDIR /app
COPY out .

Define which port our ASP.NET Core server will be running on by setting the environment variable ASPNETCORE_URLS respectively. In our case the port will be 5000. For us to be able to access this port also from outside our Docker container we need to additionally expose it. Do this by appending the following lines to the Dockerfile:

 
 
1
2
ENV ASPNETCORE_URLS http://*:5000
EXPOSE 5000

Lastly we need to start up our .NET application by using the dotnet executable and pass it the DLL with the name of our application (docker-example.dll).

 
 
1
ENTRYPOINT ["dotnet",  "docker-example.dll"]

The final Dockerfile should now look like this:

 
 
1
2
3
4
5
6
7
8
9
10
FROM microsoft/dotnet:1.1.1-runtime
LABEL name "docker-example"
 
WORKDIR /app
COPY out .
 
ENV ASPNETCORE_URLS http://*:5000
EXPOSE 5000
 
ENTRYPOINT ["dotnet",  "docker-example.dll"]

That’s it our Dockerfile is ready to take a local spin.

Run Docker Container Locally

Before we can create our Docker image and run it locally we first need to compile our project and publish it to the out folder for our Docker container to pick it up. Run the following command in your command line:

 
 
 
 

Shell

 
1
dotnet publish -c Release -o out

Once it’s done compiling we can build our Docker image:

 
 
 
 

Shell

 
1
docker build -t docker-example .


This will pull the base image from the Docker Hub and perform the steps specified in the Dockerfile and bundle the existing system in a container. Now let’s run our container:

 
 
 
 

Shell

 
1
docker run -p 5000:5000 -it --rm docker-example

This command will run our docker-example container in “interactive” mode meaning it will output the console output, additionally it will map the exposed port 5000 of our container to our localhost port 5000 and the --rm will make sure to remove the Docker container when we quit the application using Ctrl+C.

You should now be able to go again to https://localhost:5000 and see Hello World.


If everything is working you can stop the Docker container by exiting it using Ctrl+C.

Deploy your Docker to now.sh

Our Docker container is ready and it’s time to deploy it to the cloud. The great thing is that we can run this container on various cloud hosts by uploading our built container image into a registry like Docker Hub and then instructing a cloud host like Azure to deploy the respective container image. We will look at an alternative though.

now.sh is a cloud hosting platform that allows us to easily deploy Docker project. The great thing is that it doesn’t require you to build the container image on your computer or host it in any registry. Instead it will build the container for you in the cloud. Additionally it will take care of scaling and host it on Google Cloud Engine as well as Amazon Web Services.

A warning in advance: The free plan of now is designed for open source projects. As part of that it will enable you to inspect the source of your application by going to /_src on your deployed project to inspect the source. Meaning only use this free plan if you are deploying an application without sensitive data. Sensitive data should be stored in secrets and loaded via environment variables.

To start with now install the CLI or alternatively the now Desktop application (which includes the CLI) from now’s download page. Once it’s installed and you are registered it’s time to deploy our app.

Once the CLI is installed all you need to do run in the command-line of your project directory:

 
 
 
 

Shell

 
1
now

That’s it! It will upload your source code, build the Docker container image and deploy it. It will also copy a URL to your clipboard that your application will be hosted on. It should be of the format: https://docker-example-xxxxxxxxxx.now.sh. While your application is being deployed you will also see the status of your deployment on this page.

If you do any changes to your application’s code deploying the new version is as easy as re-compiling and running now:

 
 
 
 

Shell

 
1
2
dotnet publish -c Release -o out
now

now will always create a new URL each time you deploy. This means you can deploy a new version, test it and once we are happy we can alias it to a different “production” URL. Aliasing is as easy as:

 
 
 
 

Shell

 
1
now alias docker-example-xxxxxxxxxxx myamazingdockerapp

This will result in mapping your deployment docker-example-xxxxxxxxxx to https://myamazingdockerapp.now.sh. On the paid plan you can also use custom domains and it auto sets up HTTPS for you as well.

Deploy .NET Core with Docker的更多相关文章

  1. 自动部署Asp.Net Core到Docker

    原文链接:个人博客:自动部署Asp.Net Core至Docker 本文简介 最近在开发一个管理系统,代码框架是用的前后台分离的方式 后台使用的是Asp.Net Core平台,开发所有业务,向前台提供 ...

  2. .NET core for docker

    本文描述下 .net core 在 docker 里面的玩法 首先按照官方文档先 拉取镜像 docker pull microsoft/dotnet:latest 然后就有了 dotnet 这个运行时 ...

  3. ASP.NET Core开发-Docker部署运行

    ASP.NET Core开发Docker部署,.NET Core支持Docker 部署运行.我们将ASP.NET Core 部署在Docker 上运行. 大家可能都见识过Docker ,今天我们就详细 ...

  4. 基于Microsoft Azure、ASP.NET Core和Docker的博客系统

    欢迎阅读daxnet的新博客:一个基于Microsoft Azure.ASP.NET Core和Docker的博客系统   2008年11月,我在博客园开通了个人帐号,并在博客园发表了自己的第一篇博客 ...

  5. [翻译] 使用ElasticSearch,Kibana,ASP.NET Core和Docker可视化数据

    原文地址:http://www.dotnetcurry.com/aspnet/1354/elastic-search-kibana-in-docker-dotnet-core-app 想要轻松地通过许 ...

  6. .NET Core微服务之ASP.NET Core on Docker

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 一.Docker极简介绍 1.1 总体介绍 Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源.D ...

  7. [翻译] ASP.NET Core 利用 Docker、ElasticSearch、Kibana 来记录日志

    原文: Logging with ElasticSearch, Kibana, ASP.NET Core and Docker 一步一步指导您使用 ElasticSearch, Kibana, ASP ...

  8. asp.net core的docker实践

    如果centos中没有安装和docker和.net core镜像,先安装docker和asp.net core 镜像 安装dockeryum -y install docker-io 启动 Docke ...

  9. .Net Core in Docker - 在容器内编译发布并运行

    Docker可以说是现在微服务,DevOps的基础,咱们.Net Core自然也得上Docker..Net Core发布到Docker容器的教程网上也有不少,但是今天还是想来写一写. 你搜.Net c ...

随机推荐

  1. Redis学习之底层链表源码分析

    Redis底层链表的源码分析: 一.链表结点的结构(单个结点): // listNode 双端链表节点 typedef struct listNode { // 前置节点 struct listNod ...

  2. Generative Adversarial Nets[AAE]

    本文来自<Adversarial Autoencoders>,时间线为2015年11月.是大神Goodfellow的作品.本文还有些部分未能理解完全,不过代码在AAE_LabelInfo, ...

  3. oracle 创建表空间TABLESPACE

    题外话: 在oracle中,我们的数据库用户都有一个默认表空间归属,当在该用户下创建表或其他对象时默认会将其归属在默认表空间: 不排除后期修改了用户默认表空间的情况存在,此后新加入的对象默认会放置在新 ...

  4. RabbitMQ详解(二)------消息通信的概念

    PS:近期在南宁出差,工作比较忙,所以更新会比较慢. 说到消息通信,可能我们首先会想到的是邮箱,QQ,微信,短信等等这些通信方式,这些通信方式都有发送者,接收者,还有一个中间存储离线消息的容器.但是这 ...

  5. JVM源码分析--ClassLoader类加载器

    本人原创,转载请注明出处:https://www.cnblogs.com/javallh/p/10224187.html 1.JDK已有类加载器: BootStrap ClassLoader (启动类 ...

  6. 【C#复习总结】细说委托

    1 前言 本系列会将[委托] [匿名方法][Lambda表达式] [泛型委托] [表达式树] [事件]等基础知识总结一下.(本人小白一枚,有错误的地方希望大佬指正) 系类1:细说委托 系类2:细说匿名 ...

  7. 朱晔的互联网架构实践心得S2E1:业务代码究竟难不难写?

    注意,这是我的架构实践心得的第二季的系列文章,第一季有10篇你也可以回顾. 见https://www.cnblogs.com/lovecindywang/category/1296779.html 最 ...

  8. Git_GitHub详解

    Git和Github详细教程 一  概述 说到Git和Github,前几天我们知道微软以75亿美元收购全球最大的代码托管和写作平台GitHub,而GitHub是全球最大的代码仓库,很多开发人员都将代码 ...

  9. hibernate多对多 一对多 及简单入门 主键生成策略

    Hibernate简单使用 入门 通过hibernate的 一对多 多对多轻松看懂hibernate配置 (不使用注解) hibernate对jdbc访问数据库的代码进行轻量级封装,简化重复代码 减少 ...

  10. ubuntu中更改apache默认目录的方法

    如上,在这两个文件中,我都改为/home/www 及/home/www/html