简体   繁体   中英

Can’t install Choclatey into windows docker container

Im trying to install Chocolatey into a docker windows container, on a Windows 10 Machine using a Windows Container and not linux containers. Im running the docker build command in the PowerShell console and every time it get to trying to install Chocolatey using the line: Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

It comes back with:

Exception calling "DownloadString" with "1" argument(s): "The remote name could not be resolved: 'chocolatey.org'"
 At line:1 char:166
 + ...  -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('ht ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException

It cant seem to resolve the chocolatey.org part and I'm specifying the.network card in my build command:

docker build --network 21fb9a254e4b --progress=plain --tag jcontent/dockerwinbaseimage .

I have also specified the DNS settings in the Daemon file to look at 8.8.8.8 this is in testing it was not working before this....

I have attached my Build Script. Any Help would be appreciated.

# escape=`
FROM microsoft/dotnet-framework:4.7.2-sdk-windowsservercore-ltsc2016

FROM microsoft/windowsservercore:10.0.14393.1358

RUN @powershell [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12

ENV chocolateyUseWindowsCompression=false
RUN @powershell Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

RUN choco config set cachelocation C:\chococache

RUN choco feature enable --name allowGlobalConfirmation 

RUN choco install git; 
RUN choco install nodejs;
RUN choco install curl;
RUN choco install docker;
RUN choco install terraform;
 
# choco install visualstudio2015community --confirm --timeout 216000 \

RUN choco install dotnet4.6.1 --confirm --limit-output; 

RUN choco install visualstudio2017enterprise --package-parameters "--passive --locale en-US --includeOptional" --confirm --limit-output --timeout 216000;

RUN choco install visualstudio2017-workload-azure --confirm --limit-output --timeout 21600 --package-parameters "--includeOptional"; 
RUN choco install visualstudio2017-workload-netcoretools --confirm --limit-output --timeout 21600 --package-parameters "--includeOptional"; 
RUN choco install visualstudio2017-workload-netweb --confirm --limit-output --timeout 21600 --package-parameters "--includeOptional"; 
     

# Destroy Choclotatey cache
RUN rmdir /S /Q C:\chococache

# common node tools
RUN npm install gulp -g && npm install grunt -g && npm install -g less && npm install phantomjs-prebuilt -g;

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

# Install .NET Core
ENV DOTNET_VERSION 3.1.7
ENV DOTNET_DOWNLOAD_URL https://dotnetcli.blob.core.windows.net/dotnet/release/1.1.0/Binaries/$DOTNET_VERSION/dotnet-win-x64.$DOTNET_VERSION.zip

RUN Invoke-WebRequest $Env:DOTNET_DOWNLOAD_URL -OutFile dotnet.zip; \
RUN Expand-Archive dotnet.zip -DestinationPath $Env:ProgramFiles\dotnet -Force; \
RUN Remove-Item -Force dotnet.zip

# Install .NET Core
ENV DOTNET_VERSION 3.1.7
ENV DOTNET_DOWNLOAD_URL https://dotnetcli.blob.core.windows.net/dotnet/preview/Binaries/$DOTNET_VERSION/dotnet-win-x64.$DOTNET_VERSION.zip

RUN Invoke-WebRequest $Env:DOTNET_DOWNLOAD_URL -OutFile dotnet.zip; \
RUN Expand-Archive dotnet.zip -DestinationPath $Env:ProgramFiles\dotnet -Force; \
RUN Remove-Item -Force dotnet.zip
    
# Install .NET Core SDK
ENV DOTNET_VERSION 3.1.7
ENV DOTNET_SDK_DOWNLOAD_URL https://dotnetcli.blob.core.windows.net/dotnet/Sdk/$DOTNET_SDK_VERSION/dotnet-dev-win-x64.$DOTNET_SDK_VERSION.zip

RUN Invoke-WebRequest $Env:DOTNET_SDK_DOWNLOAD_URL -OutFile dotnet.zip; \
RUN Expand-Archive dotnet.zip -DestinationPath $Env:ProgramFiles\dotnet -Force; \
RUN Remove-Item -Force dotnet.zip

SHELL ["cmd", "/S", "/C"]

RUN setx /M PATH "%PATH%;%ProgramFiles%\dotnet"

# Trigger the population of the local package cache
ENV NUGET_XMLDOC_MODE skip

RUN mkdir C:\warmup \
RUN cd C:\warmup \
RUN dotnet new \
RUN cd .. \
RUN rmdir /S /Q C:\warmup 

#Change Working Directory to Install DevOps Tools
WORKDIR /azp
#Install DevOps Agents
COPY start.ps1 .
#Configure DevOps Agent
CMD powershell .\start.ps1

I managed to fix this frustrating problem today, here is what I did for when the next person has this issue, as Docker is not going to fix it any time soon.

What I did, is in the desktop app on Windows / Mac you can edit the Daemon file. Under Settings in the Docker App under Docker Engine, I added the line at the bottom of the file just above the last curly brace. "dns": [ "Your DNS Address Here", "8.8.8.8" ]

This then allows the Docker Containers all that you now build to use your host's DNS server. Technically if you can access: https://chocolatey.org/install.ps1 then you should be able to access the choco repository.

I have also built the image in https://github.com/jasric89/vsts-agent-docker/tree/master/windows/servercore/10.0.14393 and labeled it in the repo:

microsoft/windowsservercore:10.0.14393.1358

I then set: RUN choco feature enable --name allowGlobalConfirmation before my first Choco Install command, this enables choco to install all the files and not error.

With all that set my Docker File Ran and built the image. Well in my Test Env now testing in my prod env. :)

Links that helped me:

https://github.com/moby/moby/issues/24928 https://github.com/jasric89/vsts-agent-docker/blob/master/windows/servercore/10.0.14393/standard/VS2017/Dockerfile https://docs.chocolatey.org/en-us/troubleshooting https://github.com/moby/moby/issues/25537

UPDATE:

I ran into this problem again on a fresh build and after following my own instructions it didn't quite work.

I realised after a couple more hours of testing that Docker on Windows uses the Hyper-V Network setup.

Within my Hyper V Switch Manager, I did not have a network, with Internet Access. Also when I tried to change the default switch it would not let me. Therefore I had to create a new network within the Hyper-V Network.

I then had to edit the docker Daemon file, in the Docker Settings to tell it to use the right network, and also I put in the DNS settings I specified before in my Anwser.

Here is my full docker daemon file:

 { "registry-mirrors": [], "insecure-registries": [], "bridge": "Internet", "dns": [ "YOUR Local DNS Address Here", "8.8.8.8" ], "debug": false, "experimental": false }

Thanks to @jason for a great answer which actually helped me a lot to resolve this. In the end, a combination of the Chocolatey install docs and a few other online resources, and I got this to work:

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

WORKDIR /app

RUN Set-ExecutionPolicy Bypass -Scope Process -Force; \
  [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; \
  iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'));

RUN choco install dotnetcore-runtime --version 2.2.7 -y;
RUN choco install aspnetcore-runtimepackagestore --version 2.2.7 -y;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM