使用Visual Studio在Linux上将ASP.NET 5应用发布到Docker

本文介绍了如何利用Visual Studio 2015预览版的Docker扩展将ASP.NET 5应用发布到Linux上的Docker容器。讨论了Docker的基本概念,如与虚拟机的区别,以及Docker容器的优势。通过示例展示了如何创建和发布应用,强调了Dockerfile的重要性,并提供了Docker相关命令的输出示例。
摘要由CSDN通过智能技术生成
Docker Apps are mostly portable

It's early days, but this is a nice preview of the possibilities of things to come. I often use LEGO bricks in the way of an analogy when talking about software systems. I like the idea of choice, flexibility, and plug-ability. Choosing your language, operating system, deployment method and style, etc are all important.

现在还很早,但这是对未来可能性的很好的预览。 在谈论软件系统时,我经常以类推的方式使用乐高积木。 我喜欢选择,灵活性和可插入性的想法。 选择您的语言,操作系统,部署方法和样式等都很重要。

There is a preview of an extension for Visual Studio 2015 (the release candidate at the time of this writing) that adds Docker support. If you have VS2015 RC you can get the Docker Extension here. You can certainly manage things from the command line, but I think as you go through this post you'll appreciate the convenience of this extension.

预览了添加了Docker支持的Visual Studio 2015扩展(在撰写本文时为发行候选)。 如果您使用VS2015 RC ,则可以在此处获取Docker扩展。 您当然可以从命令行进行管理,但是我认为当您阅读本文时,您会欣赏此扩展程序的便利性。

NOTE: It's also worth pointing out that there is a Windows client command line for Docker as well. You can "choco install docker" and read about it here.

注意:还值得指出的是,Docker也有Windows客户端命令行。 您可以“ choco安装docker并在此处阅读有关内容

