AWS CLI 在 Windows 上的 Docker 中

huangapple go评论121阅读模式
英文:

AWS CLI on windows docker

问题

  1. 请帮我更新Dockerfile以安装AWS CLI。
  1. # escape=`
  2. FROM microsoft/dotnet-framework:4.7.2-runtime
  3. SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
  4. # Install NuGet CLI
  5. ENV NUGET_VERSION 4.4.1
  6. RUN New-Item -Type Directory $Env:ProgramFiles\NuGet; `
  7. Invoke-WebRequest -UseBasicParsing https://dist.nuget.org/win-x86-commandline/v$Env:NUGET_VERSION/nuget.exe -OutFile $Env:ProgramFiles\NuGet\nuget.exe
  8. # Install AWS CLI
  9. RUN Invoke-WebRequest -UseBasicParsing https://awscli.amazonaws.com/AWSCLIV2.msi -OutFile AWSCLIV2.msi; `
  10. Start-Process msiexec.exe -ArgumentList '/i', 'AWSCLIV2.msi', '/qn', '/norestart' -NoNewWindow -Wait; `
  11. Remove-Item -Force AWSCLIV2.msi
  12. # Install VS Test Agent
  13. RUN Invoke-WebRequest -UseBasicParsing https://download.visualstudio.microsoft.com/download/pr/12210068/8a386d27295953ee79281fd1f1832e2d/vs_TestAgent.exe -OutFile vs_TestAgent.exe; `
  14. Start-Process vs_TestAgent.exe -ArgumentList '--quiet', '--norestart', '--nocache' -NoNewWindow -Wait; `
  15. Remove-Item -Force vs_TestAgent.exe; `
  16. # Install VS Build Tools
  17. Invoke-WebRequest -UseBasicParsing https://download.visualstudio.microsoft.com/download/pr/12210059/e64d79b40219aea618ce2fe10ebd5f0d/vs_BuildTools.exe -OutFile vs_BuildTools.exe; `
  18. # Installer won't detect DOTNET_SKIP_FIRST_TIME_EXPERIENCE if ENV is used, must use setx /M
  19. setx /M DOTNET_SKIP_FIRST_TIME_EXPERIENCE 1; `
  20. Start-Process vs_BuildTools.exe -ArgumentList '--add', 'Microsoft.VisualStudio.Workload.MSBuildTools', '--add', 'Microsoft.VisualStudio.Workload.NetCoreBuildTools', '--add', 'Microsoft.VisualStudio.Workload.WebBuildTools;includeRecommended', '--quiet', '--norestart', '--nocache' -NoNewWindow -Wait; `
  21. Remove-Item -Force vs_buildtools.exe; `
  22. Remove-Item -Force -Recurse "${Env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\"; `
  23. Remove-Item -Force -Recurse ${Env:TEMP}\*; `
  24. Remove-Item -Force -Recurse "${Env:ProgramData}\Package Cache\"
  25. # Set PATH in one layer to keep image size down.
  26. RUN setx /M PATH $(${Env:PATH} `
  27. + ";${Env:ProgramFiles}\NuGet" `
  28. + ";${Env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\TestAgent\Common7\IDE\CommonExtensions\Microsoft\TestWindow" `
  29. + ";${Env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\")
  30. # Install Targeting Packs
  31. RUN @('4.0', '4.5.2', '4.6.2', '4.7.2') `
  32. | %{ `
  33. Invoke-WebRequest -UseBasicParsing https://dotnetbinaries.blob.core.windows.net/referenceassemblies/v${_}.zip -OutFile referenceassemblies.zip; `
  34. Expand-Archive -Force referenceassemblies.zip -DestinationPath "${Env:ProgramFiles(x86)}\Reference Assemblies\Microsoft\Framework\.NETFramework\"; `
  35. Remove-Item -Force referenceassemblies.zip; `
  36. }
  1. 同样,您需要在构建服务器上安装Docker for Windows,以便在构建过程中可以使用Docker。不过,上述Dockerfile并没有包括Docker的安装步骤。要在Docker容器中使用Docker,您需要在Dockerfile中添加以下步骤:
  1. # Install Docker
  2. RUN Invoke-WebRequest -UseBasicParsing https://download.docker.com/win/stable/Docker%20for%20Windows%20Installer.exe -OutFile DockerForWindowsInstaller.exe; `
  3. Start-Process DockerForWindowsInstaller.exe -ArgumentList '--quiet', '--norestart' -NoNewWindow -Wait; `
  4. Remove-Item -Force DockerForWindowsInstaller.exe

请注意,这只是一个示例,您需要根据您的构建服务器和要求来安装Docker for Windows。安装Docker时,可能需要适应您的具体环境。

英文:

I am working on asp.net applications. The current plan is to setup CI pipeline in AWS with ECS. So basically I created 2 stages now.

Scenario:

I have an ASP.NET Web API application built on .NET Framework 4.6.2. I need to setup a containerized CI/CD pipeline using the AWS Code pipeline.

My initial focus to setup CI only which includes automating the source build, unit test and upload the build to ECR repository as a docker. This will be used in the next stage to deploy in ECS.

Current Progress and Problem Description:

Stage 1: Source
This stage configured successfully with the GitHub webhook.

Stage 2: Build
I used Microsoft docker ('mcr.microsoft.com/dotnet/framework/sdk:4.7.2') as a build environment image.
This is working fine for build and unit testing.

Now I need to build the docker image and push to ECR repository. I created a repository and done the commands locally. I also enabled docker support in my application so docker-compose also there.

I am stopped here as I don't have any idea to continue...

My current build spec file consists of following;

  1. version: 0.2
  2. env:
  3. variables:
  4. PROJECT: aspnetapp
  5. DOTNET_FRAMEWORK: 4.6.2
  6. phases:
  7. pre_build:
  8. commands:
  9. - echo Logging in to Amazon ECR...
  10. - aws --version
  11. - $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
  12. - REPOSITORY_URI=727003307347.dkr.ecr.eu-west-1.amazonaws.com/ecr-repo-axiapp-cloud
  13. - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
  14. - IMAGE_TAG=aspnetapi
  15. build:
  16. commands:
  17. - echo Build started on `date`
  18. - nuget restore
  19. - msbuild $env:PROJECT.sln /p:TargetFrameworkVersion=v$env:DOTNET_FRAMEWORK /p:Configuration=Release /p:DeployIisAppPath="Default Web Site" /p:PackageAsSingleFile=false /p:OutDir=C:\codebuild\artifacts\
  20. post_build:
  21. commands:
  22. - echo Build completed on `date`
  23. - echo Pushing the Docker images...
  24. - docker push $REPOSITORY_URI:latest
  25. - docker push $REPOSITORY_URI:$IMAGE_TAG
  26. artifacts:
  27. files:
  28. - '**/*'
  29. base-directory: C:\codebuild\artifacts\

While running, I got an error on aws --version. From this, I can understand that we need to install AWS CLI on the build server. For that, I am creating a custom docker. I got an articles and following the same for this.

https://aws.amazon.com/blogs/devops/extending-aws-codebuild-with-custom-build-environments-for-the-net-framework/

From the article, I have the following DockerFile

  1. # escape=`
  2. FROM microsoft/dotnet-framework:4.7.2-runtime
  3. SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
  4. #Install NuGet CLI
  5. ENV NUGET_VERSION 4.4.1
  6. RUN New-Item -Type Directory $Env:ProgramFiles\NuGet; `
  7. Invoke-WebRequest -UseBasicParsing https://dist.nuget.org/win-x86-commandline/v$Env:NUGET_VERSION/nuget.exe -OutFile $Env:ProgramFiles\NuGet\nuget.exe
  8. #Install AWS CLI
  9. RUN Invoke-WebRequest -UseBasicParsing https://s3.amazonaws.com/aws-cli/AWSCLI64PY3.msi -OutFile AWSCLI64PY3.msi
  10. # Install VS Test Agent
  11. RUN Invoke-WebRequest -UseBasicParsing https://download.visualstudio.microsoft.com/download/pr/12210068/8a386d27295953ee79281fd1f1832e2d/vs_TestAgent.exe -OutFile vs_TestAgent.exe; `
  12. Start-Process vs_TestAgent.exe -ArgumentList '--quiet', '--norestart', '--nocache' -NoNewWindow -Wait; `
  13. Remove-Item -Force vs_TestAgent.exe; `
  14. # Install VS Build Tools
  15. Invoke-WebRequest -UseBasicParsing https://download.visualstudio.microsoft.com/download/pr/12210059/e64d79b40219aea618ce2fe10ebd5f0d/vs_BuildTools.exe -OutFile vs_BuildTools.exe; `
  16. # Installer won't detect DOTNET_SKIP_FIRST_TIME_EXPERIENCE if ENV is used, must use setx /M
  17. setx /M DOTNET_SKIP_FIRST_TIME_EXPERIENCE 1; `
  18. Start-Process vs_BuildTools.exe -ArgumentList '--add', 'Microsoft.VisualStudio.Workload.MSBuildTools', '--add', 'Microsoft.VisualStudio.Workload.NetCoreBuildTools', '--add', 'Microsoft.VisualStudio.Workload.WebBuildTools;includeRecommended', '--quiet', '--norestart', '--nocache' -NoNewWindow -Wait; `
  19. Remove-Item -Force vs_buildtools.exe; `
  20. Remove-Item -Force -Recurse \"${Env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\"; `
  21. Remove-Item -Force -Recurse ${Env:TEMP}\*; `
  22. Remove-Item -Force -Recurse \"${Env:ProgramData}\Package Cache\"
  23. # Set PATH in one layer to keep image size down.
  24. RUN setx /M PATH $(${Env:PATH} `
  25. + \";${Env:ProgramFiles}\NuGet\" `
  26. + \";${Env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\TestAgent\Common7\IDE\CommonExtensions\Microsoft\TestWindow\" `
  27. + \";${Env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\")
  28. # Install Targeting Packs
  29. RUN @('4.0', '4.5.2', '4.6.2', '4.7.2') `
  30. | %{ `
  31. Invoke-WebRequest -UseBasicParsing https://dotnetbinaries.blob.core.windows.net/referenceassemblies/v${_}.zip -OutFile referenceassemblies.zip; `
  32. Expand-Archive -Force referenceassemblies.zip -DestinationPath \"${Env:ProgramFiles(x86)}\Reference Assemblies\Microsoft\Framework\.NETFramework\"; `
  33. Remove-Item -Force referenceassemblies.zip; `
  34. }

I tried to download AWS CLI using the command.

  1. If my understanding right, please help me to update the Dockerfile to install AWS CLI.
  2. So same way shall I need to install docker for windows also in the build server?

答案1

得分: 2

我已经完成了这个任务。请找到更新后的Docker文件:

  1. # escape=`
  2. FROM microsoft/dotnet-framework:4.7.2-runtime
  3. SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
  4. # 安装 NuGet CLI
  5. ENV NUGET_VERSION 4.4.1
  6. RUN New-Item -Type Directory $Env:ProgramFiles\NuGet; `
  7. Invoke-WebRequest -UseBasicParsing https://dist.nuget.org/win-x86-commandline/v$Env:NUGET_VERSION/nuget.exe -OutFile $Env:ProgramFiles\NuGet\nuget.exe
  8. # 安装 AWS CLI
  9. RUN Invoke-WebRequest -UseBasicParsing https://s3.amazonaws.com/aws-cli/AWSCLI64PY3.msi -OutFile AWSCLI64PY3.msi; `
  10. Start-Process "msiexec.exe" -ArgumentList '/i', 'AWSCLI64PY3.msi', '/qn', '/norestart' -Wait -NoNewWindow; `
  11. Remove-Item -Force AWSCLI64PY3.msi; `
  12. # 安装 VS 测试代理
  13. Invoke-WebRequest -UseBasicParsing https://download.visualstudio.microsoft.com/download/pr/12210068/8a386d27295953ee79281fd1f1832e2d/vs_TestAgent.exe -OutFile vs_TestAgent.exe; `
  14. Start-Process vs_TestAgent.exe -ArgumentList '--quiet', '--norestart', '--nocache' -NoNewWindow -Wait; `
  15. Remove-Item -Force vs_TestAgent.exe; `
  16. # 安装 VS 构建工具
  17. Invoke-WebRequest -UseBasicParsing https://download.visualstudio.microsoft.com/download/pr/12210059/e64d79b40219aea618ce2fe10ebd5f0d/vs_BuildTools.exe -OutFile vs_BuildTools.exe; `
  18. # 如果使用 ENV,安装程序将无法检测到 DOTNET_SKIP_FIRST_TIME_EXPERIENCE,必须使用 setx /M
  19. setx /M DOTNET_SKIP_FIRST_TIME_EXPERIENCE 1; `
  20. Start-Process vs_BuildTools.exe -ArgumentList '--add', 'Microsoft.VisualStudio.Workload.MSBuildTools', '--add', 'Microsoft.VisualStudio.Workload.NetCoreBuildTools', '--add', 'Microsoft.VisualStudio.Workload.WebBuildTools;includeRecommended', '--quiet', '--norestart', '--nocache' -NoNewWindow -Wait; `
  21. Remove-Item -Force vs_buildtools.exe; `
  22. Remove-Item -Force -Recurse "${Env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\"; `
  23. Remove-Item -Force -Recurse ${Env:TEMP}\*; `
  24. Remove-Item -Force -Recurse "${Env:ProgramData}\Package Cache\"
  25. # 在一个层中设置 PATH 以保持镜像大小不变。
  26. RUN setx /M PATH $(${Env:PATH} `
  27. + ";${Env:ProgramFiles}\NuGet\" `
  28. + ";${Env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\TestAgent\Common7\IDE\CommonExtensions\Microsoft\TestWindow\" `
  29. + ";${Env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\")
  30. # 安装目标包
  31. RUN @('4.0', '4.5.2', '4.6.2', '4.7.2') `
  32. | %{ `
  33. Invoke-WebRequest -UseBasicParsing https://dotnetbinaries.blob.core.windows.net/referenceassemblies/v${_}.zip -OutFile referenceassemblies.zip; `
  34. Expand-Archive -Force referenceassemblies.zip -DestinationPath "${Env:ProgramFiles(x86)}\Reference Assemblies\Microsoft\Framework\.NETFramework\"; `
  35. Remove-Item -Force referenceassemblies.zip; `
  36. }

注意:这是更新后的Docker文件,其中包含了一些命令和注释的修改。

英文:

I have done this. Please find the updated docker file

  1. # escape=`
  2. FROM microsoft/dotnet-framework:4.7.2-runtime
  3. SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
  4. #Install NuGet CLI
  5. ENV NUGET_VERSION 4.4.1
  6. RUN New-Item -Type Directory $Env:ProgramFiles\NuGet; `
  7. Invoke-WebRequest -UseBasicParsing https://dist.nuget.org/win-x86-commandline/v$Env:NUGET_VERSION/nuget.exe -OutFile $Env:ProgramFiles\NuGet\nuget.exe
  8. #Install AWS CLI
  9. RUN Invoke-WebRequest -UseBasicParsing https://s3.amazonaws.com/aws-cli/AWSCLI64PY3.msi -OutFile AWSCLI64PY3.msi; `
  10. Start-Process "msiexec.exe" -ArgumentList '/i', 'AWSCLI64PY3.msi', '/qn', '/norestart' -Wait -NoNewWindow; `
  11. Remove-Item -Force AWSCLI64PY3.msi; `
  12. # Install VS Test Agent
  13. Invoke-WebRequest -UseBasicParsing https://download.visualstudio.microsoft.com/download/pr/12210068/8a386d27295953ee79281fd1f1832e2d/vs_TestAgent.exe -OutFile vs_TestAgent.exe; `
  14. Start-Process vs_TestAgent.exe -ArgumentList '--quiet', '--norestart', '--nocache' -NoNewWindow -Wait; `
  15. Remove-Item -Force vs_TestAgent.exe; `
  16. # Install VS Build Tools
  17. Invoke-WebRequest -UseBasicParsing https://download.visualstudio.microsoft.com/download/pr/12210059/e64d79b40219aea618ce2fe10ebd5f0d/vs_BuildTools.exe -OutFile vs_BuildTools.exe; `
  18. # Installer won't detect DOTNET_SKIP_FIRST_TIME_EXPERIENCE if ENV is used, must use setx /M
  19. setx /M DOTNET_SKIP_FIRST_TIME_EXPERIENCE 1; `
  20. Start-Process vs_BuildTools.exe -ArgumentList '--add', 'Microsoft.VisualStudio.Workload.MSBuildTools', '--add', 'Microsoft.VisualStudio.Workload.NetCoreBuildTools', '--add', 'Microsoft.VisualStudio.Workload.WebBuildTools;includeRecommended', '--quiet', '--norestart', '--nocache' -NoNewWindow -Wait; `
  21. Remove-Item -Force vs_buildtools.exe; `
  22. Remove-Item -Force -Recurse \"${Env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\"; `
  23. Remove-Item -Force -Recurse ${Env:TEMP}\*; `
  24. Remove-Item -Force -Recurse \"${Env:ProgramData}\Package Cache\"
  25. # Set PATH in one layer to keep image size down.
  26. RUN setx /M PATH $(${Env:PATH} `
  27. + \";${Env:ProgramFiles}\NuGet\" `
  28. + \";${Env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\TestAgent\Common7\IDE\CommonExtensions\Microsoft\TestWindow\" `
  29. + \";${Env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\")
  30. # Install Targeting Packs
  31. RUN @('4.0', '4.5.2', '4.6.2', '4.7.2') `
  32. | %{ `
  33. Invoke-WebRequest -UseBasicParsing https://dotnetbinaries.blob.core.windows.net/referenceassemblies/v${_}.zip -OutFile referenceassemblies.zip; `
  34. Expand-Archive -Force referenceassemblies.zip -DestinationPath \"${Env:ProgramFiles(x86)}\Reference Assemblies\Microsoft\Framework\.NETFramework\"; `
  35. Remove-Item -Force referenceassemblies.zip; `
  36. }

huangapple
  • 本文由 发表于 2020年1月3日 14:03:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59573885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定