Siv docker ntau theem los tsim cov duab windows

Nyob zoo sawv daws! Kuv lub npe yog Andrey, thiab kuv ua haujlwm ua tus kws tshaj lij DevOps ntawm Exness hauv pab pawg txhim kho. Kuv lub luag haujlwm tseem ceeb yog cuam tshuam nrog kev tsim, xa tawm thiab txhawb cov ntawv thov hauv docker raws li Linux operating system (tom qab no hu ua OS). Tsis ntev dhau los kuv tau ua haujlwm nrog tib cov haujlwm, tab sis lub hom phiaj OS ntawm qhov project yog Windows Server thiab cov txheej txheem C ++. Rau kuv, qhov no yog thawj zaug kev sib raug zoo nrog docker ntim hauv Windows OS thiab, feem ntau, nrog C ++ daim ntawv thov. Ua tsaug rau qhov no, kuv tau muaj kev nthuav dav thiab kawm txog qee qhov kev tsis sib haum xeeb ntawm cov ntawv thov ntim hauv Windows.

Siv docker ntau theem los tsim cov duab windows

Hauv tsab xov xwm no kuv xav qhia koj txog qhov teeb meem uas kuv tau ntsib thiab kuv tau tswj xyuas lawv li cas. Kuv vam tias qhov no yuav pab tau rau koj cov kev cov nyom tam sim no thiab yav tom ntej. Txaus siab rau kev nyeem ntawv!

Vim li cas cov thawv ntim khoom?

Lub tuam txhab muaj cov txheej txheem uas twb muaj lawm rau Hashicorp Nomad thawv orchestrator thiab cov khoom muaj feem xyuam - Consul thiab Vault. Yog li ntawd, daim ntawv thov ntim tau raug xaiv los ua ib txoj kev sib koom ua ke rau kev xa cov tshuaj turnkey. Txij li thaum qhov project infrastructure muaj docker hosts nrog Windows Server Core OS versions 1803 thiab 1809, nws yog ib qho tsim nyog los tsim cov qauv sib cais ntawm docker dluab rau 1803 thiab 1809. Nyob rau hauv version 1803, nws yog ib qho tseem ceeb kom nco ntsoov tias tus lej hloov kho ntawm tus tsim docker host. yuav tsum phim tus lej hloov kho ntawm lub hauv paus docker duab thiab tus tswv tsev qhov twg lub thawv los ntawm daim duab no yuav raug tso tawm. Version 1809 tsis muaj qhov tsis zoo li no. Koj tuaj yeem nyeem ntxiv no.

Vim li cas ntau theem?

Pab neeg tsim kho engineers tsis muaj lossis txwv tsis pub nkag mus tsim cov tswv; tsis muaj txoj hauv kev los tswj cov txheej txheem sai sai rau kev tsim cov ntawv thov ntawm cov tswv no, piv txwv li, nruab ib lub cuab yeej ntxiv lossis ua haujlwm rau Visual Studio. Yog li ntawd, peb tau txiav txim siab los nruab tag nrho cov khoom tsim nyog los tsim daim ntawv thov rau hauv Docker duab. Yog tias tsim nyog, koj tuaj yeem hloov pauv tsuas yog dockerfile thiab tso lub raj xa dej los tsim cov duab no.

Los ntawm txoj kev xav mus rau kev ua

Hauv qhov zoo tshaj plaws Docker ntau theem duab tsim, ib puag ncig rau kev tsim daim ntawv thov yog npaj rau hauv tib daim ntawv Dockerfile raws li daim ntawv thov nws tus kheej tau tsim. Tab sis nyob rau hauv peb cov ntaub ntawv, ib qho kev sib txuas nruab nrab tau ntxiv, uas yog, cov kauj ruam ntawm kev tsim cov duab docker nrog txhua yam tsim nyog los tsim daim ntawv thov. Qhov no tau ua tiav vim kuv xav siv docker cache feature kom txo tau lub sijhawm teeb tsa ntawm txhua qhov kev vam khom.

