创建 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
可视化界面查看运行的容器


运行
浏览器查看运行情况

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

2605

被折叠的 条评论
为什么被折叠?



