自动部署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向外提供服务。
为了使项目实现运行在全开源平台,实现低成本、安全、高可用的目的
所以写这个文章以实现自动部署系统至Ubuntu平台使用Docker对外提供服务
本文章只实现后端接口项目(Rest API)的部署
本文所有自动部署代码是基于PowerShell
实现目标
- 在Windows平台自动编译API接口
- 把编译生成的文件发布到Ubuntu服务器
- 在Ubuntu服务器使用Docker对外提供服务
前置条件
- Ubuntu服务器启用了SSH,并可以使用RSA Key登录root 参考文档:Ubuntu系统配置SSH服务
- Ubuntu服务器安装了PowerShell 参考文档:使用PowerShell操作Ubuntu
- 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的更多相关文章
- Gitlab CI 自动部署 asp.net core web api 到Docker容器
为什么要写这个? 在一个系统长大的过程中会经历不断重构升级来满足商业的需求,而一个严谨的商业系统需要高效.稳定.可扩展,有时候还不得不考虑成本的问题.我希望能找到比较完整的开源解决方案来解决持续集成. ...
- PowerShell自动部署ASP.NET Core程序到 IIS
Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用 .NET Framework的强大功能.有关于更多PowerShell的信息,可参阅百度词条 接 ...
- ASP.NET Core使用Docker进行容器化托管和部署
一.课程介绍 人生苦短,我用.NET Core!今天给大家分享一下Asp.Net Core以Docker进行容器化部署托管,本课程并不是完完全全的零基础Docker入门教学,课程知识点难免有没覆盖全面 ...
- Docker + Jenkins 持续部署 ASP.NET Core 项目
Docker 是个好东西,特别是用它来部署 ASP.NET Core Web 项目的时候,但是仅仅的让程序运行起来远远不能满足我的需求,如果能够像 DaoCloud 提供的持续集成服务那样,检测 gi ...
- 在 Docker 中手工部署 ASP.NET Core 应用
另一篇:在 Visual Studio 中部署 ASP.NET Core 应用 操作步骤 1. 安装 Docker For Windows(安装之前 Windows 需要开启 Hyper-V 虚拟机 ...
- 从零实操基于WSL2 Docker部署Asp.Net Core项目
前言 平日在公司里都是基于阿里Teambition中的飞流进行Docker部署Api项目或服务,已经习惯了那一套成熟的操作流程,开发和部署确实快捷方便,但是还没在自己的电脑上进行操作过,特别是Wind ...
- ASP.NET Core开发-Docker部署运行
ASP.NET Core开发Docker部署,.NET Core支持Docker 部署运行.我们将ASP.NET Core 部署在Docker 上运行. 大家可能都见识过Docker ,今天我们就详细 ...
- 使用docker来部署asp.net core的程序
使用docker来部署asp.net core程序 暂不介绍docker是个什么东西?不知道的自己百度. 第一步安装docker: 我的docker是装在centos7系统上,windows上我的也用 ...
- Centos下使用Docker部署asp.net core项目
本文讲述 CentOS 系统 Docker 中部署 asp.net core开源项目 abp 的过程 步骤 1. 拉取 asp.net core 基础镜像 docker pull microsoft/ ...
随机推荐
- PHP preg_quote() 函数
preg_last_error 函数用于转义正则表达式字符.高佣联盟 www.cgewang.com 语法 string preg_quote ( string $str [, string $del ...
- PHP xml_get_current_byte_index() 函数
定义和用法 xml_get_current_byte_index() 函数获取 XML 解析器的当前字节索引.高佣联盟 www.cgewang.com 如果成功,该函数则返回当前字节索引.如果失败,则 ...
- 4.3 省选模拟赛 序列游戏 dp
可以发现 某一段被删除后状态难以表示 也难以链接起来. 考虑暴力 有40分的状压dp 暴力存状态 然后枚举转移即可.最后注意和f[0]这个状态取max 不然一分都没有. const int MAXN= ...
- iOS苹果美区 Apple ID 账号最新注册教程,iPhone用户务必收藏!
编の语 前言 今天杀手宝宝出一个注册美区ID的教程,这是目前注册苹果美区ID最快的方法,所有人适合使用! 提の示 温馨提示: 所有内容均免费分享,部分资源来自于 网络,如与版权问题联系宝宝处理! 知道 ...
- Redis服务之常用配置(三)
上一篇博客我们聊了下redis的rdb持久化.安全连接.资源限制相关配置;回顾请参考:https://www.cnblogs.com/qiuhom-1874/p/13394411.html;今天我们来 ...
- What is 测试金字塔?
我的女朋友是一名测试工程师,但她之前却不知道测试金字塔的概念,为此我曾经在家里的白板上画了一个图一层一层给她讲解过.我和同事在给团队面试测试和开发岗位时,也会必问到这个问题,想到可能有很多开发童鞋都不 ...
- Python最全pdf学习书籍资料分享
本人学习Python两年时间,期间统计了一些比较好的学习资料 1.基础资料 下载地址: 链接:https://pan.baidu.com/s/1sjtyYayBbQLsrUdaXWmzkg提取码:1 ...
- Azure DevOps+Docker+Asp.NET Core 实现CI/CD(一 .简介与创建自己的代理池)
前言 本文主要是讲解如何使用Azure DevOps+Docker 来实现持续集成Asp.NET Core项目(当然 也可以是任意项目). 打算用三个篇幅来记录完整的全过程 觉得有帮助的朋友~可以左上 ...
- SonarQube 自定义规则开发
SonarQube 自定义规则开发 满足一些特定需求的时候,需要自己开发代码规则. 环境 和前文的演示环境一致. 步骤 开发步骤见 Writing Custom Java Rules 101,这是官方 ...
- idea括号选中时出现一条下滑线(突出显示)打开或关闭方法
打开设置 Editor->code Editing->Matched brace取消这个对勾