wsl 上使用docker_首先通过在WSL和Docker中进行测试,将ASP.NET Core从Windows上的Azure应用服务迁移到Linux

wsl 上使用docker

wsl 上使用docker

I updated one of my websites from ASP.NET Core 2.2 to the latest LTS (Long Term Support) version of ASP.NET Core 3.1 this week. Now I want to do the same with my podcast site AND move it to Linux at the same time. Azure App Service for Linux has some very good pricing and allowed me to move over to a Premium v2 plan from Standard which gives me double the memory at 35% off.

我本周将自己的网站从ASP.NET Core 2.2更新到了ASP.NET Core 3.1的最新LTS(长期支持)版本。 现在,我想对播客站点执行相同的操作,并将其同时移至Linux。 适用于Linux的Azure应用服务的价格非常优惠,使我可以从Standard转到Premium v​​2计划,这使我的内存减少了35%,增加了一倍。

My podcast has historically run on ASP.NET Core on Azure App Service for Windows. How do I know if it'll run on Linux? Well, I'll try it see!

我的播客历来在Windows Azure应用服务上的ASP.NET Core上运行。 我怎么知道它是否可以在Linux上运行? 好吧,我会试试看!

I use WSL (Windows Subsystem for Linux) and so should you. It's very likely that you have WSL ready to go on you machine and you just haven't turned it on. Combine WSL (or the new WSL2) with the Windows Terminal and you're in a lovely spot on Windows with the ability to develop anything for anywhere.

我使用WSL( Linux的Windows子系统),所以也应该使用。 WSL很可能已经准备好在计算机上运行,​​而您还没有打开它。 将WSL(或新的WSL2)与Windows Terminal结合使用,可以在任何地方开发任何东西,因此您在Windows上处于一个可爱的位置。

First, let's see if I can run my existing ASP.NET Core podcast site (now updated to .NET Core 3.1) on Linux. I'll start up Ubuntu 18.04 on Windows and run dotnet --version to see if I have anything installed already. You may have nothing. I have 3.0 it seems:

首先,让我们看看我是否可以在Linux上运行现有的ASP.NET Core播客站点(现已更新为.NET Core 3.1 )。 我将在Windows上启动Ubuntu 18.04并运行dotnet --version以查看是否已安装任何东西。 你可能什么都没有。 我似乎有3.0:

$ dotnet --version
3.0.100

Ok, I'll want to install .NET Core 3.1 on WSL's Ubuntu instance. Remember, just because I have .NET 3.1 installed in Windows doesn't mean it's installed in my Linux/WSL instance(s). I need to maintain those on my own. Another way to think about it is that I've got the win-x64 install of .NET 3.1 and now I need the linux-x64 one.

好的,我要在WSL的Ubuntu实例上安装.NET Core 3.1。 请记住,仅因为我在Windows中安装了.NET 3.1并不意味着它已安装在我Linux / WSL实例中。 我需要自己维护这些。 考虑它的另一种方法是,我已经安装了.NET 3.1的win-x64,现在需要linux-x64。

  • NOTE: It is true that I could "dotnet publish -r linux-x64" and then scp the resulting complete published files over to Linux/WSL. It depends on how I want to divide responsibility. Do I want to build on Windows and run on Linux/Linux? Or do I want to build and run from Linux. Both are valid, it just depends on your choices, patience, and familiarity.

    注意:的确,我可以“ dotnet publish -r linux-x64 ”,然后将生成的完整的已发布文件压缩到Linux / WSL。 这取决于我要如何划分责任。 我要在Windows上构建并在Linux / Linux上运行吗? 还是我想从Linux构建并运行。 两者都是有效的,仅取决于您的选择,耐心和熟悉程度。

  • GOTCHA: Also if you're accessing Windows files at /mnt/c under WSL that were git cloned from Windows, be aware that there are subtleties if Git for Windows and Git for Ubuntu are accessing the index/files at the same time. It's easier and safer and faster to just git clone another copy within the WSL/Linux filesystem.

    GOTCHA:同样,如果您正在从Windows git克隆的WSL下以/ mnt / c访问Windows文件,请注意,如果Windows的Git和Ubuntu的Git同时访问索引/文件,则有些微妙之处。 git在WSL / Linux文件系统中克隆另一个副本将更加容易,安全和快捷。

I'll head over to https://dotnet.microsoft.com/download and get .NET Core 3.1 for Ubuntu. If you use apt, and I assume you do, there's some preliminary setup and then it's a simple

我将转到https://dotnet.microsoft.com/download并获得适用于Ubuntu的.NET Core 3.1。 如果您使用apt,并且我认为您会这样做,则需要进行一些初步设置,然后再进行简单的设置

sudo apt-get install dotnet-sdk-3.1

No sweat. Let's "dotnet build" and hope for the best!

没汗让我们“ dotnet build ”,并希望最好!

Building my site under WSL

It might be surprising but if you aren't doing anything tricky or Windows-specific, your .NET Core app should just build the same on Windows as it does on Linux. If you ARE doing something interesting or OS-specific you can #ifdef your way to glory if you insist.

