github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/Dockerfile.windows (about)

     1  # escape=`
     2  
     3  # -----------------------------------------------------------------------------------------
     4  # This file describes the standard way to build Docker in a container on Windows
     5  # Server 2016 or Windows 10.
     6  #
     7  # Maintainer: @jhowardmsft
     8  # -----------------------------------------------------------------------------------------
     9  
    10  
    11  # Prerequisites:
    12  # --------------
    13  #
    14  # 1. Windows Server 2016 or Windows 10 with all Windows updates applied. The major 
    15  #    build number must be at least 14393. This can be confirmed, for example, by 
    16  #    running the following from an elevated PowerShell prompt - this sample output 
    17  #    is from a fully up to date machine as at mid-November 2016:
    18  #
    19  #    >> PS C:\> $(gin).WindowsBuildLabEx
    20  #    >> 14393.447.amd64fre.rs1_release_inmarket.161102-0100
    21  #
    22  # 2. Git for Windows (or another git client) must be installed. https://git-scm.com/download/win.
    23  #
    24  # 3. The machine must be configured to run containers. For example, by following
    25  #    the quick start guidance at https://msdn.microsoft.com/en-us/virtualization/windowscontainers/quick_start/quick_start or
    26  #    https://github.com/docker/labs/blob/master/windows/windows-containers/Setup.md
    27  #
    28  # 4. If building in a Hyper-V VM: For Windows Server 2016 using Windows Server
    29  #    containers as the default option, it is recommended you have at least 1GB 
    30  #    of memory assigned; For Windows 10 where Hyper-V Containers are employed, you
    31  #    should have at least 4GB of memory assigned. Note also, to run Hyper-V 
    32  #    containers in a VM, it is necessary to configure the VM for nested virtualization.
    33  
    34  # -----------------------------------------------------------------------------------------
    35  
    36  
    37  # Usage:
    38  # -----
    39  #
    40  #  The following steps should be run from an (elevated*) Windows PowerShell prompt. 
    41  #
    42  #  (*In a default installation of containers on Windows following the quick-start guidance at
    43  #    https://msdn.microsoft.com/en-us/virtualization/windowscontainers/quick_start/quick_start,
    44  #    the docker.exe client must run elevated to be able to connect to the daemon).
    45  #
    46  # 1. Clone the sources from github.com:
    47  #
    48  #    >>   git clone https://github.com/demonoid81/moby.git C:\gopath\src\github.com\docker\docker
    49  #    >>   Cloning into 'C:\gopath\src\github.com\docker\docker'...
    50  #    >>   remote: Counting objects: 186216, done.
    51  #    >>   remote: Compressing objects: 100% (21/21), done.
    52  #    >>   remote: Total 186216 (delta 5), reused 0 (delta 0), pack-reused 186195
    53  #    >>   Receiving objects: 100% (186216/186216), 104.32 MiB | 8.18 MiB/s, done.
    54  #    >>   Resolving deltas: 100% (123139/123139), done.
    55  #    >>   Checking connectivity... done.
    56  #    >>   Checking out files: 100% (3912/3912), done.
    57  #    >>   PS C:\>
    58  #
    59  #
    60  # 2. Change directory to the cloned docker sources:
    61  #
    62  #    >>   cd C:\gopath\src\github.com\docker\docker 
    63  #
    64  #
    65  # 3. Build a docker image with the components required to build the docker binaries from source
    66  #    by running one of the following:
    67  #
    68  #    >>   docker build -t nativebuildimage -f Dockerfile.windows .          
    69  #    >>   docker build -t nativebuildimage -f Dockerfile.windows -m 2GB .    (if using Hyper-V containers)
    70  #
    71  #
    72  # 4. Build the docker executable binaries by running one of the following:
    73  #
    74  #    >>   $DOCKER_GITCOMMIT=(git rev-parse --short HEAD)
    75  #    >>   docker run --name binaries -e DOCKER_GITCOMMIT=$DOCKER_GITCOMMIT nativebuildimage hack\make.ps1 -Binary
    76  #    >>   docker run --name binaries -e DOCKER_GITCOMMIT=$DOCKER_GITCOMMIT -m 2GB nativebuildimage hack\make.ps1 -Binary    (if using Hyper-V containers)
    77  #
    78  #
    79  # 5. Copy the binaries out of the container, replacing HostPath with an appropriate destination 
    80  #    folder on the host system where you want the binaries to be located.
    81  #
    82  #    >>   docker cp binaries:C:\gopath\src\github.com\docker\docker\bundles\docker.exe C:\HostPath\docker.exe
    83  #    >>   docker cp binaries:C:\gopath\src\github.com\docker\docker\bundles\dockerd.exe C:\HostPath\dockerd.exe
    84  #
    85  #
    86  # 6. (Optional) Remove the interim container holding the built executable binaries:
    87  #
    88  #    >>    docker rm binaries
    89  #
    90  #
    91  # 7. (Optional) Remove the image used for the container in which the executable
    92  #    binaries are build. Tip - it may be useful to keep this image around if you need to
    93  #    build multiple times. Then you can take advantage of the builder cache to have an
    94  #    image which has all the components required to build the binaries already installed.
    95  #
    96  #    >>    docker rmi nativebuildimage
    97  #
    98  
    99  # -----------------------------------------------------------------------------------------
   100  
   101  
   102  #  The validation tests can only run directly on the host. This is because they calculate
   103  #  information from the git repo, but the .git directory is not passed into the image as
   104  #  it is excluded via .dockerignore. Run the following from a Windows PowerShell prompt
   105  #  (elevation is not required): (Note Go must be installed to run these tests)
   106  #
   107  #    >>   hack\make.ps1 -DCO -PkgImports -GoFormat
   108  
   109  
   110  # -----------------------------------------------------------------------------------------
   111  
   112  
   113  #  To run unit tests, ensure you have created the nativebuildimage above. Then run one of
   114  #  the following from an (elevated) Windows PowerShell prompt:
   115  #
   116  #    >>   docker run --rm nativebuildimage hack\make.ps1 -TestUnit
   117  #    >>   docker run --rm -m 2GB nativebuildimage hack\make.ps1 -TestUnit    (if using Hyper-V containers)
   118  
   119  
   120  # -----------------------------------------------------------------------------------------
   121  
   122  
   123  #  To run unit tests and binary build, ensure you have created the nativebuildimage above. Then 
   124  #  run one of the following from an (elevated) Windows PowerShell prompt:
   125  #
   126  #    >>   docker run nativebuildimage hack\make.ps1 -All
   127  #    >>   docker run -m 2GB nativebuildimage hack\make.ps1 -All    (if using Hyper-V containers)
   128  
   129  # -----------------------------------------------------------------------------------------
   130  
   131  
   132  # Important notes:
   133  # ---------------
   134  #
   135  # Don't attempt to use a bind mount to pass a local directory as the bundles target
   136  # directory. It does not work (golang attempts for follow a mapped folder incorrectly).
   137  # Instead, use docker cp as per the example.
   138  #
   139  # go.zip is not removed from the image as it is used by the Windows CI servers
   140  # to ensure the host and image are running consistent versions of go.
   141  #
   142  # Nanoserver support is a work in progress. Although the image will build if the 
   143  # FROM statement is updated, it will not work when running autogen through hack\make.ps1. 
   144  # It is suspected that the required GCC utilities (eg gcc, windres, windmc) silently
   145  # quit due to the use of console hooks which are not available.
   146  #
   147  # The docker integration tests do not currently run in a container on Windows, predominantly
   148  # due to Windows not supporting privileged mode, so anything using a volume would fail.
   149  # They (along with the rest of the docker CI suite) can be run using 
   150  # https://github.com/kevpar/docker-w2wCIScripts/blob/master/runCI/Invoke-DockerCI.ps1.
   151  #
   152  # -----------------------------------------------------------------------------------------
   153  
   154  
   155  # The number of build steps below are explicitly minimised to improve performance.
   156  
   157  # Extremely important - do not change the following line to reference a "specific" image, 
   158  # such as `mcr.microsoft.com/windows/servercore:ltsc2019`. If using this Dockerfile in process
   159  # isolated containers, the kernel of the host must match the container image, and hence
   160  # would fail between Windows Server 2016 (aka RS1) and Windows Server 2019 (aka RS5).
   161  # It is expected that the image `microsoft/windowsservercore:latest` is present, and matches
   162  # the hosts kernel version before doing a build. 
   163  FROM microsoft/windowsservercore
   164  
   165  # Use PowerShell as the default shell
   166  SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
   167  
   168  ARG GO_VERSION=1.13.10
   169  ARG GOTESTSUM_COMMIT=v0.3.5
   170  
   171  # Environment variable notes:
   172  #  - GO_VERSION must be consistent with 'Dockerfile' used by Linux.
   173  #  - FROM_DOCKERFILE is used for detection of building within a container.
   174  ENV GO_VERSION=${GO_VERSION} `
   175      GIT_VERSION=2.11.1 `
   176      GOPATH=C:\gopath `
   177      GO111MODULE=off `
   178      FROM_DOCKERFILE=1 `
   179      GOTESTSUM_COMMIT=${GOTESTSUM_COMMIT}
   180  
   181  RUN `
   182    Function Test-Nano() { `
   183      $EditionId = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name 'EditionID').EditionId; `
   184      return (($EditionId -eq 'ServerStandardNano') -or ($EditionId -eq 'ServerDataCenterNano') -or ($EditionId -eq 'NanoServer')); `
   185    }`
   186    `
   187    Function Download-File([string] $source, [string] $target) { `
   188      if (Test-Nano) { `
   189        $handler = New-Object System.Net.Http.HttpClientHandler; `
   190        $client = New-Object System.Net.Http.HttpClient($handler); `
   191        $client.Timeout = New-Object System.TimeSpan(0, 30, 0); `
   192        $cancelTokenSource = [System.Threading.CancellationTokenSource]::new(); `
   193        $responseMsg = $client.GetAsync([System.Uri]::new($source), $cancelTokenSource.Token); `
   194        $responseMsg.Wait(); `
   195        if (!$responseMsg.IsCanceled) { `
   196          $response = $responseMsg.Result; `
   197          if ($response.IsSuccessStatusCode) { `
   198            $downloadedFileStream = [System.IO.FileStream]::new($target, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write); `
   199            $copyStreamOp = $response.Content.CopyToAsync($downloadedFileStream); `
   200            $copyStreamOp.Wait(); `
   201            $downloadedFileStream.Close(); `
   202            if ($copyStreamOp.Exception -ne $null) { throw $copyStreamOp.Exception } `
   203          } `
   204        } else { `
   205        Throw ("Failed to download " + $source) `
   206        }`
   207      } else { `
   208        $webClient = New-Object System.Net.WebClient; `
   209        $webClient.DownloadFile($source, $target); `
   210      } `
   211    } `
   212    `
   213    setx /M PATH $('C:\git\cmd;C:\git\usr\bin;'+$Env:PATH+';C:\gcc\bin;C:\go\bin'); `
   214    `
   215    Write-Host INFO: Downloading git...; `
   216    $location='https://www.nuget.org/api/v2/package/GitForWindows/'+$Env:GIT_VERSION; `
   217    Download-File $location C:\gitsetup.zip; `
   218    `
   219    Write-Host INFO: Downloading go...; `
   220    $dlGoVersion=$Env:GO_VERSION -replace '\.0$',''; `
   221    Download-File "https://golang.org/dl/go${dlGoVersion}.windows-amd64.zip" C:\go.zip; `
   222    `
   223    Write-Host INFO: Downloading compiler 1 of 3...; `
   224    Download-File https://raw.githubusercontent.com/moby/docker-tdmgcc/master/gcc.zip C:\gcc.zip; `
   225    `
   226    Write-Host INFO: Downloading compiler 2 of 3...; `
   227    Download-File https://raw.githubusercontent.com/moby/docker-tdmgcc/master/runtime.zip C:\runtime.zip; `
   228    `
   229    Write-Host INFO: Downloading compiler 3 of 3...; `
   230    Download-File https://raw.githubusercontent.com/moby/docker-tdmgcc/master/binutils.zip C:\binutils.zip; `
   231    `
   232    Write-Host INFO: Extracting git...; `
   233    Expand-Archive C:\gitsetup.zip C:\git-tmp; `
   234    New-Item -Type Directory C:\git | Out-Null; `
   235    Move-Item C:\git-tmp\tools\* C:\git\.; `
   236    Remove-Item -Recurse -Force C:\git-tmp; `
   237    `
   238    Write-Host INFO: Expanding go...; `
   239    Expand-Archive C:\go.zip -DestinationPath C:\; `
   240    `
   241    Write-Host INFO: Expanding compiler 1 of 3...; `
   242    Expand-Archive C:\gcc.zip -DestinationPath C:\gcc -Force; `
   243    Write-Host INFO: Expanding compiler 2 of 3...; `
   244    Expand-Archive C:\runtime.zip -DestinationPath C:\gcc -Force; `
   245    Write-Host INFO: Expanding compiler 3 of 3...; `
   246    Expand-Archive C:\binutils.zip -DestinationPath C:\gcc -Force; `
   247    `
   248    Write-Host INFO: Removing downloaded files...; `
   249    Remove-Item C:\gcc.zip; `
   250    Remove-Item C:\runtime.zip; `
   251    Remove-Item C:\binutils.zip; `
   252    Remove-Item C:\gitsetup.zip; `
   253    `
   254    # Ensure all directories exist that we will require below....
   255    $srcDir = """$Env:GOPATH`\src\github.com\docker\docker\bundles"""; `
   256    Write-Host INFO: Ensuring existence of directory $srcDir...; `
   257    New-Item -Force -ItemType Directory -Path $srcDir | Out-Null; `
   258    `
   259    Write-Host INFO: Configuring git core.autocrlf...; `
   260    C:\git\cmd\git config --global core.autocrlf true;
   261  
   262  RUN `
   263    Function Build-GoTestSum() { `
   264      Write-Host "INFO: Building gotestsum version $Env:GOTESTSUM_COMMIT in $Env:GOPATH"; `
   265      $env:GO111MODULE = 'on'; `
   266      &go get -d "gotest.tools/gotestsum@${Env:GOTESTSUM_COMMIT}"; `
   267      $env:GO111MODULE = 'off'; `
   268      if ($LASTEXITCODE -ne 0) {  `
   269        Throw '"Failed getting gotestsum sources..."'  `
   270      }; `
   271      $env:GO111MODULE = 'on'; `
   272      &go build -buildmode=exe -o "${Env:GOPATH}\bin\gotestsum.exe" gotest.tools/gotestsum; `
   273      $env:GO111MODULE = 'off'; `
   274      if ($LASTEXITCODE -ne 0) {  `
   275        Throw '"gotestsum build failed..."'; `
   276      } `
   277      Write-Host "INFO:     Build done for gotestsum..."; `
   278    } `
   279    `
   280    Build-GoTestSum
   281  
   282  # Make PowerShell the default entrypoint
   283  ENTRYPOINT ["powershell.exe"]
   284  
   285  # Set the working directory to the location of the sources
   286  WORKDIR ${GOPATH}\src\github.com\docker\docker
   287  
   288  # Copy the sources into the container
   289  COPY . .