原文链接:个人博客:自动部署Asp.Net Core至Docker

本文简介

最近在开发一个管理系统,代码框架是用的前后台分离的方式

后台使用的是Asp.Net Core平台,开发所有业务,向前台提供Rest API接口。

使用的认证方式是JWT

前端有两个项目,一个是Web端,一个是Mobild端

都是使用Vue + Ant Design of Vue架构

后端的开发工具使用的是Visual Studio 2019

前端的开发工具使用的是Visual Studio Code

在这前我也写过通过PowerShell自动部署Asp.Net Core程序到Windows服务器

并使用IIS向外提供服务。

使用PowerShell自动编译部署

为了使项目实现运行在全开源平台,实现低成本、安全、高可用的目的

所以写这个文章以实现自动部署系统至Ubuntu平台使用Docker对外提供服务

本文章只实现后端接口项目(Rest API)的部署

本文所有自动部署代码是基于PowerShell

实现目标

  1. 在Windows平台自动编译API接口
  2. 把编译生成的文件发布到Ubuntu服务器
  3. 在Ubuntu服务器使用Docker对外提供服务

前置条件

  1. Ubuntu服务器启用了SSH,并可以使用RSA Key登录root 参考文档:Ubuntu系统配置SSH服务
  2. Ubuntu服务器安装了PowerShell 参考文档:使用PowerShell操作Ubuntu
  3. Ubuntu服务器安装了Docker 参考文档:Ubuntu安装Docker

自动编译Asp.Net Core Web API接口

#设置代码目录和编译输出目录
$CurPath=(Resolve-Path .).Path
$OutputPath=$CurPath+"\bin\publish\"
#清空输出目录
Remove-Item -Path $OutputPath -Force -Recurse
#调用dotnet publish命令发布程序
#参考:https://docs.microsoft.com/zh-cn/dotnet/core/tools/dotnet-publish
Invoke-Command -ScriptBlock {param($o) dotnet publish -o $o -c "Release" --no-self-contained -r linux-arm64 -v m --nologo "05.Coldairarrow.Api.csproj"} -ArgumentList $OutputPath #压缩编译后的发布文件
$CurDateString=Get-Date -Format "yyyyMMddHHmmss"
#压缩文件名加上日期,以后追溯
$ZIPFileName="Deploy"+$CurDateString+".zip"
$ZIPFilePath=$CurPath+"\"+$ZIPFileName
$CompressPath=$OutputPath+"*"
#压缩文件
#Path:压缩对象,DestinationPath:输出压缩文件全路径
Compress-Archive -Path $CompressPath -DestinationPath $ZIPFilePath

把压缩后的编译文件发布到服务器

#使用RSA Key免密登录Ubuntu SSH
$Session = New-PSSession -HostName 10.76.20.162 -UserName root -KeyFilePath "C:\Users\Administrator\.ssh\id_rsa"
#设置远程服务器部署路径
$RemotePath="/srv/Deploy/"
#复制文件到服务器
Copy-Item $ZIPFilePath -Destination $RemotePath -ToSession $Session
#设置程序部署目录
$RemoteDestinationPath=$RemotePath+"API/"
$RemoteZipPath=$RemotePath+$ZIPFileName
#清空程序部署目录
Invoke-Command -Session $Session -ScriptBlock {param($p) Remove-Item -Path $p -Recurse -Force} -ArgumentList $RemoteDestinationPath
#解压文件到程序部署目录
Invoke-Command -Session $Session -ScriptBlock {param($p,$dp) Expand-Archive -Path $p -DestinationPath $dp} -ArgumentList $RemoteZipPath,$RemoteDestinationPath
#删除本的压缩文件
Remove-Item -Path $ZIPFilePath

Docker对外提供服务

在程序部署目录配置Dockerfile

#拉取asp.net core镜像
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
#设置工作目录
WORKDIR /app
#把服务器文件复制到Docker
COPY . .
#对外开放5000端口
EXPOSE 5000
#启动API命令
ENTRYPOINT ["dotnet","Coldairarrow.Api.dll"]

构建API镜像,并启动新容器

#停止容器
Invoke-Command -Session $Session -ScriptBlock {docker stop api}
#删除容器
Invoke-Command -Session $Session -ScriptBlock {docker rm api}
#删除镜像
Invoke-Command -Session $Session -ScriptBlock {docker rmi api}
#通过Dockerfile构建镜像
Invoke-Command -Session $Session -ScriptBlock {docker build -t api /srv/Deploy/API}
#启动新的容器
Invoke-Command -Session $Session -ScriptBlock {docker run -d -p 5000:5000 --name api api}

