• 打包应用和构建Docker镜像(docker在windows上)


    在构建Docker时编译应用

    一般有两种方法在构建镜像时进行打包应用。第一种方法就是使用基本的镜像,该镜像包括应用平台和构建工具,因此在Dockerfile中,复制源代码到镜像中并在构建镜像时编译app.

    1. 案例1:

    (1)Dockerfile内容如下

    PS E:DockeronWindows> cat .Dockerfile
    FROM microsoft/dotnet:1.1-sdk-nanoserver

    WORKDIR /src
    COPY src/ .

    RUN dotnet restore; dotnet build
    CMD ["dotnet", "run"]


    (2)进行构建

    PS E:DockeronWindowsChapter02ch02-dotnet-helloworld> docker image build --tag dockeronwindows/ch02-dotnet-helloworld .
    Sending build context to Docker daemon   7.68kB

    Step 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:srcHelloWorld.NetCore.csproj...
       Generating MSBuild file C:srcobjHelloWorld.NetCore.csproj.nuget.g.props.
       Generating MSBuild file C:srcobjHelloWorld.NetCore.csproj.nuget.g.targets.
       Restore completed in 10.8 sec for C:srcHelloWorld.NetCore.csproj.
    Microsoft (R) Build Engine version 15.3.409.57025 for .NET Core
    Copyright (C) Microsoft Corporation. All rights reserved.

      HelloWorld.NetCore -> C:srcinDebug etcoreapp1.1HelloWorld.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:DockeronWindowsChapter02ch02-dotnet-helloworld> cat .Dockerfile.slim
    FROM microsoft/dotnet:1.1-runtime-nanoserver

    WORKDIR /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 .


    编译截图:

    V{`Q_6QEPW@]HN3XPDUEE}N[4]

    构建镜像截图:

    }%HV)ATV0VSBAFO8F7G4QY7



    (3)可以看到,在编译前和编译后进行构建镜像,两个容器之间的大小差异,主要是在构建镜像时进行编译,容器需要安装构建工具。

    (NC5%SJ@5$HCWEN[L}0UJET


    3. 多阶段的构建和编译

    (1)Dockerfile.multistage文件内容如下

    PS E:DockeronWindowsChapter02ch02-dotnet-helloworld> cat .Dockerfile.multistage
    # build stage
    FROM microsoft/dotnet:1.1-sdk-nanoserver AS builder

    WORKDIR /src
    COPY src/ .

    RUN dotnet restore; dotnet publish

    # final image stage
    FROM microsoft/dotnet:1.1-runtime-nanoserver

    WORKDIR /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:DockeronWindowsChapter02ch02-static-website> cat .Dockerfile
    # escape=`
    FROM microsoft/iis
    SHELL ["powershell"]

    ARG ENV_NAME=DEV

    EXPOSE 80

    COPY template.html C: emplate.html

    RUN (Get-Content -Raw -Path C: emplate.html) `
          -replace '{hostname}', [Environment]::MachineName `
          -replace '{environment}', [Environment]::GetEnvironmentVariable('ENV_NAME') `
         | Set-Content -Path C:inetpubwwwrootindex.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


    (5)可以通过本机的浏览器进行访问

    image

  • 相关阅读:
    Java
    Java
    Java
    Java
    运算问题
    Idea常用快捷键
    java变量和变量命名规范
    java常用数据类型和基本数据类型转换和进制和大数运算
    java注释和标识符规范
    使用命令行生成的第一个java程序
  • 原文地址:https://www.cnblogs.com/zangxueyuan/p/9151340.html
Copyright © 2020-2023  润新知