Cia peb saib cov ntsiab lus tseem ceeb ntawm dockerfile tsab ntawv los tsim cov duab no.

Txhawm rau tsim cov duab ntawm OS versions sib txawv, koj tuaj yeem txhais qhov kev sib cav hauv dockerfile los ntawm qhov uas tus lej version dhau los thaum tsim, thiab nws kuj yog lub cim ntawm lub hauv paus duab.

Ib daim ntawv teev tag nrho ntawm Microsoft Windows Server duab cim tuaj yeem pom no.

ARG WINDOWS_OS_VERSION=1809
FROM mcr.microsoft.com/windows/servercore:$WINDOWS_OS_VERSION

Los ntawm lub neej ntawd cov lus txib hauv cov lus qhia RUN hauv dockerfile ntawm Windows OS lawv raug tua hauv cmd.exe console. Rau qhov yooj yim ntawm kev sau ntawv thiab nthuav dav cov haujlwm ntawm cov lus txib siv, peb yuav rov txhais cov lus txib ua tiav hauv Powershell los ntawm kev qhia. SHELL.

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

Cov kauj ruam tom ntej yog txhawm rau nruab tus tswj hwm pob chocolatey thiab cov pob tsim nyog:

COPY chocolatey.pkg.config .
RUN Set-ExecutionPolicy Bypass -Scope Process -Force ;
    [System.Net.ServicePointManager]::SecurityProtocol = 
    [System.Net.ServicePointManager]::SecurityProtocol -bor 3072 ;
    $env:chocolateyUseWindowsCompression = 'true' ;
    iex ((New-Object System.Net.WebClient).DownloadString( 
      'https://chocolatey.org/install.ps1')) ;
    choco install chocolatey.pkg.config -y --ignore-detected-reboot ;
    if ( @(0, 1605, 1614, 1641, 3010) -contains $LASTEXITCODE ) { 
      refreshenv; } else { exit $LASTEXITCODE; } ;
    Remove-Item 'chocolatey.pkg.config'

Txhawm rau txhim kho cov pob khoom siv chocolatey, koj tuaj yeem hla lawv ua ib daim ntawv teev npe, lossis nruab lawv ib zaug yog tias koj xav tau kom dhau cov cim tshwj xeeb rau txhua pob. Hauv peb qhov xwm txheej, peb siv cov ntaub ntawv manifest hauv hom XML, uas muaj cov npe ntawm cov pob khoom xav tau thiab lawv cov kev txwv. Nws cov ntsiab lus zoo li no:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="python" version="3.8.2"/>
  <package id="nuget.commandline" version="5.5.1"/>
  <package id="git" version="2.26.2"/>
</packages>

Tom ntej no, peb nruab daim ntawv thov tsim ib puag ncig, uas yog, MS Tsim Cov Cuab Yeej 2019 - qhov no yog qhov sib sib zog nqus ntawm Visual Studio 2019, uas muaj qhov tsawg kawg nkaus uas yuav tsum tau ua ntawm cov khoom siv rau kev sau cov lej.
Txhawm rau ua haujlwm tag nrho nrog peb txoj haujlwm C ++, peb yuav xav tau cov khoom ntxiv, uas yog:

  • Workload C++ cov cuab yeej
  • Cov cuab yeej v141
  • Windows 10 SDK (10.0.17134.0)

Koj tuaj yeem nruab cov cuab yeej txuas ntxiv tau siv cov ntaub ntawv teeb tsa hauv JSON hom. Configuration file contents:

Ib daim ntawv teev tag nrho ntawm cov khoom muaj nyob tuaj yeem nrhiav tau ntawm qhov chaw khaws ntaub ntawv Microsoft Visual Studio.

{
  "version": "1.0",
  "components": [
    "Microsoft.Component.MSBuild",
    "Microsoft.VisualStudio.Workload.VCTools;includeRecommended",
    "Microsoft.VisualStudio.Component.VC.v141.x86.x64",
    "Microsoft.VisualStudio.Component.Windows10SDK.17134"
  ]
}