部署结果

部署成功后,我们在浏览器里打开

http://10.76.20.162:5000/swagger/

就可以看到我们发布的API接口

源代码

Write-Host 'Build Starting' -ForegroundColor Yellow
$CurPath=(Resolve-Path .).Path
$OutputPath=$CurPath+"\bin\publish\"
Remove-Item -Path $OutputPath -Force -Recurse
Invoke-Command -ScriptBlock {param($o) dotnet publish -o $o -c "Release" --no-self-contained -r linux-arm64 -v m --nologo "05.Coldairarrow.Api.csproj"} -ArgumentList $OutputPath
Write-Host 'Build Completed' -ForegroundColor Green Write-Host 'Compress Starting' -ForegroundColor Yellow
$CurDateString=Get-Date -Format "yyyyMMddHHmmss"
$ZIPFileName="Deploy"+$CurDateString+".zip"
$ZIPFilePath=$CurPath+"\"+$ZIPFileName
$CompressPath=$OutputPath+"*"
Compress-Archive -Path $CompressPath -DestinationPath $ZIPFilePath
Write-Host 'Compress Completed' -ForegroundColor Green Write-Host 'Deploy Starting' -ForegroundColor Yellow
$Session = New-PSSession -HostName 10.76.20.162 -UserName root -KeyFilePath "C:\Users\Administrator\.ssh\id_rsa"
$Session
Write-Host 'Successfully connected to the server' -ForegroundColor Green
Write-Host 'Start copying files to the server' -ForegroundColor Yellow
$RemotePath="/srv/Deploy/"
Copy-Item $ZIPFilePath -Destination $RemotePath -ToSession $Session
Write-Host 'Copy files completed' -ForegroundColor Green
Write-Host 'Start Expand files on the server' -ForegroundColor Yellow
$RemoteDestinationPath=$RemotePath+"API/"
$RemoteZipPath=$RemotePath+$ZIPFileName
Invoke-Command -Session $Session -ScriptBlock {param($p) Remove-Item -Path $p -Recurse -Force} -ArgumentList $RemoteDestinationPath
Invoke-Command -Session $Session -ScriptBlock {param($p,$dp) Expand-Archive -Path $p -DestinationPath $dp} -ArgumentList $RemoteZipPath,$RemoteDestinationPath $ConfigProductionFile=$RemoteDestinationPath+"appsettings.Production.json"
Invoke-Command -Session $Session -ScriptBlock {param($p) Remove-Item -Path $p -Force} -ArgumentList $ConfigProductionFile
Write-Host 'Expand Completed' -ForegroundColor Green Write-Host 'Deploy to Docker Starting' -ForegroundColor Yellow
Invoke-Command -Session $Session -ScriptBlock {docker stop api}
Invoke-Command -Session $Session -ScriptBlock {docker rm api}
Invoke-Command -Session $Session -ScriptBlock {docker rmi api}
Invoke-Command -Session $Session -ScriptBlock {docker build -t api /srv/Deploy/API}
Invoke-Command -Session $Session -ScriptBlock {docker run -d -p 5000:5000 --name api api}
Write-Host 'Deploy to Docker Completed' -ForegroundColor Green Remove-Item -Path $ZIPFilePath
Write-Host 'Deploy Completed' -ForegroundColor Green

