在構建Docker時編譯應用
一般有兩種方法在構建鏡像時進行打包應用。第一種方法就是使用基本的鏡像,該鏡像包括應用平台和構建工具,因此在Dockerfile中,復制源代碼到鏡像中並在構建鏡像時編譯app.
1. 案例1:
(1)Dockerfile內容如下
PS E:\DockeronWindows\> cat .\Dockerfile
FROM microsoft/dotnet:1.1-sdk-nanoserverWORKDIR /src
COPY src/ .RUN dotnet restore; dotnet build
CMD ["dotnet", "run"]
(2)進行構建
PS E:\DockeronWindows\Chapter02\ch02-dotnet-helloworld> docker image build --tag dockeronwindows/ch02-dotnet-helloworld .
Sending build context to Docker daemon 7.68kBStep 1/5 : FROM microsoft/dotnet:1.1-sdk-nanoserver
1.1-sdk-nanoserver: Pulling from microsoft/dotnet
bce2fbc256ea: Already exists
58518d668160: Pulling fs layer
fc38482307fa: Pulling fs layer
576bbc86ac65: Pulling fs layer
7ad73ad8deab: Pulling fs layer
ab1b680e44fb: Pulling fs layer
5f89579d77cc: Pulling fs layer
39f2f6871254: Pulling fs layer
19e7bac74ddd: Pulling fs layer
ab1b680e44fb: Waiting
5f89579d77cc: Waiting
39f2f6871254: Waiting
19e7bac74ddd: Waiting
7ad73ad8deab: Waiting
576bbc86ac65: Verifying Checksum
576bbc86ac65: Download complete
fc38482307fa: Verifying Checksum
fc38482307fa: Download complete
7ad73ad8deab: Verifying Checksum
7ad73ad8deab: Download complete
58518d668160: Verifying Checksum
58518d668160: Download complete
5f89579d77cc: Download complete
39f2f6871254: Download complete
58518d668160: Pull complete
fc38482307fa: Pull complete
576bbc86ac65: Pull complete
7ad73ad8deab: Pull complete
ab1b680e44fb: Verifying Checksum
ab1b680e44fb: Download complete
ab1b680e44fb: Pull complete
5f89579d77cc: Pull complete
39f2f6871254: Pull complete
19e7bac74ddd: Verifying Checksum
19e7bac74ddd: Download complete
19e7bac74ddd: Pull complete
Digest: sha256:784d5f6ceef9a22d0ae224ea0e81869d2ef1348fa6611f6390da992b0661adc0
Status: Downloaded newer image for microsoft/dotnet:1.1-sdk-nanoserver
---> cade360c069b
Step 2/5 : WORKDIR /src
Removing intermediate container 581b35c929dc
---> df0351fc439b
Step 3/5 : COPY src/ .
---> 1769be54acaa
Step 4/5 : RUN dotnet restore; dotnet build
---> Running in 97b2eb76f163
Restoring packages for C:\src\HelloWorld.NetCore.csproj...
Generating MSBuild file C:\src\obj\HelloWorld.NetCore.csproj.nuget.g.props.
Generating MSBuild file C:\src\obj\HelloWorld.NetCore.csproj.nuget.g.targets.
Restore completed in 10.8 sec for C:\src\HelloWorld.NetCore.csproj.
Microsoft (R) Build Engine version 15.3.409.57025 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.HelloWorld.NetCore -> C:\src\bin\Debug\netcoreapp1.1\HelloWorld.NetCore.dll
Build succeeded.
0 Warning(s)
0 Error(s)Time Elapsed 00:00:02.63
Removing intermediate container 97b2eb76f163
---> 48609d781e5b
Step 5/5 : CMD ["dotnet", "run"]
---> Running in 8906b368d1ed
Removing intermediate container 8906b368d1ed
---> f061c5025519
Successfully built f061c5025519
Successfully tagged dockeronwindows/ch02-dotnet-helloworld:latest
2. 第二種方法就是在構建鏡像之前進行編譯文件。
(1)Dockerfile文件內容如下
PS E:\DockeronWindows\Chapter02\ch02-dotnet-helloworld> cat .\Dockerfile.slim
FROM microsoft/dotnet:1.1-runtime-nanoserverWORKDIR /dotnetapp
COPY ./src/bin/Debug/netcoreapp1.1/publish .CMD ["dotnet", "HelloWorld.NetCore.dll"]
(2)簡單的編譯應用和構建Docker鏡像看起來如下:
dotnet restore src; dotnet publish src
docker image build --file Dockerfile.slim --tag dockeronwindows/ch02-dotnet-helloworld:slim .
編譯截圖:
構建鏡像截圖:
(3)可以看到,在編譯前和編譯后進行構建鏡像,兩個容器之間的大小差異,主要是在構建鏡像時進行編譯,容器需要安裝構建工具。
3. 多階段的構建和編譯
(1)Dockerfile.multistage文件內容如下
PS E:\DockeronWindows\Chapter02\ch02-dotnet-helloworld> cat .\Dockerfile.multistage
# build stage
FROM microsoft/dotnet:1.1-sdk-nanoserver AS builderWORKDIR /src
COPY src/ .RUN dotnet restore; dotnet publish
# final image stage
FROM microsoft/dotnet:1.1-runtime-nanoserverWORKDIR /dotnetapp
COPY --from=builder /src/bin/Debug/netcoreapp1.1/publish .CMD ["dotnet", "HelloWorld.NetCore.dll"]
(2)第(1)步主要是把1,2階段進行合並了,先執行編譯,然后運行第2個容器執行。
4. Dockerfile指令
(1)構建鏡像 ,Dockerfile內容如下
PS E:\DockeronWindows\Chapter02\ch02-static-website> cat .\Dockerfile
# escape=`
FROM microsoft/iis
SHELL ["powershell"]ARG ENV_NAME=DEV
EXPOSE 80
COPY template.html C:\template.html
RUN (Get-Content -Raw -Path C:\template.html) `
-replace '{hostname}', [Environment]::MachineName `
-replace '{environment}', [Environment]::GetEnvironmentVariable('ENV_NAME') `
| Set-Content -Path C:\inetpub\wwwroot\index.html
(2)進行構建
docker image build --build-arg ENV_NAME=TEST --tag dockeronwindows/ch02-static-website .
(3)運行容器
> docker container run --detach --publish 80 dockeronwindows/ch02-static-website
3472a4f0efdb7f4215d49c44dcbfc81eae0426c1fc56ad75be86f63a5abf9b0e
(4)檢查容器的IP地址
> docker container inspect --format '{{ .NetworkSettings.Networks.nat.IPAddress }}' 3472
172.26.204.5