asp.net core部署 docker for windows

创建 Dockerfile 文件

项目根目录创建 Dockerfile 文件,文件内容:


FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base    
WORKDIR /app
COPY . .  
EXPOSE 5000  
ENTRYPOINT ["dotnet", "WebApplication2.dll"]  

修改项目端口号

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
                              {
                                  webBuilder.UseUrls("http://*:5000").UseStartup<Startup>();
                });

发布项目

发布到本地,目标运行时设置为可移植
在这里插入图片描述

进入到发布后的目录
在这里插入图片描述

创建镜像

查看所有iamge:

C:\Users\Administrator>docker image ls
REPOSITORY                        TAG       IMAGE ID       CREATED             SIZE
aspnetcoredocker1.3               latest    9d320d2374bc   37 minutes ago      353MB
aspnetcoredocker1.2               latest    0762b8f89512   About an hour ago   353MB
aspnetcoredocker1.1               latest    0762b8f89512   About an hour ago   353MB
mcr.microsoft.com/dotnet/aspnet   3.1       87c77e6f43d8   10 hours ago        348MB

构建asp.net core docker image

F:\source\dotnet\dotnetcore_docker_demo\WebApplication1\WebApplication2\bin\Release\netcoreapp3.1\publish>docker build -t aspnetcoredocker1.4 .
Sending build context to Docker daemon   4.77MB
Step 1/5 : FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
 ---> 87c77e6f43d8
Step 2/5 : WORKDIR /app
 ---> Using cache
 ---> 43e717ae7b0e
Step 3/5 : COPY . .
 ---> Using cache
 ---> 340677c838e1
Step 4/5 : EXPOSE 5000
 ---> Using cache
 ---> 45a31da45914
Step 5/5 : ENTRYPOINT ["dotnet", "WebApplication2.dll"]
 ---> Using cache
 ---> 9d320d2374bc
Successfully built 9d320d2374bc
Successfully tagged aspnetcoredocker1.4:latest

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them

再次查看镜像会发现多了一个

C:\Users\Administrator>docker image ls
REPOSITORY                        TAG       IMAGE ID       CREATED             SIZE
aspnetcoredocker1.1               latest    9d320d2374bc   43 minutes ago      353MB
aspnetcoredocker1.3               latest    9d320d2374bc   43 minutes ago      353MB
aspnetcoredocker1.4               latest    9d320d2374bc   43 minutes ago      353MB
aspnetcoredocker1.2               latest    0762b8f89512   About an hour ago   353MB
mcr.microsoft.com/dotnet/aspnet   3.1       87c77e6f43d8   10 hours ago        348MB

可视化查看镜像

在这里插入图片描述

启动容器

查看所有启动的容器:

C:\Users\Administrator>docker container ls
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                    NAMES
f681c970238e   aspnetcoredocker1.3   "dotnet WebApplicati…"   38 minutes ago   Up 38 minutes   0.0.0.0:5000->5000/tcp   stoic_murdock

运行容器


```powershell
docker run -it -p 5000:5000 aspnetcoredocker1.4

warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
      Storing keys in a directory 'C:\Users\ContainerUser\AppData\Local\ASP.NET\DataProtection-Keys' that may not be per
sisted outside of the container. Protected data will be unavailable when container is destroyed.
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://[::]:5000
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\app

可视化界面查看运行的容器
在这里插入图片描述
在这里插入图片描述

运行

浏览器查看运行情况
在这里插入图片描述

请关注公众号 “开源者之家”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值