可能令人惊讶,但是如果您没有做任何棘手的操作或Windows特有的操作,您的.NET Core应用程序应该在Windows上构建与在Linux上相同的应用程序。 如果您正在做有趣的事情或特定于操作系统的事情,则可以坚持坚持#ifdef荣耀之路。

Bonus points if you have Unit Tests - and I do - so next I'll run my unit tests and see how it goes.

如果您有单元测试,那么我会获得加分-我也有-因此,下一步,我将运行单元测试,看看测试如何进行。

OPTION: I write things like build.ps1 and test.ps1 that use PowerShell as PowerShell is on Windows already. Then I install PowerShell (just for the scripting, not the shelling) on Linux so I can use my .ps1 scripts everywhere. The same test.ps1 and build.ps1 and dockertest.ps1, etc just works on all platforms. Make sure you have a shebang #!/usr/bin/pwsh at the top of your ps1 files so you can just run them (chmod +x) on Linux.

选项:我编写了像Windows一样使用PowerShell作为PowerShell的build.ps1和test.ps1之类的东西。 然后,我在Linux上安装PowerShell(仅用于脚本编制,而不是用于脱壳),以便可以在任何地方使用.ps1脚本。 相同的test.ps1和build.ps1和dockertest.ps1等仅适用于所有平台。 确保ps1文件顶部有一个shebang #!/usr/bin/pwsh ,以便可以在Linux上运行它们( chmod +x )。

I run test.ps1 which runs this command

我运行运行此命令的test.ps1

dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=lcov /p:CoverletOutput=./lcov .\hanselminutes.core.tests

with coverlet for code coverage and...it works! Again, this might be surprising but if you don't have any hard coded paths, make any assumptions about a C:\ drive existing, and avoid the registry and other Windows-specific things, things work.

带有覆盖率的代码覆盖和...它的作品! 同样,这可能令人惊讶,但是如果您没有任何硬编码的路径,则对现有的C:\驱动器进行任何假设,并避免使用注册表和其他Windows特定的东西,那么一切都会起作用。

Test Run Successful.
Total tests: 23
Passed: 23
Total time: 9.6340 Seconds

Calculating coverage result...
Generating report './lcov.info'

+--------------------------+--------+--------+--------+
| Module | Line | Branch | Method |
+--------------------------+--------+--------+--------+
| hanselminutes.core.Views | 60.71% | 59.03% | 41.17% |
+--------------------------+--------+--------+--------+
| hanselminutes.core | 82.51% | 81.61% | 85.39% |
+--------------------------+--------+--------+--------+

I can build, I can test, but can I run it? What about running and testing in containers?

我可以构建,可以测试,但是可以运行吗? 在容器中运行和测试呢?

I'm running WSL2 on my system and I've doing all this in Ubuntu 18.04 AND I'm running the Docker WSL Tech Preview. Why not see if I can run my tests under Docker as well? From Docker for Windows I'll enabled the Experimental WSL2 support and then from the Resources menu, WSL Integration I'll enable Docker within my Ubuntu 18.04 instance (your instances and their names will be your own).

我在系统上运行WSL2,并且已经在Ubuntu 18.04中完成了所有这些工作,并且正在运行Docker WSL Tech Preview 。 为什么不看看我是否也可以在Docker下运行测试呢? 从Windows的Docker,我将启用实验性WSL2支持,然后从资源菜单,从WSL集成,我将在Ubuntu 18.04实例中启用Docker(您的实例及其名称将是您自己的)。

Docker under WSL2

I can confirm it's working with "docker info" under WSL and talking to a working instance. I should be able to run "docker info" in BOTH Windows AND WSL.

我可以确认它正在WSL下与“ docker info”一起使用并正在与一个工作实例通信。 我应该能够同时在Windows和WSL中运行“ docker info”。

$ docker info
Client:
Debug Mode: false

Server:
Containers: 18
Running: 18
Paused: 0
Stopped: 0
Images: 31
Server Version: 19.03.5
Storage Driver: overlay2
Backing Filesystem: extfs
...snip...

Cool. I remembered I also I needed to update my Dockerfile as well from the 2.2 SDK on the Docker hub to the 3.1 SDK from Microsoft Container Registry, so this one line change:

凉。 我记得我还需要将Dockerfile也从Docker集线器上的2.2 SDK更新到Microsoft Container Registry中的3.1 SDK,因此这一行发生了变化:

#FROM microsoft/dotnet:2.2-sdk AS build
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as build

as well as the final runtime version for the app later in the Dockerfile. Basically make sure your Dockerfile uses the right versions.

以及稍后在Dockerfile中为该应用程序提供的最终运行时版本。 基本上,请确保您的Dockerfile使用正确的版本。

#FROM microsoft/dotnet:2.1-aspnetcore-runtime AS runtime
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime

I also volume mount the tests results so there's this offensive If statement in the test.ps1. YES, I know I should just do all the paths with / and make them relative.

我还批量装入了测试结果,因此在test.ps1中有此令人讨厌的If语句。 是的,我知道我应该只使用/进行所有路径设置并使其相对。

