Docker实践之.NET6基于Dockerfile构建Docker镜像

Docker实践之.NET6基于Dockerfile构建Docker镜像

前面都在说Docker镜像,那么Docker镜像是如何构建的呢?今天我们就来学习一下,如何基于Dockerfile来构建Docker镜像。

创建一个.NET6最小API

这里我用VS2022创建一个Docker运行的最小API,

VS会自动帮我们生产Dockerfile文件

我直接把代码上传到宿主机上,

一般我们都是上传到git,然后通过jenkins来调用shell脚本来构建镜像并上传到镜像仓库。

进入到Dockerfile目录,执行构建命令docker build -t 镜像名:版本 .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
[root@VM-12-3-centos DockerWeb]# docker build -t docker-web .
Sending build context to Docker daemon 13.82kB
Step 1/17 : FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
6.0: Pulling from dotnet/aspnet
a2abf6c4d29d: Already exists
08af7dd3c640: Pull complete
742307799914: Pull complete
a260dbcd03fc: Pull complete
96c3c696f47e: Pull complete
Digest: sha256:7696d5b456eede87434c232b9070f40659ff0c4b71ca622cf197815ccaee661d
Status: Downloaded newer image for mcr.microsoft.com/dotnet/aspnet:6.0
---> 8d32e18b77a4
Step 2/17 : WORKDIR /app
---> Running in e6c17cf8d13c
Removing intermediate container e6c17cf8d13c
---> a572d041e0da
Step 3/17 : EXPOSE 80
---> Running in d13e67c50b6d
Removing intermediate container d13e67c50b6d
---> bbbbe840e85e
Step 4/17 : EXPOSE 443
---> Running in 567ce4adadc9
Removing intermediate container 567ce4adadc9
---> dfc3eb0c4065
Step 5/17 : FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
6.0: Pulling from dotnet/sdk
a2abf6c4d29d: Already exists
08af7dd3c640: Already exists
742307799914: Already exists
a260dbcd03fc: Already exists
96c3c696f47e: Already exists
d81364490ceb: Pull complete
3e56f7c4d95f: Pull complete
9939dbdaf4a7: Pull complete
Digest: sha256:a7af03bdead8976d4e3715452fc985164db56840691941996202cea411953452
Status: Downloaded newer image for mcr.microsoft.com/dotnet/sdk:6.0
---> e86d68dca8c7
Step 6/17 : WORKDIR /src
---> Running in b3db267dfde3
Removing intermediate container b3db267dfde3
---> 1e44b66e7fcc
Step 7/17 : COPY ["DockerWeb.csproj", "."]
---> 7af02dfd823e
Step 8/17 : RUN dotnet restore "./DockerWeb.csproj"
---> Running in 22c508e5bfb8
Determining projects to restore...
Restored /src/DockerWeb.csproj (in 3.04 sec).
Removing intermediate container 22c508e5bfb8
---> 530712ad2cb4
Step 9/17 : COPY . .
---> 9f72854995f8
Step 10/17 : WORKDIR "/src/."
---> Running in 4a4a8225dd93
Removing intermediate container 4a4a8225dd93
---> b60fdb650fdc
Step 11/17 : RUN dotnet build "DockerWeb.csproj" -c Release -o /app/build
---> Running in a49f8a39451c
Microsoft (R) Build Engine version 17.0.0+c9eb9dd64 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

Determining projects to restore...
All projects are up-to-date for restore.
DockerWeb -> /app/build/DockerWeb.dll

Build succeeded.
0 Warning(s)
0 Error(s)

Time Elapsed 00:00:02.92
Removing intermediate container a49f8a39451c
---> 58c5a002da43
Step 12/17 : FROM build AS publish
---> 58c5a002da43
Step 13/17 : RUN dotnet publish "DockerWeb.csproj" -c Release -o /app/publish
---> Running in b60058485f34
Microsoft (R) Build Engine version 17.0.0+c9eb9dd64 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

Determining projects to restore...
All projects are up-to-date for restore.
DockerWeb -> /src/bin/Release/net6.0/DockerWeb.dll
DockerWeb -> /app/publish/
Removing intermediate container b60058485f34
---> c5456b5f5ad1
Step 14/17 : FROM base AS final
---> dfc3eb0c4065
Step 15/17 : WORKDIR /app
---> Running in cf76a79268ee
Removing intermediate container cf76a79268ee
---> 6a0b49e53f4a
Step 16/17 : COPY --from=publish /app/publish .
---> acf5b1cb9f98
Step 17/17 : ENTRYPOINT ["dotnet", "DockerWeb.dll"]
---> Running in da2fa62b807b
Removing intermediate container da2fa62b807b
---> bb42b5515f09
Successfully built bb42b5515f09
Successfully tagged docker-web:latest

镜像构建成功后,便可以在镜像中找到我们构建的镜像

1
2
3
[root@VM-12-3-centos DockerWeb]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-web latest bb42b5515f09 2 minutes ago 212MB

启动镜像