Dockerfile khiav cov ntawv teeb tsa, thiab kom yooj yim, ntxiv txoj hauv kev rau cov cuab yeej tsim cov ntaub ntawv ua tiav rau ib puag ncig hloov pauv. PATH. Nws kuj tseem pom zoo kom tshem tawm cov ntaub ntawv tsis tsim nyog thiab cov npe kom txo qhov loj ntawm daim duab.

COPY buildtools.config.json .
RUN Invoke-WebRequest 'https://aka.ms/vs/16/release/vs_BuildTools.exe' 
      -OutFile '.vs_buildtools.exe' -UseBasicParsing ;
    Start-Process -FilePath '.vs_buildtools.exe' -Wait -ArgumentList 
      '--quiet --norestart --nocache --config C:buildtools.config.json' ;
    Remove-Item '.vs_buildtools.exe' ;
    Remove-Item '.buildtools.config.json' ;
    Remove-Item -Force -Recurse 
      'C:Program Files (x86)Microsoft Visual StudioInstaller' ;
    $env:PATH = 'C:Program Files (x86)Microsoft Visual Studio2019BuildToolsMSBuildCurrentBin;' + $env:PATH; 
    [Environment]::SetEnvironmentVariable('PATH', $env:PATH, 
      [EnvironmentVariableTarget]::Machine)

Nyob rau theem no, peb cov duab rau kev sau ib daim ntawv thov C ++ yog npaj txhij, thiab peb tuaj yeem mus ncaj qha mus rau kev tsim docker ntau theem ntawm daim ntawv thov.

Multi-theem hauv kev ua

Peb yuav siv cov duab tsim nrog tag nrho cov cuab yeej ntawm lub nkoj ua cov duab tsim. Raws li nyob rau hauv tsab ntawv dockerfile yav dhau los, peb yuav ntxiv lub peev xwm los qhia kom meej tus lej version / duab lossis kom yooj yim ntawm cov lej rov qab siv. Nws yog ib qho tseem ceeb kom ntxiv ib daim ntawv lo as builder rau cov duab sib dhos hauv cov lus qhia FROM.

ARG WINDOWS_OS_VERSION=1809
FROM buildtools:$WINDOWS_OS_VERSION as builder

Tam sim no nws yog lub sijhawm los tsim daim ntawv thov. Txhua yam ntawm no yog qhov yooj yim heev: luam qhov chaws thiab txhua yam cuam tshuam nrog nws, thiab pib cov txheej txheem muab tso ua ke.

COPY myapp .
RUN nuget restore myapp.sln ;
    msbuild myapp.sln /t:myapp /p:Configuration=Release

Cov theem kawg ntawm kev tsim cov duab kawg yog qhia cov duab hauv qab ntawm daim ntawv thov, qhov twg tag nrho cov khoom sib sau ua ke thiab cov ntaub ntawv teeb tsa yuav nyob. Txhawm rau luam cov ntaub ntawv sau los ntawm cov duab sib dhos nruab nrab, koj yuav tsum qhia qhov ntsuas --from=builder hauv cov lus qhia COPY.

FROM mcr.microsoft.com/windows/servercore:$WINDOWS_OS_VERSION

COPY --from=builder C:/x64/Release/myapp/ ./
COPY ./configs ./

Tam sim no txhua yam uas tseem tshuav yog ntxiv qhov tsim nyog kev vam khom rau peb daim ntawv thov ua haujlwm thiab qhia meej cov lus txib tso tawm los ntawm cov lus qhia ENTRYPOINT los yog CMD.

xaus

Hauv tsab xov xwm no, kuv tau tham txog yuav ua li cas los tsim ib qho chaw sau ua ke tag nrho rau C ++ daim ntawv thov hauv lub thawv hauv qab Windows thiab yuav ua li cas siv lub peev xwm ntawm docker ntau theem tsim los tsim cov duab tag nrho ntawm peb daim ntawv thov.

Tau qhov twg los: www.hab.com

Ntxiv ib saib