#!/usr/bin/pwsh
docker build --pull --target testrunner -t podcast:test .
if ($IsWindows)
{
 docker run --rm -v d:\github\hanselminutes-core\TestResults:/app/hanselminutes.core.tests/TestResults podcast:test
}
else
{
 docker run --rm -v ~/hanselminutes-core/TestResults:/app/hanselminutes.core.tests/TestResults podcast:test
}

Regardless, it works and it works wonderfully. Now I've got tests running in Windows and Linux and in Docker (in a Linux container) managed by WSL2. Everything works everywhere. Now that it runs well on WSL, I know it'll work great in Azure on Linux.

无论如何,它都能正常工作,而且效果出色。 现在,我已经在Windows和Linux以及WSL2管理的Docker(在Linux容器中)中运行测试。 一切都随处可见。 现在它在WSL上运行良好,我知道它将在Linux上的Azure中很好地工作。

从Windows上的Azure应用服务迁移到Linux (Moving from Azure App Service on Windows to Linux)

This was pretty simple as well.

这也很简单。

I'll blog in detail how I build andd eploy the sites in Azure DevOps and how I've moved from .NET 2.2 with Classic "Wizard Built" DevOps Pipelines to a .NET Core 3.1 and a source control checked-in YAML pipeline next week.

我将详细介绍如何在Azure DevOps中构建和部署站点,以及如何从具有经典“向导创建” DevOps管道的.NET 2.2迁移到.NET Core 3.1和已签入源代码控制的YAML管道。周。

The short version is, make a Linux App Service Plan (remember that an "App Service Plan " is a VM that you don't worry about. See in the pick below that the Linux Plan has a penguin icon. Also remember that you can have as many apps inside your plan as you'd like (and will fit in memory and resources). When you select a "Stack" for your app within Azure App Service for Linux you're effectively selecting a Docker Image that Azure manages for you.

简短的版本是制定一份Linux应用服务计划(请记住,“应用服务计划”是您不必担心的虚拟机。请在下面的选择中看到Linux计划带有企鹅图标。另外请记住,您可以在计划中拥有尽可能多的应用程序(并且将适合内存和资源)。当您在适用于Linux的Azure App Service中为您的应用程序选择一个“堆栈”时,您实际上是在选择一个Azure管理的Docker映像您。

I started by deploying to staging.mydomain.com and trying it out. You can use Azure Front Door or CloudFlare to manage traffic and then swap the DNS. I tested on Staging for a while, then just changed DNS directly. I waited a few hours for traffic to drain off the Windows podcast site and then stopped it. After a day or two of no traffic I deleted it. If I did my job right, none of you noticed the site moved from Windows to Linux, from .NET Core 2.2 to .NET Core 3.1. It should be as fast or faster with no downtime.

我首先部署到staging.mydomain.com并进行了尝试。 您可以使用Azure前门或CloudFlare来管理流量,然后交换DNS。 我在暂存中测试了一段时间,然后直接更改了DNS。 我等了几个小时,使流量耗尽了Windows播客站点,然后停止了该站点。 一两天没有流量后,我将其删除。 如果我的工作做对了,没人会注意到该站点从Windows迁移到Linux,从.NET Core 2.2迁移到.NET Core 3.1。 它应该与没有停机的速度一样快或更快。

Here's a snap of my Azure Portal. As of today, I've moved my home page, my blood sugar management portal, and my podcast site all onto a single Linux App Service Plan. Each is hosted on GitHub and each is deploying automatically with Azure DevOps.

这是我的Azure门户的快照。 从今天起,我已经将主页血糖管理门户和播客站点都移到了一个Linux App Service Plan中。 每个都托管在GitHub上,并且每个都使用Azure DevOps自动部署。

Azure Plan with 3 apps on Linux

Next big migration to the cloud will be this blog which still runs .NET Framework 4.x. I'll blog how the podcast gets checked into GitHub then deployed with Azure DevOps next week.

下一个向云的大规模迁移将是仍然运行.NET Framework 4.x的此博客。 我将在下周博客播客如何登录到GitHub,然后将其与Azure DevOps一起部署。

What cool migrations have YOU done lately, Dear Reader?

亲爱的读者,您最近做了哪些出色的迁移?

Sponsor: Like C#? We do too! That’s why we've developed a fast, smart, cross-platform .NET IDE which gives you even more coding power. Clever code analysis, rich code completion, instant search and navigation, an advanced debugger... With JetBrains Rider, everything you need is at your fingertips. Code C# at the speed of thought on Linux, Mac, or Windows. Try JetBrains Rider today!

赞助商:喜欢C#吗? 我们也这样做! 这就是为什么我们开发了一个快速,智能,跨平台的.NET IDE的原因,它为您提供了更多的编码能力。 巧妙的代码分析,丰富的代码完成,即时搜索和导航,高级调试器...使用JetBrains Rider,您所需的一切都唾手可得。 在Linux,Mac或Windows上以思考的速度编写C#代码。 立即尝试JetBrains Rider

翻译自: https://www.hanselman.com/blog/moving-an-aspnet-core-from-azure-app-service-on-windows-to-linux-by-testing-in-wsl-and-docker-first

wsl 上使用docker

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值