Docker简介 (The Brief What's Docker Explanation)

If you aren't familiar with Docker, here's the super basics.

如果您不熟悉Docker,那么这里是超级基础知识。

  • Virtual Machines: You likely know what a virtual machine is. It's the whole operating system, the whole computer, virtualized. If you have a 10 megabyte app you want to run, you may end up putting it in a 10 gigabyte virtual machine and carrying it around. That gives great security and isolation as your app is all alone on its own private VM, but it's a little overkill. Now you want to deploy 100 apps, and you've got space, CPU, and other things to think about. VMs also start slow and have to be actively maintained.

    虚拟机:您可能知道什么是虚拟机。 它是整个操作系统,整个计算机,虚拟化的。 如果您要运行10 MB的应用程序,则可能最终将其放入10 GB的虚拟机中并随身携带。 由于您的应用程序完全在其自己的私有VM上,因此可提供出色的安全性和隔离性,但这有点过头了。 现在,您要部署100个应用程序,并且需要考虑空间,CPU和其他因素。 虚拟机启动速度也很慢,必须积极维护。
  • Docker/Linux Containers (and Windows containers "Docker for Windows Server"): Docker containers are sandboxes running on the same OS kernel. They are easy to deploy and start fast.  As a side effect of running on the same kernel, containers let you share most of that 10 gigabytes (as an example number) of support software between lots of apps, giving you less isolation but also using a LOT fewer resources. Containers start fast and the underlying shared resources are what's maintained and kept up to date.

    Docker / Linux容器(以及Windows容器“ Windows Server的Docker ):Docker容器是在同一OS内核上运行的沙箱。 它们易于部署和快速启动。 作为在同一内核上运行的副作用,容器使您可以在许多应用程序之间共享那10 GB的大部分支持软件(例如,示例数量),从而减少了隔离,但也减少了资源。 容器快速启动,基础共享资源得到维护并保持最新状态。

Docker also is a way to package up an app and push it out in a reliable and reproducible way. So you can say that Docker is a technology, but also a philosophy and a process.

Docker还是打包应用程序并以可靠且可复制的方式将其推出的一种方式。 因此,您可以说Docker是一种技术,也是一种哲学和一个过程。

Docker will work on Windows and Linux

Docker和Visual Studio (Docker and Visual Studio)

Once you have the Docker for Visual Studio 2015 extension (preview) installed, go ahead and make an ASP.NET 5 app. Right click the project and hit Publish.

一旦安装了Docker for Visual Studio 2015扩展(预览版),请继续制作一个ASP.NET 5应用程序。 右键单击该项目,然后单击发布。

Publishing to Docker from Visual Studio

Note the Docker Containers section that's been added? You still have PAAS (Platform as a Service) and can also publish to VMs within Azure as well. Select Docker and you'll be here:

请注意已添加的Docker容器部分吗? 您仍然拥有PAAS(平台即服务),并且还可以发布到Azure中的VM。 选择Docker,您将在这里:

Selecting a Docker VM

We'll make a new VM to host our Docker stuff. This VM will have be the host for our containers. Today it'll be an Ubuntu LTS VM. Note that the dialog includes all the setup for Docker, ports, certs, etc. I could use existing VMs, of course.

我们将制作一个新的VM来托管我们的Docker。 该虚拟机将成为我们容器的主机。 今天它将是一个Ubuntu LTS VM。 请注意,该对话框包括Docker的所有设置,端口,证书等。当然,我可以使用现有的VM。

Making a Docker VM

If you don't have a VM, then the initial create takes a while (5-10 min or more) so hang back. If you already have one, or the one you created is ready, then march on.

如果您没有虚拟机,那么初始创建将花费一些时间(5-10分钟或更长时间),因此请后退。 如果您已经拥有一个,或者您创建的一个已经准备就绪,请继续前进。

Visual Studio put in all our Docker details

Make special note of the Dockerfile option. You'll usually want to select your own manually created Dockerfile, assuming you're doing more than just a Hello World like I am.

特别注意Dockerfile选项。 假设您要做的不只是像我这样的Hello World,您通常会选择自己创建的Dockerfile。

The ASP.NET Dockerfile is up on GitHub: https://github.com/aspnet/aspnet-docker and in the Docker registry: https://registry.hub.docker.com/u/microsoft/aspnet/

ASP.NET Dockerfile在GitHub上: https : //github.com/aspnet/aspnet-docker和Docker注册表中: https ://registry.hub.docker.com/u/microsoft/aspnet/

In the build window you'll see lots of docker-related output. Here's a snipped version for flavor.

在构建窗口中,您将看到许多与Docker相关的输出。 这是一个片段的味道。

VERBOSE: Replacing tokens in Dockerfile: C:\Users\Scott\AppData\Local\Temp\PublishTemp\approot\src\WebApplication6\Properties\PublishProfiles\Dockerfile
VERBOSE: Package output path: C:\Users\Scott\AppData\Local\Temp\PublishTemp
VERBOSE: DockerHost: tcp://hanseldocker.cloudapp.net:2376
VERBOSE: DockerImageName: webapplication6
VERBOSE: DockerPublishHostPort: 80
VERBOSE: DockerPublishContainerPort: 80
VERBOSE: DockerAuthOptions: --tls
VERBOSE: DockerAppType: Web
VERBOSE: DockerBuildOnly: False
VERBOSE: DockerRemoveConflictingContainers: True
VERBOSE: LaunchSiteAfterPublish: True
VERBOSE: SiteUrlToLaunchAfterPublish:
VERBOSE: Querying for conflicting containers which has the same port mapped to the host...
Executing command [docker --tls -H tcp://hanseldocker.cloudapp.net:2376 ps -a | select-string -pattern ":80->" | foreach { Write-Output $_.ToString().split()[0] }]
VERBOSE: Building Docker image: webapplication6
Executing command [docker --tls -H tcp://hanseldocker.cloudapp.net:2376 build -t webapplication6 -f "C:\Users\Scott\AppData\Local\Temp\PublishTemp\approot\src\WebApplication6\Properties\PublishProfiles\Dockerfile" "C:\Users\Scott\AppData\Local\Temp\PublishTemp"]
VERBOSE: time="2015-05-27T10:59:06-07:00" level=warning msg="SECURITY WARNING: You are building a Docker image from Windows against a Linux Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories."
VERBOSE: Sending build context to Docker daemon 28.01 MB
VERBOSE: Step 0 : FROM microsoft/aspnet:vs-1.0.0-beta4
VERBOSE: vs-1.0.0-beta4: Pulling from microsoft/aspnet
VERBOSE: e5c30fef7918: Pulling fs layer
VERBOSE: e5c30fef7918: Pull complete
VERBOSE: e5c30fef7918: Already exists
VERBOSE: Digest: sha256:27fbe2377b5d4e66c4aaf3c984ef03d22afbfee3d4e78e10ff38cac7ff162d2e
VERBOSE: Status: Downloaded newer image for microsoft/aspnet:vs-1.0.0-beta4
VERBOSE: ---> e5c30fef7918
VERBOSE: Step 1 : ADD . /app
VERBOSE: ---> cf1f788321b3
VERBOSE: Removing intermediate container dd345cdcc5d9
VERBOSE: Step 2 : WORKDIR /app/approot/src/WebApplication6
VERBOSE: ---> Running in f22027140233
VERBOSE: ---> 7eabc0da4645
VERBOSE: Removing intermediate container f22027140233
VERBOSE: Step 3 : ENTRYPOINT dnx . Kestrel --server.urls http://localhost:80
VERBOSE: ---> Running in 4810324d32a5
VERBOSE: ---> e0a7ad38eb34
VERBOSE: Removing intermediate container 4810324d32a5
VERBOSE: Successfully built e0a7ad38eb34
The Docker image "webapplication6" was created successfully.
VERBOSE: Starting Docker container: webapplication6
Executing command [docker --tls -H tcp://hanseldocker.cloudapp.net:2376 run -t -d -p 80:80 webapplication6]
Docker container started with ID: 6d4820044df200e87f08cb5becbec879d1b58fcab73145ca3aa99a424c162054
To see standard output from your application, open a command line window and execute the following command:
docker --tls -H tcp://hanseldocker.cloudapp.net:2376 logs --follow 6d4820044df200e87f08cb5becbec879d1b58fcab73145ca3aa99a424c162054
VERBOSE: received -1-byte response of content type text/html; charset=utf-8
Executing command [Start-Process -FilePath "http://hanseldocker.cloudapp.net/"]
Publish completed successfully.

The interesting parts are the calls to dnx (the .NET Execution host), the warning that I started on Windows and I'm going to Linux, as well as the fact that we're using the "microsoft/aspnet" docker image.

有趣的部分是对dnx(.NET执行主机)的调用,在Windows上启动并且要在Linux上发出的警告以及我们正在使用“ microsoft / aspnet”泊坞窗映像的事实。

ASP.NET in a Linux Docker Container

In my example, I had VS and the extension make my certificates. If I want to connect to this instance from the Windows Docker command line, I need to either pass those certs in, or set an env var. Here I'm running "ps" to see the remote docker containers in this Azure Linux VM. The Docker client looks in %USERPROFILE%\.docker for certs., so you just need to set DOCKER_HOST or pass it in like this.

在我的示例中,我使用了VS,并且扩展名制作了我的证书。 如果要从Windows Docker命令行连接到该实例,则需要传递这些证书或设置环境变量。 在这里,我运行“ ps”以查看此Azure Linux VM中的远程docker容器。 Docker客户端在%USERPROFILE%\。docker中查找证书。因此,您只需要设置DOCKER_HOST或像这样传递它即可。

C:\>docker --tls -H=tcp://hanseldocker.cloudapp.net:2376 ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6d4820044df2 webapplication6:latest "dnx . Kestrel --ser 58 minutes ago Up 58 minutes 0.0.0.0:80->80/tcp silly_poincare

It worked great. Also be sure to explore the PublishProfiles folder that gets created in your Visual Studio project under "Properties." A PowerShell script and a Shell script get created in that folder that you can use to publish your app from the command line. For example:

效果很好。 另外,请确保浏览在Visual Studio项目的“属性”下创建的PublishProfiles文件夹。 将在该文件夹中创建一个PowerShell脚本和一个Shell脚本,您可以使用该脚本从命令行发布应用程序。 例如:

.\hanseldocker-Docker-publish.ps1 -packOutput $env:USERPROFILE\AppData\Local\Temp\PublishTemp -pubxmlFile .\hanseldocker-Docker.pubxml

or from Linux:

或从Linux:

cd ProjectFolder (like WebApplication/src/WebApplication)
source dnvm.sh
dnu restore --no-cache
mkdir ~/Temp
dnu publish . --out ~/Temp/ --wwwroot-out "wwwroot" --quiet
cd Properties/PublishProfiles
chmod +x ./Docker-publish.sh
./Docker-publish.sh ./Docker.pubxml ~/Temp/

I'm looking forward a cross-platform cross-tools choice-filled future. Finally, there's a great 7 part video series here called "Docker for .NET Developers" that you should check out on Channel 9.

我期待着跨平台,跨工具选择的未来。 最后,这里有一个精彩的7部分视频系列,称为“面向.NET开发人员的Docker ”,您应该在Channel 9上查看。

Sponsor: Big thanks to Atalasoft for sponsoring the blog and feed this week! If your company works with documents, definitely check out Atalasoft's developer tools for web & mobile viewing, capture, and transformation. They've got free trials and a remarkable support team, too.

赞助商:非常感谢Atalasoft赞助了本周的博客和feed! 如果您的公司使用文档,则一定要使用Atalasoft的开发人员工具来进行Web和移动查看,捕获和转换。 他们也有免费试用版和出色的支持团队。

翻译自: https://www.hanselman.com/blog/publishing-an-aspnet-5-app-to-docker-on-linux-with-visual-studio

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值