1
2
docker run -d --name docker-web  -p 8880:80 docker-web

这里,一个.net6从构建到启动的流程的基操就over了,微软很贴心的为我们生产了Dockerfile,使得我们的操作尤为简便。但是,大多时候需要我们自己去构建Dockerfile,那么,我们就需要去了解一下怎么编写Dockerfile

Dockerfile

我们就基于前面VS帮我们生成的Dockerfile来学习一下如何编写自己的Dockerfile,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["DockerWeb.csproj", "."]
RUN dotnet restore "./DockerWeb.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "DockerWeb.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "DockerWeb.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DockerWeb.dll"]

其实这个脚本对于入门还是稍微负责了一些,这里涉及到了多阶段构建,先不管,我们先弄清楚里面的一些关键字及作用。

  • FROM : 基于哪个镜像,可以前端构建好的阶段别名,AS 给当前层镜像定义一个别名,别名可以在后面的构建阶段中使用
  • WORKDIR:类似于cd,如果目录不存在,会自动创建该目录
  • EXPOSE:容器要开放的端口,一般是程序启动的端口,
  • COPY:拷贝文件,一般是从宿主机目录复制到镜像里的指定目录,当然它还可以再多阶段构建过程中,从前阶段复制到当前阶段,如:COPY –from=publish /app/publish .
  • RUN: 执行脚本命令
  • ENTRYPOINT:容器启动时的参数,如:ENTRYPOINT [“dotnet”, “DockerWeb.dll”],当我们启动容器时,便会执行dotnet DockerWeb.dll,如果存在多个ENTRYPOINT命令,只有最后条命令生效。
  • CMD:和ENTRYPOINT功能类似,只不过CMD可以在启动时覆盖
  • ENV:设置容器环境变量,如:ENV JAVA_OPTS="-server -Xmx2g -Xms2g"
  • VOLUME:容器挂载目录,会在容器启动时,自动挂载到宿主机。如果在docker run时指定了-v参数来映射挂载目录则会覆盖默认挂载目录。

这里我们就把前面的Dockerfile改一下,弄一个简单一点的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 基于 mcr.microsoft.com/dotnet/sdk:6.0镜像
FROM mcr.microsoft.com/dotnet/sdk:6.0
#进入到工作目录
WORKDIR /app
# 复制宿主机当前目录文件至镜像根目录
COPY . .
# 还原.net引用包
RUN dotnet restore
# 编译发布
RUN dotnet publish -c Release -o /app/publish
# 暴露端口
EXPOSE 80
# 启动运行
ENTRYPOINT ["dotnet", "DockerWeb.dll"]

这里我们新建一个名为Dockerfile-sample的脚本文件,讲上面的构建脚本拷贝进去,然后执行构建命令docker build -f Dockerfile-sample -t docker-web-sample .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 [root@VM-12-3-centos DockerWeb]# docker build -f Dockerfile-sample -t docker-web-sample .
Sending build context to Docker daemon 13.31kB
Step 1/7 : FROM mcr.microsoft.com/dotnet/sdk:6.0
---> e86d68dca8c7
Step 2/7 : WORKDIR /app
---> Running in 8c8f849824a1
Removing intermediate container 8c8f849824a1
---> 6b2f44c516d8
Step 3/7 : COPY . .
---> 0e31eac361dd
Step 4/7 : RUN dotnet restore
---> Running in a1d2a2f605ec
Determining projects to restore...
Restored /app/DockerWeb.csproj (in 3.47 sec).
Removing intermediate container a1d2a2f605ec
---> 0218643eef01
Step 5/7 : RUN dotnet publish -c Release -o /app/publish
---> Running in 0f59735014d0
Microsoft (R) Build Engine version 17.0.0+c9eb9dd64 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

Determining projects to restore...
All projects are up-to-date for restore.
DockerWeb -> /app/bin/Release/net6.0/DockerWeb.dll
DockerWeb -> /app/publish/
Removing intermediate container 0f59735014d0
---> 68cbad659576
Step 6/7 : EXPOSE 80
---> Running in b9b13621c4ee
Removing intermediate container b9b13621c4ee
---> 6fc0b9507a88
Step 7/7 : ENTRYPOINT ["dotnet", "DockerWeb.dll"]
---> Running in 569d0b39ec06
Removing intermediate container 569d0b39ec06
---> 02f1827b8c43
Successfully built 02f1827b8c43
Successfully tagged docker-web-sample:latest


这次构建并没有通过多阶段构建,那么为什么需要多阶段构建呢?我们来看一下两种构建方式的镜像文件,

可以看到,多阶段构建出来的镜像只有212MB,未进行多阶段构建出来的镜像有764MB,这在镜像推送和拉取时,镜像较小的文件的优势便体现出来了。

Docker实践之.NET6基于Dockerfile构建Docker镜像

https://blogs.52fx.biz/posts/1637136584.html

作者

eyiadmin

发布于

2021-12-29

更新于

2024-05-31

许可协议

评论