在Docker For Windows部署ASP.NET Core MVC

2018/11/11 17:07:56 人评论 次浏览 分类:Docker



ASP.NET Core具备可以移植性,可以部署发布到Docker中。下面给大家说一下具体的发布方法。我们现在简单的方式了解Docker的部署。

准备:已经安装了Docker for window 。已经安装了 .NET Core 的SDK ,(不用安装Server Hosting)

执行:

1.打开Powershell 窗口
2.通过 dotnet new mvc –o mvcapp 命令创建 dotnet core mvc的默认应用程序
3.进入mvcapp目录 cd mvcapp ,mvcapp会被创建到 C:\Windows\System32\mvcapp 中。
4.还原包 dotnet restore ,如果是dotnet core 2.0,则可以跳过这个步骤,dotnet core 2.0 自带这个操作,无须单独执行,我们可以从命令行的输出信息可以看到。
5.发布应用程序 dotnet publish –c release 。执行此操作需要在C:\Windows\System32\mvcapp的目录下才能执行。
6.在当前目录下面创建一个DOCKERFILE,内容如下 :

FROM microsoft/dotnet:latest
WORKDIR /app
COPY bin/release/netcoreapp2.1/publish .
ENTRYPOINT ["dotnet", "mvcapp.dll"]

说明:DOCKERFILE文件,只是文件名称为DOCKERFILE,没有后缀的文件,不需要加.txt 。DOCKERFILE是build 镜像的配置文件,记录build镜像的步骤。
Form表示基础镜像,我们可以单独去pull 对应版本的镜像 dotnet,或者直接通过DOCKERFILE文件里面的Form在build镜像的时候才拉取下来。如果是使用最新版的SDK,可以直接FROM microsoft/dotnet:latest。如果你的 .NET Core SDK是最新的,那么From这里还是填 FROM microsoft/dotnet:latest。
COPY表示发布程序所在目录,注意别漏掉点号。

7.生成docker镜像 docker build –t mvcapp . ,注意后面的点不要漏了,否则会报错,提示没有找到 -t 的选项。
8.运行docker镜像 docker run –p 8000:80 –e “ASPNETCORE_URLS=http://+:80” mvcapp

如果成功的话,在执行最后一个步骤的时候会有成功的提示:Application started. Press Ctrl+C to shut down.

PS C:\Windows\System32\mvcapp> docker run -p 8000:80 -e “ASPNETCORE_URLS=http://+:80” mvcapp
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
No XML encryptor configured. Key {1255175f-b6ce-4bef-b2c3-7391446f9d9c} may be persisted to storage in unencrypted form.
Hosting environment: Production
Content root path: /app
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.
warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
Failed to determine the https port for redirect.

这样子就可以在浏览器打开 localhost:8000/ 就可以访问了,
如果是虚拟机里面的话,可以先把防火墙给关闭了,方便虚拟机外部浏览器可以访问 http://192.168.1.11:8000/

=====================================================================

这个过程可能遇到错误:
a. 在执行 docker run –p 8000:80 –e “ASPNETCORE_URLS=http://+:80” mvcapp 的时候:
It was not possible to find any compatible framework version
The specified framework 'Microsoft.AspNetCore.App', version '2.1.1' was not found.
- Check application dependencies and target a framework version installed at:
/
- Alternatively, install the framework version '2.1.1'.
这个错误说明Dockerfile的From的dotnet版本跟应用程序的版本对应不上。


b.在执行 docker build -t mvcapp . 的时候:
PS C:\Users\Hayden> docker build -t mvcapp .
unable to prepare context: unable to evaluate symlinks in Dockerfile path: GetFileAttributesEx C:\Users\Hayden\Dockerfile: The system cannot find the file specified.

这个说明没有找到发布程序所在的目录,所以找不到对应的Dockerfile,所以需要进入到程序所在的目录,比如 cd C:\Windows\System32\mvcapp。



相关资讯

  • 阿里云的Docker镜像仓库的发布和拉取操作

    镜像仓库的操作方法:pull和push【阿里云的镜像仓库】从阿里云的镜像仓库中拉取镜像以及将镜像推送至镜像仓库。注意下面的命令在windows的CMD和powershell 是没有sudo的。1. 登录阿里云Docker Registry$ sudo docker login --username=hi50156666@aliyun.com registry.cn-hang…

    2018/10/29 22:29:15
  • Windows中Docker的Pull镜像源配置

    在windows containers模式下,打开setting的daemon,选择高级模式advanced,在registry-mirrors填入镜像地址。Docker 中国官方镜像加速地址

    2018/10/28 21:59:04
  • win10的Docker提示error during connect和 This error may also indicate that the docker

    error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.38/containers/json: open //./pipe/docker_engine: The system cannot find the file specified. This error may also indicate that the docker daemon is not running.

    2018/10/28 20:15:44
  • Docker注册账号与Docker安装包下载安装

    【Docker For Window与Docker toolbox】根据window系统选择对应的安装包1.Docker For Window (需要在Win10环境下安装Hyper-V).不具备Hyper-V的服务器可以安装Docker Tooltip 。阿里云ECS本身作为虚拟化产品不支持hyper-V二次虚拟化,这是就可以考虑用Docker Tooltip 了。注意…

    2018/10/28 15:03:52