自动部署Asp.Net Core到Docker的更多相关文章

  1. Gitlab CI 自动部署 asp.net core web api 到Docker容器

    为什么要写这个? 在一个系统长大的过程中会经历不断重构升级来满足商业的需求,而一个严谨的商业系统需要高效.稳定.可扩展,有时候还不得不考虑成本的问题.我希望能找到比较完整的开源解决方案来解决持续集成. ...

  2. PowerShell自动部署ASP.NET Core程序到 IIS

    Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用 .NET Framework的强大功能.有关于更多PowerShell的信息,可参阅百度词条 接 ...

  3. ASP.NET Core使用Docker进行容器化托管和部署

    一.课程介绍 人生苦短,我用.NET Core!今天给大家分享一下Asp.Net Core以Docker进行容器化部署托管,本课程并不是完完全全的零基础Docker入门教学,课程知识点难免有没覆盖全面 ...

  4. Docker + Jenkins 持续部署 ASP.NET Core 项目

    Docker 是个好东西,特别是用它来部署 ASP.NET Core Web 项目的时候,但是仅仅的让程序运行起来远远不能满足我的需求,如果能够像 DaoCloud 提供的持续集成服务那样,检测 gi ...

  5. 在 Docker 中手工部署 ASP.NET Core 应用

    另一篇:在 Visual Studio 中部署 ASP.NET Core 应用  操作步骤 1. 安装 Docker For Windows(安装之前 Windows 需要开启 Hyper-V 虚拟机 ...

  6. 从零实操基于WSL2 Docker部署Asp.Net Core项目

    前言 平日在公司里都是基于阿里Teambition中的飞流进行Docker部署Api项目或服务,已经习惯了那一套成熟的操作流程,开发和部署确实快捷方便,但是还没在自己的电脑上进行操作过,特别是Wind ...

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

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

  8. 使用docker来部署asp.net core的程序

    使用docker来部署asp.net core程序 暂不介绍docker是个什么东西?不知道的自己百度. 第一步安装docker: 我的docker是装在centos7系统上,windows上我的也用 ...

  9. Centos下使用Docker部署asp.net core项目

    本文讲述 CentOS 系统 Docker 中部署 asp.net core开源项目 abp 的过程 步骤 1. 拉取 asp.net core 基础镜像 docker pull microsoft/ ...

随机推荐

  1. LIMS/QMS产品索引

    Starlims https://www.cnblogs.com/mahongbiao/p/12863304.html 客户申请门户/客户服务门户 https://www.cnblogs.com/ma ...

  2. 无法安装 VMware Tools。尝试访问安装 VMware Tools 所需的图像文件“/usr/lib/vmware/isoimages/linuxPreGlibc25.iso”时出错: 2 (No such file or directory)。请参考产品文档或知识库文章 2129825,了解关于如何获取该客户机操作系统的 VMware Tools 软件包的详细信息。

    无法安装 VMware Tools.尝试访问安装 VMware Tools 所需的图像文件"/usr/lib/vmware/isoimages/linuxPreGlibc25.iso&quo ...

  3. python绝技:运用python成为顶级黑客|中文pdf完整版[42MB|网盘地址附提取码自行提取|

    Python 是一门常用的编程语言,它不仅上手容易,而且还拥有丰富的支持库.对经常需要针对自己所 处的特定场景编写专用工具的黑客.计算机犯罪调查人员.渗透测试师和安全工程师来说,Python 的这些 ...

  4. PHP getNamespaces() 函数

    实例 返回 XML 文档中使用的命名空间: <?php$xml=<<<XML高佣联盟 www.cgewang.com<?xml version="1.0&quo ...

  5. 使用ST-Link下载程序出现Error:Flash Download Failed-“Cortex-M3“ 解决详细步骤(附图)

    我一直用stm32 f407开发. 最近要学mqtt与阿里云联网之类的课程,因为没有做过,所以网上搜了一遍,结果全是stm32 f103c8t6的例程. 后来我就搬出我的f103最小系统版 (这个就为 ...

  6. 串口通信—USB转串口

    如何使用c库printf

  7. 17、Java 三大特性之 多态

    知识点:多态的概念.java中多态的使用(方法重载和重写.子类对象的多态性) .多态使用的好处 1.什么是多态? 所谓多态就是指程序中定义的引用变量所指向的具体类型和通过该引用变量发出的方法调用在编程 ...

  8. 谈下APP测试和WEB测试的区别

    先来讲下相同点: 1.都需要理论知识,相同的用例设计方法:边界值,等价类,错误推导法,场景法 2.同样的测试方法 验证功能是否满足需求 3.都需要检查UI  界面设计是否合理 4.性能检测  并发 吞 ...

  9. go微服务系列(一) go micro入门

    1. 什么是go micro 1.1 go micro作用 1.2 go micro架构组成 2. go micro入门 3. 结合consul进行服务注册/发现 3.1 consul的安装 3.2 ...

  10. 漫画 | 到底是什么让IT人如此苦逼???

    写在最后 漫画是有点夸张,不过多少还是有点现实开发过程的影子! 老板很乐观,核心就是三个月上线,至于怎么办那是底下人的事. 产品很无奈,心里盘算着,万万不可在他这一环节耽误了进度,时间这么赶,先出个壳 ...