| 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/docker/docker.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 |
ARG WINDOWS_BASE_IMAGE=mcr.microsoft.com/windows/servercore |
| 158 |
ARG WINDOWS_BASE_IMAGE_TAG=ltsc2022 |
| 159 |
FROM ${WINDOWS_BASE_IMAGE}:${WINDOWS_BASE_IMAGE_TAG} |
| 160 |
|
| 161 |
# Use PowerShell as the default shell |
| 162 |
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"] |
| 163 |
|
| 164 |
ARG GO_VERSION=1.24.9 |
| 165 |
|
| 166 |
# GOTESTSUM_VERSION is the version of gotest.tools/gotestsum to install. |
| 167 |
ARG GOTESTSUM_VERSION=v1.12.3 |
| 168 |
|
| 169 |
# GOWINRES_VERSION is the version of go-winres to install. |
| 170 |
ARG GOWINRES_VERSION=v0.3.3 |
| 171 |
ARG CONTAINERD_VERSION=v1.7.28 |
| 172 |
|
| 173 |
# Environment variable notes: |
| 174 |
# - GO_VERSION must be consistent with 'Dockerfile' used by Linux. |
| 175 |
# - CONTAINERD_VERSION must be consistent with 'hack/dockerfile/install/containerd.installer' used by Linux. |
| 176 |
# - FROM_DOCKERFILE is used for detection of building within a container. |
| 177 |
ENV GO_VERSION=${GO_VERSION} ` |
| 178 |
CONTAINERD_VERSION=${CONTAINERD_VERSION} ` |
| 179 |
GIT_VERSION=2.11.1 ` |
| 180 |
GOPATH=C:\gopath ` |
| 181 |
GO111MODULE=off ` |
| 182 |
GOTOOLCHAIN=local ` |
| 183 |
FROM_DOCKERFILE=1 ` |
| 184 |
GOTESTSUM_VERSION=${GOTESTSUM_VERSION} ` |
| 185 |
GOWINRES_VERSION=${GOWINRES_VERSION} |
| 186 |
|
| 187 |
RUN ` |
| 188 |
Function Test-Nano() { ` |
| 189 |
$EditionId = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name 'EditionID').EditionId; ` |
| 190 |
return (($EditionId -eq 'ServerStandardNano') -or ($EditionId -eq 'ServerDataCenterNano') -or ($EditionId -eq 'NanoServer')); ` |
| 191 |
}` |
| 192 |
` |
| 193 |
Function Download-File([string] $source, [string] $target) { ` |
| 194 |
if (Test-Nano) { ` |
| 195 |
$handler = New-Object System.Net.Http.HttpClientHandler; ` |
| 196 |
$client = New-Object System.Net.Http.HttpClient($handler); ` |
| 197 |
$client.Timeout = New-Object System.TimeSpan(0, 30, 0); ` |
| 198 |
$cancelTokenSource = [System.Threading.CancellationTokenSource]::new(); ` |
| 199 |
$responseMsg = $client.GetAsync([System.Uri]::new($source), $cancelTokenSource.Token); ` |
| 200 |
$responseMsg.Wait(); ` |
| 201 |
if (!$responseMsg.IsCanceled) { ` |
| 202 |
$response = $responseMsg.Result; ` |
| 203 |
if ($response.IsSuccessStatusCode) { ` |
| 204 |
$downloadedFileStream = [System.IO.FileStream]::new($target, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write); ` |
| 205 |
$copyStreamOp = $response.Content.CopyToAsync($downloadedFileStream); ` |
| 206 |
$copyStreamOp.Wait(); ` |
| 207 |
$downloadedFileStream.Close(); ` |
| 208 |
if ($copyStreamOp.Exception -ne $null) { throw $copyStreamOp.Exception } ` |
| 209 |
} ` |
| 210 |
} else { ` |
| 211 |
Throw ("Failed to download " + $source) ` |
| 212 |
}` |
| 213 |
} else { ` |
| 214 |
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; ` |
| 215 |
$webClient = New-Object System.Net.WebClient; ` |
| 216 |
$webClient.DownloadFile($source, $target); ` |
| 217 |
} ` |
| 218 |
} ` |
| 219 |
` |
| 220 |
setx /M PATH $('C:\git\cmd;C:\git\usr\bin;'+$Env:PATH+';C:\gcc\bin;C:\go\bin;C:\containerd\bin'); ` |
| 221 |
` |
| 222 |
Write-Host INFO: Downloading git...; ` |
| 223 |
$location='https://www.nuget.org/api/v2/package/GitForWindows/'+$Env:GIT_VERSION; ` |
| 224 |
Download-File $location C:\gitsetup.zip; ` |
| 225 |
` |
| 226 |
Write-Host INFO: Downloading go...; ` |
| 227 |
$dlGoVersion=$Env:GO_VERSION; ` |
| 228 |
Download-File "https://go.dev/dl/go${dlGoVersion}.windows-amd64.zip" C:\go.zip; ` |
| 229 |
` |
| 230 |
Write-Host INFO: Downloading compiler 1 of 3...; ` |
| 231 |
Download-File https://raw.githubusercontent.com/moby/docker-tdmgcc/master/gcc.zip C:\gcc.zip; ` |
| 232 |
` |
| 233 |
Write-Host INFO: Downloading compiler 2 of 3...; ` |
| 234 |
Download-File https://raw.githubusercontent.com/moby/docker-tdmgcc/master/runtime.zip C:\runtime.zip; ` |
| 235 |
` |
| 236 |
Write-Host INFO: Downloading compiler 3 of 3...; ` |
| 237 |
Download-File https://raw.githubusercontent.com/moby/docker-tdmgcc/master/binutils.zip C:\binutils.zip; ` |
| 238 |
` |
| 239 |
Write-Host INFO: Extracting git...; ` |
| 240 |
Expand-Archive C:\gitsetup.zip C:\git-tmp; ` |
| 241 |
New-Item -Type Directory C:\git | Out-Null; ` |
| 242 |
Move-Item C:\git-tmp\tools\* C:\git\.; ` |
| 243 |
Remove-Item -Recurse -Force C:\git-tmp; ` |
| 244 |
` |
| 245 |
Write-Host INFO: Expanding go...; ` |
| 246 |
Expand-Archive C:\go.zip -DestinationPath C:\; ` |
| 247 |
` |
| 248 |
Write-Host INFO: Expanding compiler 1 of 3...; ` |
| 249 |
Expand-Archive C:\gcc.zip -DestinationPath C:\gcc -Force; ` |
| 250 |
Write-Host INFO: Expanding compiler 2 of 3...; ` |
| 251 |
Expand-Archive C:\runtime.zip -DestinationPath C:\gcc -Force; ` |
| 252 |
Write-Host INFO: Expanding compiler 3 of 3...; ` |
| 253 |
Expand-Archive C:\binutils.zip -DestinationPath C:\gcc -Force; ` |
| 254 |
` |
| 255 |
Write-Host INFO: Removing downloaded files...; ` |
| 256 |
Remove-Item C:\gcc.zip; ` |
| 257 |
Remove-Item C:\runtime.zip; ` |
| 258 |
Remove-Item C:\binutils.zip; ` |
| 259 |
Remove-Item C:\gitsetup.zip; ` |
| 260 |
` |
| 261 |
Write-Host INFO: Downloading containerd; ` |
| 262 |
$location='https://github.com/containerd/containerd/releases/download/'+$Env:CONTAINERD_VERSION+'/containerd-'+$Env:CONTAINERD_VERSION.TrimStart('v')+'-windows-amd64.tar.gz'; ` |
| 263 |
Download-File $location C:\containerd.tar.gz; ` |
| 264 |
New-Item -Path C:\containerd -ItemType Directory; ` |
| 265 |
tar -xzf C:\containerd.tar.gz -C C:\containerd; ` |
| 266 |
Remove-Item C:\containerd.tar.gz; ` |
| 267 |
` |
| 268 |
# Ensure all directories exist that we will require below.... |
| 269 |
$srcDir = """$Env:GOPATH`\src\github.com\docker\docker\bundles"""; ` |
| 270 |
Write-Host INFO: Ensuring existence of directory $srcDir...; ` |
| 271 |
New-Item -Force -ItemType Directory -Path $srcDir | Out-Null; ` |
| 272 |
` |
| 273 |
Write-Host INFO: Configuring git core.autocrlf...; ` |
| 274 |
C:\git\cmd\git config --global core.autocrlf true; |
| 275 |
|
| 276 |
RUN ` |
| 277 |
Function Install-GoTestSum() { ` |
| 278 |
$Env:GO111MODULE = 'on'; ` |
| 279 |
$tmpGobin = "${Env:GOBIN_TMP}"; ` |
| 280 |
$Env:GOBIN = """${Env:GOPATH}`\bin"""; ` |
| 281 |
Write-Host "INFO: Installing gotestsum version $Env:GOTESTSUM_VERSION in $Env:GOBIN"; ` |
| 282 |
&go install "gotest.tools/gotestsum@${Env:GOTESTSUM_VERSION}"; ` |
| 283 |
$Env:GOBIN = "${tmpGobin}"; ` |
| 284 |
$Env:GO111MODULE = 'off'; ` |
| 285 |
if ($LASTEXITCODE -ne 0) { ` |
| 286 |
Throw '"gotestsum install failed..."'; ` |
| 287 |
} ` |
| 288 |
} ` |
| 289 |
` |
| 290 |
Install-GoTestSum |
| 291 |
|
| 292 |
RUN ` |
| 293 |
Function Install-GoWinres() { ` |
| 294 |
$Env:GO111MODULE = 'on'; ` |
| 295 |
$tmpGobin = "${Env:GOBIN_TMP}"; ` |
| 296 |
$Env:GOBIN = """${Env:GOPATH}`\bin"""; ` |
| 297 |
Write-Host "INFO: Installing go-winres version $Env:GOWINRES_VERSION in $Env:GOBIN"; ` |
| 298 |
&go install "github.com/tc-hib/go-winres@${Env:GOWINRES_VERSION}"; ` |
| 299 |
$Env:GOBIN = "${tmpGobin}"; ` |
| 300 |
$Env:GO111MODULE = 'off'; ` |
| 301 |
if ($LASTEXITCODE -ne 0) { ` |
| 302 |
Throw '"go-winres install failed..."'; ` |
| 303 |
} ` |
| 304 |
} ` |
| 305 |
` |
| 306 |
Install-GoWinres |
| 307 |
|
| 308 |
# Make PowerShell the default entrypoint |
| 309 |
ENTRYPOINT ["powershell.exe"] |
| 310 |
|
| 311 |
# Set the working directory to the location of the sources |
| 312 |
WORKDIR ${GOPATH}\src\github.com\docker\docker |
| 313 |
|
| 314 |
# Copy the sources into the container |
| 315 |
COPY . . |