9a19bd1b |
# escape=`
# -----------------------------------------------------------------------------------------
# This file describes the standard way to build Docker in a container on Windows |
155435b6 |
# Server 2016 or Windows 10. |
9a19bd1b |
#
# Maintainer: @jhowardmsft
# -----------------------------------------------------------------------------------------
# Prerequisites:
# --------------
# |
155435b6 |
# 1. Windows Server 2016 or Windows 10 with all Windows updates applied. The major
# build number must be at least 14393. This can be confirmed, for example, by
# running the following from an elevated PowerShell prompt - this sample output
# is from a fully up to date machine as at mid-November 2016: |
6c88b8ef |
# |
9a19bd1b |
# >> PS C:\> $(gin).WindowsBuildLabEx |
155435b6 |
# >> 14393.447.amd64fre.rs1_release_inmarket.161102-0100 |
9a19bd1b |
#
# 2. Git for Windows (or another git client) must be installed. https://git-scm.com/download/win.
#
# 3. The machine must be configured to run containers. For example, by following
# the quick start guidance at https://msdn.microsoft.com/en-us/virtualization/windowscontainers/quick_start/quick_start or
# https://github.com/docker/labs/blob/master/windows/windows-containers/Setup.md |
155435b6 |
#
# 4. If building in a Hyper-V VM: For Windows Server 2016 using Windows Server
# containers as the default option, it is recommended you have at least 1GB
# of memory assigned; For Windows 10 where Hyper-V Containers are employed, you
# should have at least 4GB of memory assigned. Note also, to run Hyper-V
# containers in a VM, it is necessary to configure the VM for nested virtualization. |
9a19bd1b |
# -----------------------------------------------------------------------------------------
|
6c88b8ef |
# Usage: |
9a19bd1b |
# -----
#
# The following steps should be run from an (elevated*) Windows PowerShell prompt.
#
# (*In a default installation of containers on Windows following the quick-start guidance at
# https://msdn.microsoft.com/en-us/virtualization/windowscontainers/quick_start/quick_start,
# the docker.exe client must run elevated to be able to connect to the daemon).
#
# 1. Clone the sources from github.com:
# |
5add6213 |
# >> git clone https://github.com/docker/docker.git C:\go\src\github.com\docker\docker
# >> Cloning into 'C:\go\src\github.com\docker\docker'... |
9a19bd1b |
# >> remote: Counting objects: 186216, done.
# >> remote: Compressing objects: 100% (21/21), done.
# >> remote: Total 186216 (delta 5), reused 0 (delta 0), pack-reused 186195
# >> Receiving objects: 100% (186216/186216), 104.32 MiB | 8.18 MiB/s, done.
# >> Resolving deltas: 100% (123139/123139), done.
# >> Checking connectivity... done.
# >> Checking out files: 100% (3912/3912), done.
# >> PS C:\>
#
#
# 2. Change directory to the cloned docker sources: |
6c88b8ef |
# |
5add6213 |
# >> cd C:\go\src\github.com\docker\docker |
eacd2fd4 |
# |
6c88b8ef |
# |
f7b4d654 |
# 3. Build a docker image with the components required to build the docker binaries from source
# by running one of the following: |
6c88b8ef |
# |
f7b4d654 |
# >> docker build -t nativebuildimage -f Dockerfile.windows .
# >> docker build -t nativebuildimage -f Dockerfile.windows -m 2GB . (if using Hyper-V containers) |
eacd2fd4 |
#
# |
f7b4d654 |
# 4. Build the docker executable binaries by running one of the following: |
9a19bd1b |
# |
f32b267d |
# >> $DOCKER_GITCOMMIT=(git rev-parse --short HEAD)
# >> docker run --name binaries -e DOCKER_GITCOMMIT=$DOCKER_GITCOMMIT nativebuildimage hack\make.ps1 -Binary
# >> docker run --name binaries -e DOCKER_GITCOMMIT=$DOCKER_GITCOMMIT -m 2GB nativebuildimage hack\make.ps1 -Binary (if using Hyper-V containers) |
9a19bd1b |
#
# |
155435b6 |
# 5. Copy the binaries out of the container, replacing HostPath with an appropriate destination |
9a19bd1b |
# folder on the host system where you want the binaries to be located.
# |
155435b6 |
# >> docker cp binaries:C:\go\src\github.com\docker\docker\bundles\docker.exe C:\HostPath\docker.exe
# >> docker cp binaries:C:\go\src\github.com\docker\docker\bundles\dockerd.exe C:\HostPath\dockerd.exe |
9a19bd1b |
#
#
# 6. (Optional) Remove the interim container holding the built executable binaries:
#
# >> docker rm binaries
#
#
# 7. (Optional) Remove the image used for the container in which the executable
# binaries are build. Tip - it may be useful to keep this image around if you need to
# build multiple times. Then you can take advantage of the builder cache to have an
# image which has all the components required to build the binaries already installed.
#
# >> docker rmi nativebuildimage |
155435b6 |
#
# -----------------------------------------------------------------------------------------
|
f32b267d |
# The validation tests can only run directly on the host. This is because they calculate
# information from the git repo, but the .git directory is not passed into the image as
# it is excluded via .dockerignore. Run the following from a Windows PowerShell prompt
# (elevation is not required): (Note Go must be installed to run these tests) |
155435b6 |
#
# >> hack\make.ps1 -DCO -PkgImports -GoFormat
|
f32b267d |
|
155435b6 |
# -----------------------------------------------------------------------------------------
|
f7b4d654 |
# To run unit tests, ensure you have created the nativebuildimage above. Then run one of
# the following from an (elevated) Windows PowerShell prompt: |
155435b6 |
#
# >> docker run --rm nativebuildimage hack\make.ps1 -TestUnit |
f7b4d654 |
# >> docker run --rm -m 2GB nativebuildimage hack\make.ps1 -TestUnit (if using Hyper-V containers) |
155435b6 |
# -----------------------------------------------------------------------------------------
|
f32b267d |
# To run unit tests and binary build, ensure you have created the nativebuildimage above. Then |
f7b4d654 |
# run one of the following from an (elevated) Windows PowerShell prompt: |
155435b6 |
#
# >> docker run nativebuildimage hack\make.ps1 -All |
f7b4d654 |
# >> docker run -m 2GB nativebuildimage hack\make.ps1 -All (if using Hyper-V containers) |
9a19bd1b |
# -----------------------------------------------------------------------------------------
|
eacd2fd4 |
# Important notes:
# ---------------
# |
dffa5d6d |
# Don't attempt to use a bind mount to pass a local directory as the bundles target
# directory. It does not work (golang attempts for follow a mapped folder incorrectly). |
155435b6 |
# Instead, use docker cp as per the example. |
eacd2fd4 |
# |
155435b6 |
# go.zip is not removed from the image as it is used by the Windows CI servers
# to ensure the host and image are running consistent versions of go. |
c7089b4b |
# |
155435b6 |
# Nanoserver support is a work in progress. Although the image will build if the
# FROM statement is updated, it will not work when running autogen through hack\make.ps1.
# It is suspected that the required GCC utilities (eg gcc, windres, windmc) silently
# quit due to the use of console hooks which are not available. |
9a19bd1b |
# |
155435b6 |
# The docker integration tests do not currently run in a container on Windows, predominantly
# due to Windows not supporting privileged mode, so anything using a volume would fail.
# They (along with the rest of the docker CI suite) can be run using
# https://github.com/jhowardmsft/docker-w2wCIScripts/blob/master/runCI/Invoke-DockerCI.ps1. |
f6ecfd62 |
# |
9a19bd1b |
# ----------------------------------------------------------------------------------------- |
6c88b8ef |
|
155435b6 |
|
9a19bd1b |
# The number of build steps below are explicitly minimised to improve performance. |
3bcf5823 |
# Extremely important - do not change the following line to reference a "specific" image,
# such as `mcr.microsoft.com/windows/servercore:ltsc2019`. If using this Dockerfile in process
# isolated containers, the kernel of the host must match the container image, and hence
# would fail between Windows Server 2016 (aka RS1) and Windows Server 2019 (aka RS5).
# It is expected that the image `microsoft/windowsservercore:latest` is present, and matches
# the hosts kernel version before doing a build. |
9a19bd1b |
FROM microsoft/windowsservercore |
6c88b8ef |
|
5add6213 |
# Use PowerShell as the default shell |
5e190644 |
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"] |
5add6213 |
|
eacd2fd4 |
# Environment variable notes: |
5add6213 |
# - GO_VERSION must be consistent with 'Dockerfile' used by Linux. |
eacd2fd4 |
# - FROM_DOCKERFILE is used for detection of building within a container. |
6d0823af |
ENV GO_VERSION=1.12.5 ` |
5e190644 |
GIT_VERSION=2.11.1 ` |
f2614f21 |
GOPATH=C:\go ` |
6c88b8ef |
FROM_DOCKERFILE=1
|
9a19bd1b |
RUN ` |
155435b6 |
Function Test-Nano() { `
$EditionId = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name 'EditionID').EditionId; `
return (($EditionId -eq 'ServerStandardNano') -or ($EditionId -eq 'ServerDataCenterNano') -or ($EditionId -eq 'NanoServer')); `
}`
` |
9a19bd1b |
Function Download-File([string] $source, [string] $target) { ` |
155435b6 |
if (Test-Nano) { `
$handler = New-Object System.Net.Http.HttpClientHandler; `
$client = New-Object System.Net.Http.HttpClient($handler); `
$client.Timeout = New-Object System.TimeSpan(0, 30, 0); `
$cancelTokenSource = [System.Threading.CancellationTokenSource]::new(); `
$responseMsg = $client.GetAsync([System.Uri]::new($source), $cancelTokenSource.Token); `
$responseMsg.Wait(); `
if (!$responseMsg.IsCanceled) { `
$response = $responseMsg.Result; `
if ($response.IsSuccessStatusCode) { `
$downloadedFileStream = [System.IO.FileStream]::new($target, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write); `
$copyStreamOp = $response.Content.CopyToAsync($downloadedFileStream); `
$copyStreamOp.Wait(); `
$downloadedFileStream.Close(); `
if ($copyStreamOp.Exception -ne $null) { throw $copyStreamOp.Exception } `
} `
} else { `
Throw ("Failed to download " + $source) `
}`
} else { `
$webClient = New-Object System.Net.WebClient; `
$webClient.DownloadFile($source, $target); `
} ` |
9a19bd1b |
} `
` |
5e190644 |
setx /M PATH $('C:\git\cmd;C:\git\usr\bin;'+$Env:PATH+';C:\gcc\bin;C:\go\bin'); ` |
155435b6 |
` |
9a19bd1b |
Write-Host INFO: Downloading git...; ` |
5e190644 |
$location='https://www.nuget.org/api/v2/package/GitForWindows/'+$Env:GIT_VERSION; `
Download-File $location C:\gitsetup.zip; ` |
9a19bd1b |
`
Write-Host INFO: Downloading go...; ` |
155435b6 |
Download-File $('https://golang.org/dl/go'+$Env:GO_VERSION+'.windows-amd64.zip') C:\go.zip; ` |
9a19bd1b |
`
Write-Host INFO: Downloading compiler 1 of 3...; ` |
155435b6 |
Download-File https://raw.githubusercontent.com/jhowardmsft/docker-tdmgcc/master/gcc.zip C:\gcc.zip; ` |
9a19bd1b |
`
Write-Host INFO: Downloading compiler 2 of 3...; ` |
155435b6 |
Download-File https://raw.githubusercontent.com/jhowardmsft/docker-tdmgcc/master/runtime.zip C:\runtime.zip; ` |
9a19bd1b |
`
Write-Host INFO: Downloading compiler 3 of 3...; ` |
155435b6 |
Download-File https://raw.githubusercontent.com/jhowardmsft/docker-tdmgcc/master/binutils.zip C:\binutils.zip; ` |
9a19bd1b |
` |
155435b6 |
Write-Host INFO: Extracting git...; ` |
5e190644 |
Expand-Archive C:\gitsetup.zip C:\git-tmp; `
New-Item -Type Directory C:\git | Out-Null; `
Move-Item C:\git-tmp\tools\* C:\git\.; `
Remove-Item -Recurse -Force C:\git-tmp; ` |
9a19bd1b |
` |
5add6213 |
Write-Host INFO: Expanding go...; `
Expand-Archive C:\go.zip -DestinationPath C:\; ` |
9a19bd1b |
` |
155435b6 |
Write-Host INFO: Expanding compiler 1 of 3...; ` |
5add6213 |
Expand-Archive C:\gcc.zip -DestinationPath C:\gcc -Force; ` |
155435b6 |
Write-Host INFO: Expanding compiler 2 of 3...; ` |
5add6213 |
Expand-Archive C:\runtime.zip -DestinationPath C:\gcc -Force; ` |
155435b6 |
Write-Host INFO: Expanding compiler 3 of 3...; ` |
5add6213 |
Expand-Archive C:\binutils.zip -DestinationPath C:\gcc -Force; ` |
9a19bd1b |
` |
155435b6 |
Write-Host INFO: Removing downloaded files...; `
Remove-Item C:\gcc.zip; `
Remove-Item C:\runtime.zip; `
Remove-Item C:\binutils.zip; ` |
5e190644 |
Remove-Item C:\gitsetup.zip; ` |
155435b6 |
`
Write-Host INFO: Creating source directory...; `
New-Item -ItemType Directory -Path C:\go\src\github.com\docker\docker | Out-Null; `
`
Write-Host INFO: Configuring git core.autocrlf...; ` |
5e190644 |
C:\git\cmd\git config --global core.autocrlf true; ` |
155435b6 |
` |
c7089b4b |
Write-Host INFO: Completed |
4677f803 |
|
155435b6 |
# Make PowerShell the default entrypoint
ENTRYPOINT ["powershell.exe"]
# Set the working directory to the location of the sources
WORKDIR C:\go\src\github.com\docker\docker |
eacd2fd4 |
|
155435b6 |
# Copy the sources into the container
COPY . . |