原文链接:个人博客:自动部署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. Day01_mongoDB入门

    学于黑马和传智播客联合做的教学项目 感谢 黑马官网:http://www.itheima.com 传智播客官网:http://www.itcast.cn 微信搜索"艺术行者",关注 ...

  2. Python os.close() 方法

    概述 os.close() 方法用于关闭指定的文件描述符 fd.高佣联盟 www.cgewang.com 语法 close()方法语法格式如下: os.close(fd); 参数 fd -- 文件描述 ...

  3. 探究:编程语言那么多,为什么偏偏是 C 语言成了大学的必修课?

    谁叫你不幸生在中国了? ——何祚庥(中国科学院院士) 这是一本给非计算机专业的大学生的C语言的书.“我不是学计算机的,为啥要学C语言?”这个问题每年在中华大地都会被问上几百万次.被问的对象可能是老师, ...

  4. Redis好文章推荐

    文章来源:掘金   作者:敖丙 Redis-避免缓存穿透的利器之BloomFilter <我们一起进大厂>系列- Redis基础 <我们一起进大厂>系列-缓存雪崩.击穿.穿透 ...

  5. Angular 10材质的模态弹出示例和教程

    在本教程中,我们将通过示例使用Angular 10材质构建模式弹出窗口. 在这里,我们将研究创建Angular 10项目,安装和设置Angular 10材质,以及创建自定义材质模块文件. 在本教程中, ...

  6. Semantic Monocular SLAM for Highly Dynamic Environments面向高动态环境的语义单目SLAM

    一.摘要 当前单目SLAM系统能够实时稳定地在静态环境中运行,但是由于缺乏明显的动态异常处理能力,在动态场景变化与运动中往往会失败.作者为解决高度动态环境中的问题,提出一种语义单目SLAM架构,结合基 ...

  7. 实验11——java线程模拟卖票

    package cn.tedu.demo; /** * @author 赵瑞鑫 E-mail:1922250303@qq.com * @version 1.0 * @创建时间:2020年7月31日 下 ...

  8. property补充

    property补充 # class Foo: # @property # def AAA(self): # print('get的时候运行我啊') # # @AAA.setter # def AAA ...

  9. 铁大树洞APP视频讲解和原型演示

    首先放上我们团队视频讲解演示的视频:https://v.youku.com/v_show/id_XNDYyMzA3MTgzNg==.html 团队名称:超能陆战队 团队成员:刘梦鑫(队长) 段行行 徐 ...

  10. C# 8.0 的新特性概览和讲解

    本文转自 https://blog.csdn.net/hez2010/article/details/84036742 C# 8.0 的新特性概览和讲解 前言 新的改变 可空引用类型(Nullable ...