自动部署Asp.Net Core到Docker

自动编译Asp.Net Core Web API接口
#设置代码目录和编译输出目录
$CurPath=(Resolve-Path .).Path
O u t p u t P a t h = OutputPath= OutputPath=CurPath+"\bin\publish"
#清空输出目录
Remove-Item -Path KaTeX parse error: Expected 'EOF', got '#' at position 28: …Force -Recurse #̲调用dotnet publis…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”
#压缩文件名加上日期,以后追溯
Z I P F i l e N a m e = " D e p l o y " + ZIPFileName="Deploy"+ ZIPFileName="Deploy"+CurDateString+".zip"
Z I P F i l e P a t h = ZIPFilePath= ZIPFilePath=CurPath+""+$ZIPFileName
C o m p r e s s P a t h = CompressPath= 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
#设置程序部署目录
R e m o t e D e s t i n a t i o n P a t h = RemoteDestinationPath= RemoteDestinationPath=RemotePath+“API/”
R e m o t e Z i p P a t h = RemoteZipPath= RemoteZipPath=RemotePath+$ZIPFileName
#清空程序部署目录
Invoke-Command -Session KaTeX parse error: Expected '}', got 'EOF' at end of input: …ptBlock {param(p) Remove-Item -Path $p -Recurse -Force} -ArgumentList $RemoteDestinationPath
#解压文件到程序部署目录
Invoke-Command -Session KaTeX parse error: Expected '}', got 'EOF' at end of input: …ptBlock {param(p,$dp) Expand-Archive -Path $p -DestinationPath $dp} -ArgumentList R e m o t e Z i p P a t h , RemoteZipPath, 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}
龙华大道1号http://www.kinghill.cn/LongHuaDaDao1Hao/index.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值