<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Windows 彙整 - 泰克哪裡去</title>
	<atom:link href="https://tech.uccu.website/category/it/windows/feed" rel="self" type="application/rss+xml" />
	<link>https://tech.uccu.website/category/it/windows</link>
	<description>一個科技相關的隨手記錄網站</description>
	<lastBuildDate>Sat, 17 Sep 2022 20:45:57 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>
<site xmlns="com-wordpress:feed-additions:1">119574712</site>	<item>
		<title>Windows Sandbox沙箱環境啟用GPU功能</title>
		<link>https://tech.uccu.website/windows-sandbox-enable-gpu.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=windows-sandbox-enable-gpu</link>
					<comments>https://tech.uccu.website/windows-sandbox-enable-gpu.html#comments</comments>
		
		<dc:creator><![CDATA[鳴人]]></dc:creator>
		<pubDate>Sat, 19 Feb 2022 07:35:08 +0000</pubDate>
				<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows Sandbox]]></category>
		<guid isPermaLink="false">https://tech.uccu.website/?p=1700</guid>

					<description><![CDATA[<p>Windows Sandbox沙箱是一個不錯的功能，除了可以在裡面進行一些測試或是不想留在Host的一些記錄之外，有時候更是拿來當作簡易陽春型的VM虛擬機器來使用。如果在裡面執行一些需要較多顯示運算能力的軟體時，記得啟用GPU的功能轉譯，才不會耗用太多的CPU運算資源。</p>
<p>這篇文章 <a href="https://tech.uccu.website/windows-sandbox-enable-gpu.html">Windows Sandbox沙箱環境啟用GPU功能</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>Windows Sandbox沙箱是一個不錯的功能，除了可以在裡面進行一些測試或是不想留在Host的一些記錄之外，有時候更是拿來當作簡易陽春型的VM虛擬機器來使用。(可以參考之前的<a href="https://tech.uccu.website/windows-10-home-install-sandbox-feature.html" target="_blank" rel="noreferrer noopener">這篇文章</a>，裡面有簡單的介紹並說明如何在Windows 10 家用Home版安裝Sandbox功能)</p>



<span id="more-1700"></span>



<p>這兩天試玩了某個Web的網頁遊戲，它有循環同一個關卡戰鬥的功能(為了獲得經驗/道具)，但是如果有程式蓋過它的全部畫面，它就會暫停執行。或是為了減少螢幕的耗電與延長使用壽命，所以人不在電腦前面的時候都會將系統內關閉螢幕顯示的間隔時間調低(直接按下關閉螢幕實體電源會讓解析度被調整)，這樣也會讓它暫停動作。</p>



<p>因為上面的這些因素，我希望它能夠在「掛機」的狀態繼續運作，所以就試著在Windows Sandbox沙箱環境執行，結果能夠避免上面這些因素造成的暫停執行，用Windows Sandbox沙箱環境執行了幾天之後，一直覺得這幾天的CPU使用率都在80%以上，雖然不以為然(主要是平常還開了三個BlueStacks的模擬器在掛天堂M)，但是還是心想這個網頁遊戲在沙箱功能裡有這麼吃資源嗎？</p>



<p>留意了一下沒有在Sandbox環境執行的時候CPU的使用率似乎沒有那麼高，代表其實資源並沒有吃那麼多，所以肯定是Sandbox環境額外增加了CPU的負擔…</p>



<p>？！</p>



<p>突然好像知道了些什麼！</p>



<p>應該是Sandbox環境沒有使用到GPU的運算功能，所有畫面運算的負擔都叫CPU去執行，所以造成了CPU較多的負擔。這樣的推測是很合理的，因此查了一下<a href="https://docs.microsoft.com/en-us/windows/security/threat-protection/windows-sandbox/windows-sandbox-configure-using-wsb-file#vgpu" target="_blank" rel="noreferrer noopener">Windows Sandbox沙箱環境設定GPU的文件</a>，果然預設值是沒有啟用GPU的功能，難怪…(一直以為預設是有使用GPU的…)，設定啟用GPU功能的方式非常簡單，把下面的內容複製貼到記事本，然後存成副檔名為wsb的文件(ex: SandboxEnableGPU.wsb)再點擊執行就可以開啟有啟用GPU功能的Sandbox沙箱環境了。</p>



<pre class="wp-block-prismatic-blocks"><code class="language-">&lt;Configuration&gt;
    &lt;vGPU&gt;enable&lt;/vGPU&gt;
&lt;/Configuration&gt;</code></pre>
<p>這篇文章 <a href="https://tech.uccu.website/windows-sandbox-enable-gpu.html">Windows Sandbox沙箱環境啟用GPU功能</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tech.uccu.website/windows-sandbox-enable-gpu.html/feed</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1700</post-id>	</item>
		<item>
		<title>【2021鐵人賽】建立自管的Azure DevOps Agent(Windows Container agent)</title>
		<link>https://tech.uccu.website/2021ironman-day21-build-windows-container-image-for-azure-devops-agent.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=2021ironman-day21-build-windows-container-image-for-azure-devops-agent</link>
					<comments>https://tech.uccu.website/2021ironman-day21-build-windows-container-image-for-azure-devops-agent.html#respond</comments>
		
		<dc:creator><![CDATA[鳴人]]></dc:creator>
		<pubDate>Sun, 03 Oct 2021 11:58:42 +0000</pubDate>
				<category><![CDATA[2021鐵人賽]]></category>
		<category><![CDATA[Azure DevOps]]></category>
		<category><![CDATA[Docker]]></category>
		<category><![CDATA[iThome鐵人賽]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[資訊科技]]></category>
		<category><![CDATA[2021ironman]]></category>
		<category><![CDATA[azure devops]]></category>
		<category><![CDATA[container]]></category>
		<category><![CDATA[docker]]></category>
		<guid isPermaLink="false">https://tech.uccu.website/?p=1235</guid>

					<description><![CDATA[<p>這篇延續前一篇在Windows VM中安裝Azure DevOps Agent的內容，講解了如何讓Agent在Windows Container中執行，從基底映像檔的差異到挑選適合的映像檔在Dockerfile中使用，也示範如何將Host中Docker提供給Container使用。</p>
<p>這篇文章 <a href="https://tech.uccu.website/2021ironman-day21-build-windows-container-image-for-azure-devops-agent.html">【2021鐵人賽】建立自管的Azure DevOps Agent(Windows Container agent)</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p><a href="https://tech.uccu.website/2021ironman-day20-setup-azure-devops-agent-on-windows-vm.html" target="_blank" rel="noreferrer noopener">前一篇</a>提到了在Windows VM中安裝Azure DevOps Agent，步驟非常的簡單，不過Azure DevOps Agent也可以在Container內執行，這一篇就來看看如何在Windows Container內執行Azure DevOps Agent。(如果需要Windows Server安裝Docker的方式請看<a href="https://tech.uccu.website/install-docker-on-windows-server.html" target="_blank" rel="noreferrer noopener">這篇文章</a>)</p>



<p>首先，在Windows Container的部份有些基本的知識需要了解，那就是Windows Container區分不同的基底映像類型(Base Image)，分為Windows、Windows Server、Windows Server Core、Nano Server四種，越前面的映像檔大小所佔的容量越大，包含的功能越多，越後面的則越小，所包含的功能也越少。(參考官方文件：<a href="https://docs.microsoft.com/zh-tw/virtualization/windowscontainers/manage-containers/container-base-images" target="_blank" rel="noreferrer noopener">中文</a>/<a href="https://docs.microsoft.com/en-us/virtualization/windowscontainers/manage-containers/container-base-images" target="_blank" rel="noreferrer noopener">英文</a>)</p>



<p>另外就是Windows Container的隔離模式(Isolation  Modes)，分為Process和Hyper-V隔離這兩種，在Windows Server上執行的Windows Container預設以Process隔離方式執行，在Windows 10執行的Windows Container則預設以Hyper-V隔離方式執行。(參考官方文件：<a href="https://docs.microsoft.com/zh-tw/virtualization/windowscontainers/manage-containers/hyperv-container" target="_blank" rel="noreferrer noopener">中文</a>/<a href="https://docs.microsoft.com/en-us/virtualization/windowscontainers/manage-containers/hyperv-container" target="_blank" rel="noreferrer noopener">英文</a>)</p>



<p>在上面這兩個不同的差異之下，選擇建立Windows Container Image就會有很多的選擇，這牽涉到執行Container的主機(Host)所使用的是哪一個版本的OS，不同版本之間的相容性會有所差異，例如：Windows Server 2019與Windows Server 20H2這兩個版本的OS相容性：</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img data-recalc-dims="1" fetchpriority="high" decoding="async" width="1024" height="574" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/e7ee6766-oscompatibility-winserver2019.png?resize=1024%2C574&#038;ssl=1" alt="" class="wp-image-1238" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/e7ee6766-oscompatibility-winserver2019.png?w=1024&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/e7ee6766-oscompatibility-winserver2019.png?resize=300%2C168&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/e7ee6766-oscompatibility-winserver2019.png?resize=768%2C431&amp;ssl=1 768w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>Windows Server 2019的OS相容性</figcaption></figure></div>



<div class="wp-block-image"><figure class="aligncenter size-large"><img data-recalc-dims="1" decoding="async" width="1024" height="573" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/d95ace76-oscompatibility-winserver20h2-1024x573.png?resize=1024%2C573&#038;ssl=1" alt="" class="wp-image-1241" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/d95ace76-oscompatibility-winserver20h2.png?resize=1024%2C573&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/d95ace76-oscompatibility-winserver20h2.png?resize=300%2C168&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/d95ace76-oscompatibility-winserver20h2.png?resize=768%2C430&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/d95ace76-oscompatibility-winserver20h2.png?w=1031&amp;ssl=1 1031w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>Windows Server 20H2的OS相容性</figcaption></figure></div>



<p>從上面兩張圖可以看到Windows Server 2019的Host可以支援以Windows Server 2019為基底的Container Image以Process隔離模式執行，若是以Hyper-V隔離模式執行，則是可以支援Windows Server 2019以下的版本。同樣的，Windows Server 20H2的Host可以支援同為Windows Server 20H2的Image以Process隔離模式執行，Hyper-V的隔離模式則是與Host相同版本以下的都能夠支援。</p>



<p>換成是Windows 10作為Host的話，支援性又有不同的差異：</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img data-recalc-dims="1" decoding="async" width="1015" height="566" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/a65d2b83-oscompatibility-win10_1909.png?resize=1015%2C566&#038;ssl=1" alt="" class="wp-image-1244" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/a65d2b83-oscompatibility-win10_1909.png?w=1015&amp;ssl=1 1015w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/a65d2b83-oscompatibility-win10_1909.png?resize=300%2C167&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/a65d2b83-oscompatibility-win10_1909.png?resize=768%2C428&amp;ssl=1 768w" sizes="(max-width: 1015px) 100vw, 1015px" /><figcaption>Windows 10 1909版本的OS相容性</figcaption></figure></div>



<div class="wp-block-image"><figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1016" height="574" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/06a1c852-oscompatibility-win10_2004.png?resize=1016%2C574&#038;ssl=1" alt="" class="wp-image-1247" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/06a1c852-oscompatibility-win10_2004.png?w=1016&amp;ssl=1 1016w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/06a1c852-oscompatibility-win10_2004.png?resize=300%2C169&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/06a1c852-oscompatibility-win10_2004.png?resize=768%2C434&amp;ssl=1 768w" sizes="auto, (max-width: 1016px) 100vw, 1016px" /><figcaption>Windows 10 2004版本的OS相容性</figcaption></figure></div>



<div class="wp-block-image"><figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1017" height="570" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/530b0e7f-oscompatibility-win10_20h2.png?resize=1017%2C570&#038;ssl=1" alt="" class="wp-image-1250" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/530b0e7f-oscompatibility-win10_20h2.png?w=1017&amp;ssl=1 1017w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/530b0e7f-oscompatibility-win10_20h2.png?resize=300%2C168&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/530b0e7f-oscompatibility-win10_20h2.png?resize=768%2C430&amp;ssl=1 768w" sizes="auto, (max-width: 1017px) 100vw, 1017px" /><figcaption>Windows 10 20H2版本的OS相容性</figcaption></figure></div>



<p>Windows 10的部份可以從上面三張圖看到一點點差異，就是Windows 10的1909版本並不能支援Windows Server任何版本為基底映像檔以Process隔離模式執行，Windows 10的2004版本之後才開始支援與Windows Server相對應的版本映像檔以Process隔離模式執行，但是以Hyper-V隔離模式執行的話則是支援相同版本以下的OS映像檔。</p>



<p>有點複雜，對吧？</p>



<p>簡單來說，要能夠支援以Process隔離模式執行，必須是Container Base Image和Host為相同版本的OS，若是以Hyper-V隔離模式執行，則必須挑選Host OS版本以下製成的Container Image，也就是說Windows Server 2016的選擇性非常少，只能使用同為Windows Server 2016為基底製作的映像檔，越新的OS版本則支援以Hyper-V隔離模式執行的Container Image選擇越多。</p>



<p>更多Windows Container版本相容性的部份可以參考官方文件的說明(<a href="https://docs.microsoft.com/zh-tw/virtualization/windowscontainers/deploy-containers/version-compatibility" target="_blank" rel="noreferrer noopener">中文</a>/<a href="https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/version-compatibility" target="_blank" rel="noreferrer noopener">英文</a>)。</p>



<p>回到Azure DevOps Agent，上面說了那麼多，主要是必須考量Agent要執行的任務需要多大的支援與資源，若是只需要能夠以dotnet sdk建置專案，那麼選擇任何一個OS版本的Image應該都可以，只要能夠在Image裡面安裝dotnet sdk即可。</p>



<p>以dotnet sdk這個例子來說，在官方文件「<a href="https://docs.microsoft.com/zh-tw/azure/devops/pipelines/agents/docker?view=azure-devops" target="_blank" rel="noreferrer noopener">在 Docker 中執行自我裝載的代理程式</a>」所提到的Dockerfile範例是使用Windows Server Core 2019的基底映像檔，裡面並沒有安裝dotnet sdk：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-dockerfile">FROM mcr.microsoft.com/windows/servercore:ltsc2019

WORKDIR /azp

COPY start.ps1 .

CMD powershell .\start.ps1</code></pre>



<p>所以要讓Agent可以執行dotnet build的任務，可以選擇使用上面的範例，在Dockerfile中再另外加入安裝dotnet sdk的內容，或是從<a href="https://hub.docker.com/" target="_blank" rel="noreferrer noopener">Docker hub</a>上另外找<a href="https://hub.docker.com/_/microsoft-dotnet-sdk/" target="_blank" rel="noreferrer noopener">微軟官方製作的dotnet sdk映像檔</a>，找到對應的Container Repository與Image tag，例如：「mcr.microsoft.com/dotnet/sdk:5.0-windowsservercore-ltsc2019」這個就是同樣以Windows Server Core 2019為基底的Image，並且安裝了dotnet 5.0 sdk。</p>



<p>從官方文件「<a href="https://docs.microsoft.com/zh-tw/azure/devops/pipelines/agents/docker?view=azure-devops" target="_blank" rel="noreferrer noopener">在 Docker 中執行自我裝載的代理程式</a>」 中的範例可以看得出來，在Container中執行Azure DevOps Agent的關鍵在於下面這段PowerShell內容，將它複製之後以start.ps1儲存在與Dockerfile相同的位置即可：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-powershell">if (-not (Test-Path Env:AZP_URL)) {
  Write-Error &quot;error: missing AZP_URL environment variable&quot;
  exit 1
}

if (-not (Test-Path Env:AZP_TOKEN_FILE)) {
  if (-not (Test-Path Env:AZP_TOKEN)) {
    Write-Error &quot;error: missing AZP_TOKEN environment variable&quot;
    exit 1
  }

  $Env:AZP_TOKEN_FILE = &quot;\azp\.token&quot;
  $Env:AZP_TOKEN | Out-File -FilePath $Env:AZP_TOKEN_FILE
}

Remove-Item Env:AZP_TOKEN

if ((Test-Path Env:AZP_WORK) -and -not (Test-Path $Env:AZP_WORK)) {
  New-Item $Env:AZP_WORK -ItemType directory | Out-Null
}

New-Item &quot;\azp\agent&quot; -ItemType directory | Out-Null

# Let the agent ignore the token env variables
$Env:VSO_AGENT_IGNORE = &quot;AZP_TOKEN,AZP_TOKEN_FILE&quot;

Set-Location agent

Write-Host &quot;1. Determining matching Azure Pipelines agent...&quot; -ForegroundColor Cyan

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(&quot;:$(Get-Content ${Env:AZP_TOKEN_FILE})&quot;))
$package = Invoke-RestMethod -Headers @{Authorization=(&quot;Basic $base64AuthInfo&quot;)} &quot;$(${Env:AZP_URL})/_apis/distributedtask/packages/agent?platform=win-x64&amp;`$top=1&quot;
$packageUrl = $package[0].Value.downloadUrl

Write-Host $packageUrl

Write-Host &quot;2. Downloading and installing Azure Pipelines agent...&quot; -ForegroundColor Cyan

$wc = New-Object System.Net.WebClient
$wc.DownloadFile($packageUrl, &quot;$(Get-Location)\agent.zip&quot;)

Expand-Archive -Path &quot;agent.zip&quot; -DestinationPath &quot;\azp\agent&quot;

try
{
  Write-Host &quot;3. Configuring Azure Pipelines agent...&quot; -ForegroundColor Cyan

  .\config.cmd --unattended `
    --agent &quot;$(if (Test-Path Env:AZP_AGENT_NAME) { ${Env:AZP_AGENT_NAME} } else { ${Env:computername} })&quot; `
    --url &quot;$(${Env:AZP_URL})&quot; `
    --auth PAT `
    --token &quot;$(Get-Content ${Env:AZP_TOKEN_FILE})&quot; `
    --pool &quot;$(if (Test-Path Env:AZP_POOL) { ${Env:AZP_POOL} } else { &#039;Default&#039; })&quot; `
    --work &quot;$(if (Test-Path Env:AZP_WORK) { ${Env:AZP_WORK} } else { &#039;_work&#039; })&quot; `
    --replace

  Write-Host &quot;4. Running Azure Pipelines agent...&quot; -ForegroundColor Cyan

  .\run.cmd
}
finally
{
  Write-Host &quot;Cleanup. Removing Azure Pipelines agent...&quot; -ForegroundColor Cyan

  .\config.cmd remove --unattended `
    --auth PAT `
    --token &quot;$(Get-Content ${Env:AZP_TOKEN_FILE})&quot;
}</code></pre>



<p>接著就是準備我們要用的Dockerfile，這邊我以dotnet 5.0 sdk的Windows Server Core 2022映像檔為基底，它裡面包含了PowerShell與dotnet 5.0 sdk，並且我還要額外在Image裡面安裝Docker。若是需要dotnet core 3.1與其它OS基底映像檔的部份可以到<a href="https://hub.docker.com/_/microsoft-dotnet-sdk/" target="_blank" rel="noreferrer noopener">Docker hub的頁面</a>上去尋找相關的Tag：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-dockerfile">FROM mcr.microsoft.com/dotnet/sdk:5.0-windowsservercore-ltsc2022

#在這底下加入要額外裝在Docker Image內的程式

SHELL [&quot;powershell&quot;, &quot;-Command&quot;, &quot;$ErrorActionPreference = &#039;Stop&#039;; $ProgressPreference = &#039;SilentlyContinue&#039;;&quot;]

RUN Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
RUN Install-Module -Name DockerMsftProvider -Repository PSGallery -Force -verbose
RUN Install-Package -Name docker -ProviderName DockerMsftProvider -Force -verbose -ErrorAction SilentlyContinue; exit 0

#===========================================

WORKDIR /azp

COPY start.ps1 .

CMD powershell .\start.ps1</code></pre>



<p>準備好Dockerfile和start.ps1這兩個檔案之後，執行下面的指令建立映像檔：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-docker">docker build -t devopsagent .</code></pre>



<p>完成Build Image的動作之後，透過下面的指令建立一個名為azure-agent的Container：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-docker">docker run --name azure-agent -d -v \.\pipe\docker_engine:\.\pipe\docker_engine -e AZP_URL=[AzureDevOpsURL] -e AZP_TOKEN=[AzureDevOpsPAT] -e AZP_AGENT_NAME=[AzureDevOpsAgentName] IMAGE:TAG</code></pre>



<p>指令中比較特別的地方是透過mount的方式將host的docker轉給container內使用，也就是「-v \.\pipe\docker_engine:\.\pipe\docker_engine」這段，其它的除了IMAGE:TAG是根據上面的docker build所指定的Repository與Tag(預設為latest)之外，環境變數的部份則參考下面這張表：</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1025" height="408" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/4582d3e6-devopsagent-containerenvironmentvariables.png?resize=1025%2C408&#038;ssl=1" alt="" class="wp-image-1265" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/4582d3e6-devopsagent-containerenvironmentvariables.png?w=1025&amp;ssl=1 1025w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/4582d3e6-devopsagent-containerenvironmentvariables.png?resize=300%2C119&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/4582d3e6-devopsagent-containerenvironmentvariables.png?resize=768%2C306&amp;ssl=1 768w" sizes="auto, (max-width: 1025px) 100vw, 1025px" /><figcaption>Azure DevOps Agent所使用的環境變數</figcaption></figure></div>



<p>五個環境變數中的最後兩個AZP_POOL、AZP_WORK是選擇性的，只有前三個是必須的，執行之後的結果如下：</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="755" height="533" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/2af50f9a-devopsagent-runagentinwindowscontainer.png?resize=755%2C533&#038;ssl=1" alt="" class="wp-image-1268" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/2af50f9a-devopsagent-runagentinwindowscontainer.png?w=755&amp;ssl=1 755w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/2af50f9a-devopsagent-runagentinwindowscontainer.png?resize=300%2C212&amp;ssl=1 300w" sizes="auto, (max-width: 755px) 100vw, 755px" /><figcaption>Azure DevOps Agent執行在Windows Container中</figcaption></figure></div>



<p>從Azure DevOps中的Agent Pools列表中可以看到這個Agent：</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="793" height="277" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/6ad9bba5-agentpools-default-wincontaineragent.png?resize=793%2C277&#038;ssl=1" alt="" class="wp-image-1271" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/6ad9bba5-agentpools-default-wincontaineragent.png?w=793&amp;ssl=1 793w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/6ad9bba5-agentpools-default-wincontaineragent.png?resize=300%2C105&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/6ad9bba5-agentpools-default-wincontaineragent.png?resize=768%2C268&amp;ssl=1 768w" sizes="auto, (max-width: 793px) 100vw, 793px" /></figure></div>



<p>進入查看Capabilities的部份也可以看到幾個比較不一樣的資訊，像是ComputerName是Docker產生的隨機名稱，還有docker的資訊：</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="790" height="927" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/df70a783-agentpools-default-wincontaineragent-capabilities.png?resize=790%2C927&#038;ssl=1" alt="" class="wp-image-1274" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/df70a783-agentpools-default-wincontaineragent-capabilities.png?w=790&amp;ssl=1 790w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/df70a783-agentpools-default-wincontaineragent-capabilities.png?resize=256%2C300&amp;ssl=1 256w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/df70a783-agentpools-default-wincontaineragent-capabilities.png?resize=768%2C901&amp;ssl=1 768w" sizes="auto, (max-width: 790px) 100vw, 790px" /><figcaption>Azure DevOps Agent執行在Windows Container中的Capabilities資訊</figcaption></figure></div>



<p>至於為什麼需要將host的docker提供給Container中的docker使用，這部份則是為了讓Container中的docker可以共用host上的docker，從Container中的Agent執行docker build所產生的docker image也會儲存在host的docker images裡面，並且可以代替host執行docker run建立新的Container。不過這麼做也是有安全性的風險的，<a href="https://docs.microsoft.com/zh-tw/azure/devops/pipelines/agents/docker?view=azure-devops#use-docker-within-a-docker-container" target="_blank" rel="noreferrer noopener">官方就有提出警告</a>，是否要這麼做則是可以自行思考決定：</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1017" height="367" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/7582834f-waring-usedockerindocker.png?resize=1017%2C367&#038;ssl=1" alt="" class="wp-image-1277" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/7582834f-waring-usedockerindocker.png?w=1017&amp;ssl=1 1017w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/7582834f-waring-usedockerindocker.png?resize=300%2C108&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/7582834f-waring-usedockerindocker.png?resize=768%2C277&amp;ssl=1 768w" sizes="auto, (max-width: 1017px) 100vw, 1017px" /></figure></div>



<p>會在這篇文章中這麼做的原因，純粹是為了提供Windows環境將docker轉給Container內使用的指令使用方式，因為這部份在微軟的文件頁面上似乎沒有提到(我忘了之前從哪裡看到的)。</p>
<p>這篇文章 <a href="https://tech.uccu.website/2021ironman-day21-build-windows-container-image-for-azure-devops-agent.html">【2021鐵人賽】建立自管的Azure DevOps Agent(Windows Container agent)</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tech.uccu.website/2021ironman-day21-build-windows-container-image-for-azure-devops-agent.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1235</post-id>	</item>
		<item>
		<title>Windows Server安裝Docker與Container功能</title>
		<link>https://tech.uccu.website/install-docker-on-windows-server.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=install-docker-on-windows-server</link>
					<comments>https://tech.uccu.website/install-docker-on-windows-server.html#respond</comments>
		
		<dc:creator><![CDATA[鳴人]]></dc:creator>
		<pubDate>Sat, 02 Oct 2021 18:00:21 +0000</pubDate>
				<category><![CDATA[Docker]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[docker]]></category>
		<category><![CDATA[windows server]]></category>
		<guid isPermaLink="false">https://tech.uccu.website/?p=1211</guid>

					<description><![CDATA[<p>Windows Server要安裝Docker很簡單，只需要透過具有Administrator權限的PowerShell執行三行指令就可以完成安裝Docker的動作，分別是安裝Provider、安裝Docker、重新開機。</p>
<p>這篇文章 <a href="https://tech.uccu.website/install-docker-on-windows-server.html">Windows Server安裝Docker與Container功能</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>Windows 10 Pro版本以上要安裝Docker不難，只要從docker的網站下載<a href="https://desktop.docker.com/win/main/amd64/Docker%20Desktop%20Installer.exe" target="_blank" rel="noreferrer noopener">Docker Desktop for Windows的安裝檔</a>，一般來說就是一直下一步就行了，如果是Windows 10 Home版想要安裝Docker，因為Docker需要Hyper-V的功能，所以Windows 10 Home版需要透過一些技巧安裝Hyper-V功能之後再安裝Docker，詳細的內容可以參考先前的文章：<a href="https://tech.uccu.website/windows10-home-install-hyperv-docker.html" target="_blank" rel="noreferrer noopener">Windows 10 Home家用版安裝Hyper-V與Docker</a>。</p>



<p>最近在寫<a href="https://tech.uccu.website/2021ironman" target="_blank" rel="noreferrer noopener">iThome鐵人賽的文章</a>，有一篇文章需要在Windows Server上使用Docker與Container的功能，在Azure上建立了一個VM，所以就順便將過程記錄一下。</p>



<p>首先，必須先將Container功能安裝進去(如果這台也要安裝Docker可以跳過這步)，透過ServerManager的AddRolesAndFeatures叫出UI介面之後在Features的部份找到Containers：</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="374" height="167" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/7545dbd7-windowsserver-servermanager-addrolesandfeatures.png?resize=374%2C167&#038;ssl=1" alt="" class="wp-image-1214" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/7545dbd7-windowsserver-servermanager-addrolesandfeatures.png?w=374&amp;ssl=1 374w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/7545dbd7-windowsserver-servermanager-addrolesandfeatures.png?resize=300%2C134&amp;ssl=1 300w" sizes="auto, (max-width: 374px) 100vw, 374px" /></figure></div>



<div class="wp-block-image"><figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="785" height="561" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/d4e98f13-windowsserver-servermanager-addrolesandfeatures-features-containers.png?resize=785%2C561&#038;ssl=1" alt="" class="wp-image-1217" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/d4e98f13-windowsserver-servermanager-addrolesandfeatures-features-containers.png?w=785&amp;ssl=1 785w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/d4e98f13-windowsserver-servermanager-addrolesandfeatures-features-containers.png?resize=300%2C214&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/d4e98f13-windowsserver-servermanager-addrolesandfeatures-features-containers.png?resize=768%2C549&amp;ssl=1 768w" sizes="auto, (max-width: 785px) 100vw, 785px" /></figure></div>



<p>勾選之後安裝完需要重新開機。</p>



<p>重新開機之後開啟具有Administrator權限的PowerShell，輸入下面這行指令：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-powershell">Install-Module -Name DockerMsftProvider -Repository PSGallery -Force</code></pre>



<p>上面這行是安裝稱為&nbsp;<a href="https://github.com/OneGet/MicrosoftDockerProvider">DockerMicrosoftProvider</a>&nbsp;的&nbsp;<a href="https://github.com/oneget/oneget">OneGet 提供者 PowerShell 模組</a>。並且需要NuGet提供者(Provider)，如果沒安裝過的話會一併提示是否安裝(預設為Yes)：</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="859" height="173" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/72bb2e85-windowsserver-powershell-installdockerprovider.png?resize=859%2C173&#038;ssl=1" alt="" class="wp-image-1220" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/72bb2e85-windowsserver-powershell-installdockerprovider.png?w=859&amp;ssl=1 859w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/72bb2e85-windowsserver-powershell-installdockerprovider.png?resize=300%2C60&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/72bb2e85-windowsserver-powershell-installdockerprovider.png?resize=768%2C155&amp;ssl=1 768w" sizes="auto, (max-width: 859px) 100vw, 859px" /></figure></div>



<p>接著輸入下面這行指令，從剛才安裝的DockerMicrosoftProvider中安裝Docker：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-powershell">Install-Package -Name docker -ProviderName DockerMsftProvider</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="808" height="183" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/8ba0e065-windowsserver-powershell-installdocker.png?resize=808%2C183&#038;ssl=1" alt="" class="wp-image-1223" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/8ba0e065-windowsserver-powershell-installdocker.png?w=808&amp;ssl=1 808w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/8ba0e065-windowsserver-powershell-installdocker.png?resize=300%2C68&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/8ba0e065-windowsserver-powershell-installdocker.png?resize=768%2C174&amp;ssl=1 768w" sizes="auto, (max-width: 808px) 100vw, 808px" /></figure></div>



<p>安裝完成之後需要重新啟動電腦，可以透過UI操作，或是輸入下面的PowerShell指令：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-powershell">Restart-Computer -Force</code></pre>



<p>重開機之後再輸入docker info就可以看到docker相關的資訊了。</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="627" height="649" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/ff2ef8d6-windowsserver-powershell-dockerinfo.png?resize=627%2C649&#038;ssl=1" alt="" class="wp-image-1226" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/ff2ef8d6-windowsserver-powershell-dockerinfo.png?w=627&amp;ssl=1 627w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2021/10/ff2ef8d6-windowsserver-powershell-dockerinfo.png?resize=290%2C300&amp;ssl=1 290w" sizes="auto, (max-width: 627px) 100vw, 627px" /></figure></div>



<p>參考連結：</p>



<ul class="wp-block-list"><li><a href="https://docs.microsoft.com/zh-tw/virtualization/windowscontainers/quick-start/set-up-environment?tabs=Windows-Server" target="_blank" rel="noreferrer noopener">開始使用：準備適用於容器的 Windows</a></li></ul>
<p>這篇文章 <a href="https://tech.uccu.website/install-docker-on-windows-server.html">Windows Server安裝Docker與Container功能</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tech.uccu.website/install-docker-on-windows-server.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1211</post-id>	</item>
		<item>
		<title>Windows Container中修改json檔案設定值的方法</title>
		<link>https://tech.uccu.website/how-to-modify-json-file-in-windows-container.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-modify-json-file-in-windows-container</link>
					<comments>https://tech.uccu.website/how-to-modify-json-file-in-windows-container.html#respond</comments>
		
		<dc:creator><![CDATA[鳴人]]></dc:creator>
		<pubDate>Tue, 24 Nov 2020 09:28:50 +0000</pubDate>
				<category><![CDATA[Windows]]></category>
		<category><![CDATA[appsettings.json]]></category>
		<category><![CDATA[container]]></category>
		<category><![CDATA[docker]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[windows]]></category>
		<guid isPermaLink="false">https://tech.uccu.website/?p=153</guid>

					<description><![CDATA[<p>通常使用到Docker技術執行Container容器都是使用Linux環境，但是Windows環境也可以使用C ... <a title="Windows Container中修改json檔案設定值的方法" class="read-more" href="https://tech.uccu.website/how-to-modify-json-file-in-windows-container.html" aria-label="Read more about Windows Container中修改json檔案設定值的方法">閱讀全文</a></p>
<p>這篇文章 <a href="https://tech.uccu.website/how-to-modify-json-file-in-windows-container.html">Windows Container中修改json檔案設定值的方法</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>通常使用到Docker技術執行Container容器都是使用Linux環境，但是Windows環境也可以使用Container，有的時候因為一些考量會需要製作兩套不同環境(Windows &amp; Linux)的Docker Image，所以前面一篇文章提到了如何在<em><a href="https://tech.uccu.website/how-to-modify-json-file-in-linux-container.html" target="_blank" rel="noreferrer noopener">Linux Container中修改json檔案設定值的方法</a></em>，這一篇則是來說明如何在Windows Container中做到一樣的事情。</p>



<p>不同的地方在於，Linux環境中透過額外安裝jq套件來達成修改json檔案的作法，在Windows環境下我們則是使用PowerShell來達成這樣的事情，所以不需要額外安裝任何套件(PowerShell應該都有內建在Windows環境中)。</p>



<span id="more-153"></span>



<p>下面的內容和<em><a href="https://tech.uccu.website/how-to-modify-json-file-in-linux-container.html" target="_blank" rel="noreferrer noopener">Linux Container中修改json檔案設定值的方法</a></em>文章中的內容幾乎一樣，我額外用不同的字型顏色標出不同的地方，以便參考。</p>



<h2 class="wp-block-heading">使用方式</h2>



<p>假設在Dockerfile中定義了<em><strong>ENV&nbsp;LOG_LEVEL=Error</strong></em>，並且appsettings.json檔案中有段Serilog的設定如下：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-json">{
    &quot;Serilog&quot;: {
        &quot;MinimumLevel&quot;: &quot;Debug&quot;,
        &quot;WriteTo&quot;: [
            {
                &quot;Name&quot;: &quot;File&quot;,
                &quot;Args&quot;: {
                    &quot;path&quot;: &quot;MyApp.log&quot;,
                    &quot;outputTemplate&quot;: &quot;{Timestamp:yyyy-MM-dd HH:mm:ss} RequestID:{RequestID} User:{UserID} {RequestPath} {SourceContext} [{Level:u3}] {Message:lj}{NewLine}{Exception}&quot;,
                    &quot;rollingInterval&quot;: &quot;Day&quot;
                }
            }
        ],
        &quot;Enrich&quot;: [ &quot;FromLogContext&quot; ]
    }
}</code></pre>



<p>可以看到在Serilog中有個MinimumLevel屬性設定的是Debug，這個設定會記錄大量的Log記錄，但是有時候在執行container的時候並不想要記錄這麼詳細的資訊，以降低檔案大小，減少儲存空間的耗用時，有個LOG_LEVEL的ENV設定就可以方便的調整設定值。</p>



<p>現在假設有個.Net Core的程式叫MyApp，Build完之後有個MyApp.dll檔案，在Dockerfile中設定了ENV和複製相關的執行檔案之外，還需要一個<span style="color:#0000ff" class="has-inline-color">.ps1檔案(Powershell的副檔名)</span>，假設這個檔案叫做app-init.<span style="color:#0000ff" class="has-inline-color">ps1</span>。</p>



<p>接下來在app-init<span style="color:#0000ff" class="has-inline-color">.ps1</span>檔案中要做下面這幾件事：</p>



<ol class="wp-block-list"><li>判斷container是不是第一次執行，以避免每次都進行修改appsettings.json的動作。</li><li>透過<span style="color:#0000ff" class="has-inline-color">Powershell的Get-Content指令</span>將appsettings.json的內容讀入記憶體。</li><li>找到MinimumLevel屬性，將值修改為LOG_LEVEL這個ENV所設定的值。</li><li>將<span style="color:#0000ff" class="has-inline-color">Powershell</span>修改後的內容儲存為appsettings.temp.json。</li><li>將原本的appsettings.json改名為appsettings.ori.json。</li><li>將前面的appsettings.temp.json改名為appsettings.json。</li><li>產生一個用來判斷是否執行過app-init<span style="color:#0000ff" class="has-inline-color">.ps1</span>的檔案，例如：inited<span style="color:#0000ff" class="has-inline-color">.txt</span>。</li><li>啟動MyApp。</li></ol>



<pre class="wp-block-prismatic-blocks"><code class="language-powershell">Set-Location C:\app

if (Test-Path inited.txt) {
    echo &quot;Powershell already run finished.&quot;
}
else {
    $JsonSettings = Get-Content appsettings.json | ConvertFrom-Json
    $JsonSettings.Serilog.MinimumLevel = &quot;$env:LOG_LEVEL&quot;
    $JsonSettings | ConvertTo-Json -Depth 10 &gt; appsettings.temp.json

    Rename-Item appsettings.json appsettings.ori.json
    Rename-Item appsettings.temp.json appsettings.json
    &quot;Powershell run finished.&quot; &gt; inited.txt
}

dotnet MyApp.dll</code></pre>
<p>這篇文章 <a href="https://tech.uccu.website/how-to-modify-json-file-in-windows-container.html">Windows Container中修改json檔案設定值的方法</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tech.uccu.website/how-to-modify-json-file-in-windows-container.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">153</post-id>	</item>
		<item>
		<title>同一台電腦可執行Windows與Linux Container？在WSL2安裝原生Docker環境</title>
		<link>https://tech.uccu.website/how-to-install-docker-on-wsl2.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-install-docker-on-wsl2</link>
					<comments>https://tech.uccu.website/how-to-install-docker-on-wsl2.html#respond</comments>
		
		<dc:creator><![CDATA[鳴人]]></dc:creator>
		<pubDate>Thu, 10 Sep 2020 09:10:50 +0000</pubDate>
				<category><![CDATA[Docker]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[container]]></category>
		<category><![CDATA[docker]]></category>
		<category><![CDATA[wsl]]></category>
		<guid isPermaLink="false">https://tech.uccu.website/?p=70</guid>

					<description><![CDATA[<p>如果想要在同一台電腦中既可以執行Windows Container，又可以執行Linux Container， ... <a title="同一台電腦可執行Windows與Linux Container？在WSL2安裝原生Docker環境" class="read-more" href="https://tech.uccu.website/how-to-install-docker-on-wsl2.html" aria-label="Read more about 同一台電腦可執行Windows與Linux Container？在WSL2安裝原生Docker環境">閱讀全文</a></p>
<p>這篇文章 <a href="https://tech.uccu.website/how-to-install-docker-on-wsl2.html">同一台電腦可執行Windows與Linux Container？在WSL2安裝原生Docker環境</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>如果想要在同一台電腦中既可以執行Windows Container，又可以執行Linux Container，以便測試程式在不同環境之下執行的結果或是設定值的差異，該如何達到這樣的需求呢？</p>



<p>答案就是：利用Windows中的Linux子系統(Windows Subsystem for Linux, WSL)。</p>



<p>透過WSL安裝Linux子系統之後，可以在Linux子系統中安裝原生的Docker執行環境，在Windows環境中則是安裝Docker Desktop並且切換到使用Windows Container，這樣一來就可以在Windows環境執行Windows Container，Linux子系統中執行Linux Container了。</p>



<p><em>註：WSL是Windows 10的功能，版本又分為Version 1(WSL)與Version 2(WSL2)，WSL2只能在Windows 10 2004版(組建19041以上)執行。(<a href="https://docs.microsoft.com/zh-tw/windows/wsl/" target="_blank" aria-label="undefined (opens in a new tab)" rel="noreferrer noopener nofollow">參考官方wsl文件說明</a>)</em></p>



<p>在開始之前，先輸入<code class="language-bash">wsl -l -v</code>查看一下系統內安裝的Linux子系統是執行在哪一個版本(1或2)。<br>在Linux子系統中要執行原生的Docker，只能在Version 2的版本(WSL2)。</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="657" height="229" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/3955add2-image.png?resize=657%2C229&#038;ssl=1" alt="" class="wp-image-71" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/3955add2-image.png?w=657&amp;ssl=1 657w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/3955add2-image.png?resize=300%2C105&amp;ssl=1 300w" sizes="auto, (max-width: 657px) 100vw, 657px" /><figcaption>使用<code>wsl -l -v</code>查看系統中 Linux子系統執行的版本</figcaption></figure>



<span id="more-70"></span>



<p>先輸入<code class="language-bash">docker info</code>確認系統中沒有安裝docker。</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="943" height="247" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/7f0c8bfc-image.png?resize=943%2C247&#038;ssl=1" alt="" class="wp-image-72" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/7f0c8bfc-image.png?w=943&amp;ssl=1 943w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/7f0c8bfc-image.png?resize=300%2C79&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/7f0c8bfc-image.png?resize=768%2C201&amp;ssl=1 768w" sizes="auto, (max-width: 943px) 100vw, 943px" /><figcaption>輸入<code>docker info</code>確認系統中確實沒有安裝docker</figcaption></figure>



<p>接著，根據官方的安裝指引將Docker Engine安裝到Linux子系統中：</p>



<ul class="wp-block-list"><li>Ubuntu：<a aria-label="undefined (opens in a new tab)" href="https://docs.docker.com/engine/install/ubuntu/" target="_blank" rel="noreferrer noopener nofollow">https://docs.docker.com/engine/install/ubuntu/</a></li><li>Debian：<a aria-label="undefined (opens in a new tab)" href="https://docs.docker.com/engine/install/debian/" target="_blank" rel="noreferrer noopener nofollow">https://docs.docker.com/engine/install/debian/</a></li><li>CentOS：<a aria-label="undefined (opens in a new tab)" href="https://docs.docker.com/engine/install/centos/" target="_blank" rel="noreferrer noopener nofollow">https://docs.docker.com/engine/install/centos/</a></li><li>Fedora：<a href="https://docs.docker.com/engine/install/fedora/" target="_blank" aria-label="undefined (opens in a new tab)" rel="noreferrer noopener nofollow">https://docs.docker.com/engine/install/fedora/</a></li></ul>



<h4 class="wp-block-heading">更新系統中的套件</h4>



<p>先輸入<code class="language-bash">sudo apt-get update</code>更新系統中的套件。</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="906" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/4afb5ecb-image-1024x906.png?resize=1024%2C906&#038;ssl=1" alt="" class="wp-image-73" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/4afb5ecb-image.png?resize=1024%2C906&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/4afb5ecb-image.png?resize=300%2C265&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/4afb5ecb-image.png?resize=768%2C680&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/4afb5ecb-image.png?w=1269&amp;ssl=1 1269w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /><figcaption>先輸入<code>sudo apt-get</code>更新系統中的套件</figcaption></figure>



<h4 class="wp-block-heading">安裝需要的套件</h4>



<p>再輸入下列指令安裝docker需要的其它套件：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-bash">sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common</code></pre>



<p><em>註：「\」符號是Linux指令的換行連接符號，用來將較長的指令換行輸入，實際上在系統中仍然是組合成一整行的指令，因此也可以將上面的指令直接輸入成一行。(記得拿掉「\」符號)</em></p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="827" height="175" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/5ee778d6-image.png?resize=827%2C175&#038;ssl=1" alt="" class="wp-image-74" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/5ee778d6-image.png?w=827&amp;ssl=1 827w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/5ee778d6-image.png?resize=300%2C63&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/5ee778d6-image.png?resize=768%2C163&amp;ssl=1 768w" sizes="auto, (max-width: 827px) 100vw, 827px" /><figcaption>安裝docker前需要安裝的套件</figcaption></figure>



<p>安裝上述幾個套件的過程中會碰到詢問y/n的問題，通常是告知會使用多少磁碟空間，輸入y繼續安裝就行了。</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="749" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/6958cf39-image-1024x749.png?resize=1024%2C749&#038;ssl=1" alt="" class="wp-image-75" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/6958cf39-image.png?resize=1024%2C749&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/6958cf39-image.png?resize=300%2C220&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/6958cf39-image.png?resize=768%2C562&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/6958cf39-image.png?w=1499&amp;ssl=1 1499w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /><figcaption>安裝apt-transport-https、ca-certificates、curl、gnupg-agent、software-properties-common套件與其相依的套件</figcaption></figure>



<h4 class="wp-block-heading">取得GPG Key</h4>



<p>接下來取得docker的GPG key：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-bash">curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -</code></pre>



<p><em>註：「|」符號是Linux中的命令管線符號，用來將前一個命令執行的結果送到下一個命令的輸入。</em></p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="39" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/88c5a78e-image-1024x39.png?resize=1024%2C39&#038;ssl=1" alt="" class="wp-image-76" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/88c5a78e-image.png?resize=1024%2C39&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/88c5a78e-image.png?resize=300%2C11&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/88c5a78e-image.png?resize=768%2C29&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/88c5a78e-image.png?w=1505&amp;ssl=1 1505w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /><figcaption>透過curl取得docker的GPG key</figcaption></figure>



<p>接下來<strong>這一步可以跳過</strong>，只是用來驗證剛才取得的GPG key是否正確。</p>



<pre class="wp-block-prismatic-blocks"><code class="language-bash">sudo apt-key fingerprint 0EBFCD88</code></pre>



<p>輸入完之後會看到下面<code>9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88</code>的內容。</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="947" height="145" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/eb1ab3ee-image.png?resize=947%2C145&#038;ssl=1" alt="" class="wp-image-77" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/eb1ab3ee-image.png?w=947&amp;ssl=1 947w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/eb1ab3ee-image.png?resize=300%2C46&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/eb1ab3ee-image.png?resize=768%2C118&amp;ssl=1 768w" sizes="auto, (max-width: 947px) 100vw, 947px" /><figcaption>驗證取得的GPG key</figcaption></figure>



<h4 class="wp-block-heading">加入套件存放庫(Repository)</h4>



<p>接下來將docker官方的套件存放庫(repository)加入到Linux子系統中，這樣才能夠安裝docker engine：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-bash">sudo add-apt-repository \
&quot;deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable&quot;</code></pre>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="319" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/a5c90088-image-1024x319.png?resize=1024%2C319&#038;ssl=1" alt="" class="wp-image-80" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/a5c90088-image.png?resize=1024%2C319&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/a5c90088-image.png?resize=300%2C94&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/a5c90088-image.png?resize=768%2C240&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/a5c90088-image.png?w=1087&amp;ssl=1 1087w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /><figcaption>將docker的套件庫加入到系統中</figcaption></figure>



<p><em>小提示：如果你發現你的網址輸入錯誤．也就是在輸入指令之後下面的訊息有顯示Err的字樣，那麼可以編輯套件庫清單的設定檔，將錯誤的網址修正或移除重新加入。</em>(<a aria-label="undefined (opens in a new tab)" href="https://unix.stackexchange.com/questions/219341/how-to-apt-delete-repository" target="_blank" rel="noreferrer noopener nofollow">參考來源</a>)</p>



<pre class="wp-block-prismatic-blocks"><code class="language-bash">sudo nano /etc/apt/sources.list</code></pre>



<h4 class="wp-block-heading">安裝Docker Engine</h4>



<p>輸入下面兩行指令安裝Docker Engine，第一行是更新套件指令，第二行則是安裝Docker的指令</p>



<pre class="wp-block-prismatic-blocks"><code class="language-bash">sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io</code></pre>



<p>安裝過程中會有個磁碟空間耗用的問題，輸入y就能繼續。<br>接著會碰到下面的問題，是詢問grub-pc套件要裝在哪一個磁碟分割：</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="423" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/f688bcfe-image-1024x423.png?resize=1024%2C423&#038;ssl=1" alt="" class="wp-image-81" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/f688bcfe-image.png?resize=1024%2C423&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/f688bcfe-image.png?resize=300%2C124&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/f688bcfe-image.png?resize=768%2C317&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/f688bcfe-image.png?resize=1536%2C635&amp;ssl=1 1536w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/f688bcfe-image.png?w=1619&amp;ssl=1 1619w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /><figcaption>選擇grub-pc套件要安裝的目標</figcaption></figure>



<p>grub-pc應該是一個啟動管理的套件，不太確定是上面哪個套件相依的需求，以上圖來看，Linux子系統抓到三個(實際上只有sda、sdb兩個)容量大小相同的項目。<br>透過空白鍵可以選擇/取消，想知道sda、sdb分別是什麼的話，可以再另外開一個cmd或powershell視窗，輸入wsl透過另外一個terminal進入查看：</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="867" height="565" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/b287d344-image.png?resize=867%2C565&#038;ssl=1" alt="" class="wp-image-82" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/b287d344-image.png?w=867&amp;ssl=1 867w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/b287d344-image.png?resize=300%2C196&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/b287d344-image.png?resize=768%2C500&amp;ssl=1 768w" sizes="auto, (max-width: 867px) 100vw, 867px" /><figcaption>透過<code>df -h</code>、<code>lsblk</code>、<code>sudo blkid</code>指令查看磁碟資訊</figcaption></figure>



<p>從上面可以發現sda、sdb其實是一樣的，只是檔案系統實際上掛載在sdb項目，所以選擇sda、sdb也許都可以，不過實際上選擇第一個sda項目的時候，安裝到一半會出現下面的錯誤訊息：</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="299" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/71a5ac8f-image-1024x299.png?resize=1024%2C299&#038;ssl=1" alt="" class="wp-image-83" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/71a5ac8f-image.png?resize=1024%2C299&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/71a5ac8f-image.png?resize=300%2C88&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/71a5ac8f-image.png?resize=768%2C224&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/71a5ac8f-image.png?w=1223&amp;ssl=1 1223w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /><figcaption>無法安裝在sda</figcaption></figure>



<p>既然無法安裝在sda，那麼就不要霸王硬上弓硬是要安裝在sda，所以選擇No之後，回到上面的選單項目，將sda取消，選擇第二個sdb試試。<br>沒有再出現上面無法安裝詢問是否要霸王硬上弓的問題，順利安裝完成。</p>



<p>輸入<code>docker info</code>查看安裝的結果，只顯示client資訊，server資訊則是無法連線，其實這只是docker service沒有啟動的關係。</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="152" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/5682d84c-image-1024x152.png?resize=1024%2C152&#038;ssl=1" alt="" class="wp-image-84" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/5682d84c-image.png?resize=1024%2C152&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/5682d84c-image.png?resize=300%2C45&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/5682d84c-image.png?resize=768%2C114&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/5682d84c-image.png?w=1351&amp;ssl=1 1351w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>輸入<code>sudo service docker start</code>啟動docker服務，再次輸入<code>docker info</code>查看docker的資訊卻發現…</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="149" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/afb2eb9d-image-1024x149.png?resize=1024%2C149&#038;ssl=1" alt="" class="wp-image-85" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/afb2eb9d-image.png?resize=1024%2C149&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/afb2eb9d-image.png?resize=300%2C44&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/afb2eb9d-image.png?resize=768%2C112&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/afb2eb9d-image.png?resize=1536%2C224&amp;ssl=1 1536w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/afb2eb9d-image.png?w=1559&amp;ssl=1 1559w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>看起來和上面那張截圖滿像的，不過最後有個關鍵字是「permission denied」。<br>既然如此，那使用sudo執行看看呢？(<code>sudo docker info</code>)</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="868" height="1024" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/9cd58616-image-868x1024.png?resize=868%2C1024&#038;ssl=1" alt="" class="wp-image-87" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/9cd58616-image.png?resize=868%2C1024&amp;ssl=1 868w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/9cd58616-image.png?resize=254%2C300&amp;ssl=1 254w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/9cd58616-image.png?resize=768%2C906&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/9cd58616-image.png?w=1123&amp;ssl=1 1123w" sizes="auto, (max-width: 868px) 100vw, 868px" /></figure>



<p>Bingo，能夠正常查看到docker的資訊了！</p>



<p>不過這樣每次要執行docker相關的指令都要輸入sudo有點麻煩，所以接著可以利用下面的指令，將docker加入特定的用戶群：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-bash">sudo usermod -aG docker 帳號</code></pre>



<p>輸入完上面的指令之後，如果直接再次輸入docker info想要試試是否有效，會得到令人失望的結果。主要是因為當下使用的terminal沒有登出再重新登入取得最新的資訊，只要退出terminal之後再重新執行就可以正常了。</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="669" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/28de246d-image-1024x669.png?resize=1024%2C669&#038;ssl=1" alt="" class="wp-image-88" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/28de246d-image.png?resize=1024%2C669&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/28de246d-image.png?resize=300%2C196&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/28de246d-image.png?resize=768%2C502&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/28de246d-image.png?resize=1536%2C1003&amp;ssl=1 1536w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/28de246d-image.png?w=1557&amp;ssl=1 1557w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /><figcaption>輸入完<code>sudo usermod -aG docker &lt;em&gt;帳號&lt;/em&gt;</code>，需要離開wsl再重新進入才會生效</figcaption></figure>



<p>OK，這下子終於將docker順利安裝在WSL2裡面啦~</p>



<p>從下面的截圖也可以發現，在wsl裡面查看docker info和windows底下查看docker info的差異，證明兩個docker環境是彼此分開獨立的喔！</p>



<p>PS.Windows環境安裝Docker Desktop並且切換到Windows Container模式了，這樣一來就可以在一台電腦中同時執行Linux Container與Windows Container了。</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="787" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/d696fac4-image-1024x787.png?resize=1024%2C787&#038;ssl=1" alt="" class="wp-image-90" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/d696fac4-image.png?resize=1024%2C787&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/d696fac4-image.png?resize=300%2C230&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/d696fac4-image.png?resize=768%2C590&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/d696fac4-image.png?resize=1536%2C1180&amp;ssl=1 1536w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/08/d696fac4-image.png?w=1725&amp;ssl=1 1725w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /><figcaption>同一台電腦中同時執行Windows Docker與Linux Docker環境</figcaption></figure>
<p>這篇文章 <a href="https://tech.uccu.website/how-to-install-docker-on-wsl2.html">同一台電腦可執行Windows與Linux Container？在WSL2安裝原生Docker環境</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tech.uccu.website/how-to-install-docker-on-wsl2.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">70</post-id>	</item>
		<item>
		<title>Windows 10 Home版本安裝Windows Sandbox沙箱功能</title>
		<link>https://tech.uccu.website/windows-10-home-install-sandbox-feature.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=windows-10-home-install-sandbox-feature</link>
					<comments>https://tech.uccu.website/windows-10-home-install-sandbox-feature.html#respond</comments>
		
		<dc:creator><![CDATA[鳴人]]></dc:creator>
		<pubDate>Tue, 26 May 2020 10:35:34 +0000</pubDate>
				<category><![CDATA[Windows]]></category>
		<category><![CDATA[Sandbox]]></category>
		<category><![CDATA[windows 10 home]]></category>
		<guid isPermaLink="false">https://tech.uccu.website/?p=37</guid>

					<description><![CDATA[<p>跟上一篇「Windows 10 Home家用版安裝Hyper-V與Docker」類似，Windows 10 H ... <a title="Windows 10 Home版本安裝Windows Sandbox沙箱功能" class="read-more" href="https://tech.uccu.website/windows-10-home-install-sandbox-feature.html" aria-label="Read more about Windows 10 Home版本安裝Windows Sandbox沙箱功能">閱讀全文</a></p>
<p>這篇文章 <a href="https://tech.uccu.website/windows-10-home-install-sandbox-feature.html">Windows 10 Home版本安裝Windows Sandbox沙箱功能</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>跟上一篇「<a rel="noreferrer noopener" href="https://tech.uccu.website/windows10-home-install-hyperv-docker.html" target="_blank">Windows 10 Home家用版安裝Hyper-V與Docker</a>」類似，Windows 10 Home的版本也沒有Windows Sandbox沙箱功能，這個Windows Sandbox沙箱功能是基於Hyper-V功能的Container，不過卻不會有體積龐大或是資源浪費的問題。</p>



<p>根據<a href="https://docs.microsoft.com/zh-tw/windows/security/threat-protection/windows-sandbox/windows-sandbox-architecture">微軟的文件說明</a>，Windows Sandbox沙箱功能在未安裝之前是一個大小約為30MB的壓縮封裝，即使安裝之後解壓縮大小也只會佔據大約500MB左右的磁碟空間。(當然你在沙箱裡面下載大檔案或是其它操作所產生的檔案是另外一回事)</p>



<span id="more-37"></span>



<h3 class="wp-block-heading"><strong>動態基底影像</strong></h3>



<p>它所使用的技術和容器化技術相同，也就是大部份的功能是和核心OS共用的，只有少部份作業系統功能或檔案無法共用，所以在執行沙箱環境的時候，就會使用核心OS共用的功能，也因為Host本身就已經是開機執行的狀態，所以很多功能已經是Ready狀態，讓沙箱環境的載入速度非常快，時間非常短就能夠產生出一個可用的沙箱環境。</p>



<figure class="wp-block-image"><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/docs.microsoft.com/zh-tw/windows/security/threat-protection/windows-sandbox/images/1-dynamic-host.png?w=1125&#038;ssl=1" alt="圖表會比較檔案的動態影像與主機檔案系統的連結。"/><figcaption>沙箱環境使用Host環境的共用檔案是透過連結(Link)的方式存取</figcaption></figure>



<h3 class="wp-block-heading"><strong>記憶體共用</strong></h3>



<p>Windows Sandbox沙箱環境在記憶體耗用量的部份也比傳統VM來得少，如果同樣在VM中安裝Windows 10，兩個OS載入相同功能的系統檔案所需要耗用的記憶體空間是獨立的，但是在Sandbox沙箱環境中是透過「直接對應(Direct Map)」的方式存取相同的記憶體位置。</p>



<figure class="wp-block-image"><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/docs.microsoft.com/zh-tw/windows/security/threat-protection/windows-sandbox/images/3-memory-sharing.png?w=1125&#038;ssl=1" alt="圖表會比較 Windows 沙箱中的記憶體佔用空間與傳統 VM。"/><figcaption>沙箱環境使用到相同功能的記憶體環境也是可以共用記憶體，可以有效節省記憶體的使用量</figcaption></figure>



<h3 class="wp-block-heading"><strong>GPU虛擬化</strong></h3>



<p>在Sandbox沙箱環境裡面處理圖形轉譯的部份也是很流暢的，因為在Sandbox沙箱環境執行的程式可以透過Windows顯示驅動程式模型(WDDM)來直接存取GPU的資源。</p>



<p>所以在沙箱環境裡面使用各種瀏覽器上網看高畫質的影片或是玩遊戲也不會有畫面不順暢的問題。<span style="text-decoration: underline;">當然，Sandbox沙箱功能的音效也是沒問題的</span>。<br>(但前提是你的顯示卡配備要夠好)</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/docs.microsoft.com/zh-tw/windows/security/threat-protection/windows-sandbox/images/5-wddm-gpu-virtualization.png?w=1125&#038;ssl=1" alt=""/><figcaption>Sandbox沙箱環境中的應用程式可以直接存取GPU資源</figcaption></figure>



<h3 class="wp-block-heading"><strong>可以拿來做什麼用？</strong></h3>



<p>雖然平常不是想要在沙箱環境裡面測試什麼有問題的軟體或程式，也不是要搞駭客任務，但是Sandbox沙箱環境仍然是很適合拿來做一些可能有風險的動作，例如：</p>



<ul class="wp-block-list"><li>從IM軟體(ex:Line)收到不知道是什麼的網址，想打開來看看是什麼東西卻又怕電腦中毒。</li><li>網路上搜尋一些東西不想被記錄，或是上一些不常去的網站抓一些東西(免費軟體、共享軟體)怕有問題，可以在沙箱環境裝防毒軟體先試試。</li><li>其它各類不想影響主要系統的使用情境。</li></ul>



<h2 class="wp-block-heading">如何在Windows 10 Home家用版安裝？</h2>



<p>好了，上面簡單介紹了Windows Sandbox沙箱功能之後，是不是覺得Sandbox沙箱功能好像還不錯用啊？</p>



<h3 class="wp-block-heading"><strong>安裝條件</strong></h3>



<p>不過Sandbox沙箱功能預設的安裝條件是需要Windows 10 Pro或是Enterprise的版本才能安裝(OS組建必須在18305以上)，除此之外還有下面的基本需求：</p>



<ol class="wp-block-list"><li>AMD64的架構(就是64位元的一般電腦，ARM系列的不行)</li><li>主機板與CPU必須支援虛擬化技術，並且在主機板有將虛擬化功能打開</li><li>至少4GB的記憶體(建議8GB以上)</li><li>至少1GB以上的磁碟可用空間(建議使用SSD)</li><li>至少是雙核心以上的CPU(建議4核心以上)</li><li>如果是在VM中執行的Windows 10，需要開啟巢狀虛擬化功能(可用下列PowerShell指令)<br>Set-VMProcess -VMName <em><span class="has-inline-color has-vivid-green-cyan-color">&lt;VM名稱&gt;</span></em> -ExposeVirtualizationExtensions $true</li></ol>



<h3 class="wp-block-heading"><strong>安裝步驟</strong></h3>



<h5 class="wp-block-heading">步驟1：滑鼠右鍵點擊左下角的Windows圖案開始按鈕，選擇「應用程式與功能(F)」。</h5>



<div class="wp-block-image"><figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="317" height="580" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/0eb8d526-winiconrightclickquickmenu-appandfeatures.png?resize=317%2C580&#038;ssl=1" alt="" class="wp-image-41" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/0eb8d526-winiconrightclickquickmenu-appandfeatures.png?w=317&amp;ssl=1 317w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/0eb8d526-winiconrightclickquickmenu-appandfeatures.png?resize=164%2C300&amp;ssl=1 164w" sizes="auto, (max-width: 317px) 100vw, 317px" /><figcaption>透過「應用程式與功能(F)」這個快速選單叫出系統設定頁</figcaption></figure></div>



<h5 class="wp-block-heading">步驟2：點擊右邊「相關設定」底下的「程式和功能」。</h5>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="864" height="353" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/d63ae041-windowssettings-appandfeatures.png?resize=864%2C353&#038;ssl=1" alt="" class="wp-image-42" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/d63ae041-windowssettings-appandfeatures.png?w=864&amp;ssl=1 864w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/d63ae041-windowssettings-appandfeatures.png?resize=300%2C123&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/d63ae041-windowssettings-appandfeatures.png?resize=768%2C314&amp;ssl=1 768w" sizes="auto, (max-width: 864px) 100vw, 864px" /><figcaption>從「程式和功能」開啟控制台的頁面</figcaption></figure>



<h5 class="wp-block-heading">步驟3：點擊控制台左邊的「開啟或關閉 Windows 功能」。</h5>



<div class="wp-block-image"><figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="406" height="203" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/ad6b8045-controlpanel-enableordisablewindowsfeature.png?resize=406%2C203&#038;ssl=1" alt="" class="wp-image-43" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/ad6b8045-controlpanel-enableordisablewindowsfeature.png?w=406&amp;ssl=1 406w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/ad6b8045-controlpanel-enableordisablewindowsfeature.png?resize=300%2C150&amp;ssl=1 300w" sizes="auto, (max-width: 406px) 100vw, 406px" /><figcaption>從「開啟或關閉 Windows 功能」叫出Windows功能清單</figcaption></figure></div>



<h5 class="wp-block-heading">步驟4：在Windows功能清單中找到「Windows 沙箱」或「Windows Sandbox」功能後打勾。</h5>



<div class="wp-block-image"><figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="385" height="404" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/400f3ca1-windowsfeaturelist-windowssandbox.png?resize=385%2C404&#038;ssl=1" alt="" class="wp-image-44" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/400f3ca1-windowsfeaturelist-windowssandbox.png?w=385&amp;ssl=1 385w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/400f3ca1-windowsfeaturelist-windowssandbox.png?resize=286%2C300&amp;ssl=1 286w" sizes="auto, (max-width: 385px) 100vw, 385px" /></figure></div>



<h3 class="wp-block-heading">找不到「Windows 沙箱」或「Windows Sandbox」功能怎麼辦？</h3>



<p>別著急，Windows 10 Home家用版在沒有使用密技之前，找不到這個功能是很正常的，請將下面的指令碼複製下來貼到記事本，並且將檔案儲存為副檔名.bat的檔案(主檔名隨意)，再透過滑鼠右鍵選擇「以系統管理員身份執行」開啟。<br>批次指令執行完後輸入Y並且按下Enter讓<strong><span class="has-inline-color has-vivid-red-color">電腦重新開機</span></strong>，之後再跑一次上面的步驟應該就可以找得到了。</p>



<pre class="wp-block-prismatic-blocks"><code class="language-batch">@echo off

echo Checking for permissions
&gt;nul 2&gt;&amp;1 &quot;%SYSTEMROOT%\system32\cacls.exe&quot; &quot;%SYSTEMROOT%\system32\config\system&quot;

echo Permission check result: %errorlevel%

REM --&gt; If error flag set, we do not have admin.
if &#039;%errorlevel%&#039; NEQ &#039;0&#039; (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
echo Set UAC = CreateObject^(&quot;Shell.Application&quot;^) &gt; &quot;%temp%\getadmin.vbs&quot;
echo UAC.ShellExecute &quot;%~s0&quot;, &quot;&quot;, &quot;&quot;, &quot;runas&quot;, 1 &gt;&gt; &quot;%temp%\getadmin.vbs&quot;

echo Running created temporary &quot;%temp%\getadmin.vbs&quot;
timeout /T 2
&quot;%temp%\getadmin.vbs&quot;
exit /B

:gotAdmin
if exist &quot;%temp%\getadmin.vbs&quot; ( del &quot;%temp%\getadmin.vbs&quot; )
pushd &quot;%CD%&quot;
CD /D &quot;%~dp0&quot; 

echo Batch was successfully started with admin privileges
echo .
cls
Title Sandbox Installer

pushd &quot;%~dp0&quot;

dir /b %SystemRoot%\servicing\Packages\*Containers*.mum &gt;sandbox.txt

for /f %%i in (&#039;findstr /i . sandbox.txt 2^&gt;nul&#039;) do dism /online /norestart /add-package:&quot;%SystemRoot%\servicing\Packages\%%i&quot;

del sandbox.txt

Dism /online /enable-feature /featurename:Containers-DisposableClientVM /LimitAccess /ALL

pause</code></pre>



<h3 class="wp-block-heading"><strong>參考來源</strong></h3>



<ul class="wp-block-list"><li><a href="https://www.tenforums.com/tutorials/131437-enable-windows-sandbox-feature-windows-10-home-edition.html">How to Enable Windows Sandbox Feature in Windows 10 Home Edition</a></li><li><a href="https://www.thomasmaurer.ch/2019/05/how-to-configure-windows-sandbox/">HOW TO CONFIGURE WINDOWS SANDBOX</a></li><li><a href="https://docs.microsoft.com/zh-tw/windows/security/threat-protection/windows-sandbox/windows-sandbox-architecture">Windows 沙箱架構</a></li><li><a href="https://techcommunity.microsoft.com/t5/windows-kernel-internals/windows-sandbox/ba-p/301849">Windows Sandbox</a></li></ul>
<p>這篇文章 <a href="https://tech.uccu.website/windows-10-home-install-sandbox-feature.html">Windows 10 Home版本安裝Windows Sandbox沙箱功能</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tech.uccu.website/windows-10-home-install-sandbox-feature.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">37</post-id>	</item>
		<item>
		<title>Windows 10 Home家用版安裝Hyper-V與Docker</title>
		<link>https://tech.uccu.website/windows10-home-install-hyperv-docker.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=windows10-home-install-hyperv-docker</link>
					<comments>https://tech.uccu.website/windows10-home-install-hyperv-docker.html#respond</comments>
		
		<dc:creator><![CDATA[鳴人]]></dc:creator>
		<pubDate>Tue, 19 May 2020 08:05:08 +0000</pubDate>
				<category><![CDATA[Docker]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[container]]></category>
		<category><![CDATA[docker]]></category>
		<category><![CDATA[hyper-v]]></category>
		<category><![CDATA[windows 10 home]]></category>
		<category><![CDATA[wsl]]></category>
		<guid isPermaLink="false">https://tech.uccu.website/?p=30</guid>

					<description><![CDATA[<p>公司一開始配的電腦是Acer的套裝電腦，裡面搭配的是Windows 10 Home版，作為一個開發者，Home ... <a title="Windows 10 Home家用版安裝Hyper-V與Docker" class="read-more" href="https://tech.uccu.website/windows10-home-install-hyperv-docker.html" aria-label="Read more about Windows 10 Home家用版安裝Hyper-V與Docker">閱讀全文</a></p>
<p>這篇文章 <a href="https://tech.uccu.website/windows10-home-install-hyperv-docker.html">Windows 10 Home家用版安裝Hyper-V與Docker</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>公司一開始配的電腦是Acer的套裝電腦，裡面搭配的是Windows 10 Home版，作為一個開發者，Home版本的功能實在是不太符合需求，因為預設應該是沒有Hyper-V功能，如果要安裝Docker的話，也只能安裝Client工具…</p>



<p>不太想要動到OS重灌這一招，想到之前似乎有試過(看到)在Windows 10 Home版安裝Hyper-V與WSL，不過因為很久沒有寫部落格記錄，有些問題解決之後就沒有記錄，所以趁著這次解決這個問題並且將部落格從幾次亂搞之後最近放到Google Cloud Run上面執行，一併將這些操作記錄下來。</p>



<span id="more-30"></span>



<p class="has-text-align-left">首先，Windows 10 Home版打開控制台「程式和功能」中的「Windows 功能」應該不會有「Hyper-V平台」、「適用於 Linux 的 Windows 子系統」兩個選項。</p>



<p class="has-text-align-center"><img data-recalc-dims="1" loading="lazy" decoding="async" width="600" height="400" class="wp-image-33" style="width: 600px;" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/b94afa4b-windows10-home-windows-features.png?resize=600%2C400&#038;ssl=1" alt="Windows 10 Home家用版Windows功能" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/b94afa4b-windows10-home-windows-features.png?w=731&amp;ssl=1 731w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/b94afa4b-windows10-home-windows-features.png?resize=300%2C200&amp;ssl=1 300w" sizes="auto, (max-width: 600px) 100vw, 600px" /></p>



<p>從<a href="https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/hyper-v-requirements">Windows 10 Hyper-V System Requirements</a>的頁面說明中也有下面這段，明確表明Windows 10 Home的版本不能安裝。</p>



<p>The Hyper-V role&nbsp;<strong>cannot</strong>&nbsp;be installed on:</p>



<ul class="wp-block-list"><li>Windows 10 Home</li><li>Windows 10 Mobile</li><li>Windows 10 Mobile Enterprise</li></ul>



<p>不過這個其實有秘技可以讓Hyper-V安裝在Windows 10 Home，只需要將下面這段指令貼在記事本上，並且以.bat的副檔名儲存後透過管理員權限執行即可。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>pushd &#8220;%~dp0&#8221;<br>dir /b %SystemRoot%\servicing\Packages*Hyper-V*.mum &gt;hyper-v.txt<br>for /f %%i in (&#8216;findstr /i . hyper-v.txt 2^&gt;nul&#8217;) do dism /online /norestart /add-package:&#8221;%SystemRoot%\servicing\Packages\%%i&#8221;<br>del hyper-v.txt<br>Dism /online /enable-feature /featurename:Microsoft-Hyper-V -All /LimitAccess /ALL<br>pause</p></blockquote>



<p>如果也想要安裝Container功能的話，同樣將下面這段指令以.bat儲存後透過管理員權限執行。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>pushd &#8220;%~dp0&#8221;<br>dir /b %SystemRoot%\servicing\Packages*containers*.mum &gt;containers.txt<br>for /f %%i in (&#8216;findstr /i . containers.txt 2^&gt;nul&#8217;) do dism /online /norestart /add-package:&#8221;%SystemRoot%\servicing\Packages\%%i&#8221;<br>del containers.txt<br>Dism /online /enable-feature /featurename:Containers -All /LimitAccess /ALL<br>pause</p></blockquote>



<p>執行完之後重新開機應該就可以找到Hyper-V的功能了，不過到這裡為止，仍然無法安裝Docker。<br>或者是說，如果不是<a href="https://docs.docker.com/docker-for-windows/install-windows-home/">Windows 10 Home搭配Docker使用WSL2(Windows Subsystem for Linux, WSL)</a>，那麼就無法使用Docker。</p>



<p>再更精確一點的說法，就是<a href="https://docs.docker.com/docker-for-windows/install/">Docker需要Hyper-V與Container的功能</a>，如果是Windows Home安裝WSL2再加上Docker，那麼就是在WSL2裡面執行Container，跑的是Linux，如果只需要執行Linux的Container是OK的。</p>



<p>不過若是需要在Windows Container和Linux Container之間切換的話，Windows 10 Home版本一般來說是無法達成，而且Docker搭配WSL2跑Linux Container若是使用到Docker-Compose的話，可能還會有一點點問題…</p>



<p>所以為了在Windows 10 Home的版本正常執行Docker(Hyper-V都透過秘技安裝了，應該還有秘技可以讓Docker正常在Windows 10 Home版本安裝與執行吧！)，所以我繼續在網路上搜尋可能的秘技，想要解決在Windows 10 Home版本使用Docker的問題。</p>



<p>這次的秘技不需要再執行指令安裝什麼東西，需要的是執行regedit編輯Windows登錄檔，透過將「HKLM\Software\Microsoft\Windows NT\CurrentVersion」底下的EditionID從「Core」更改為「Professional」，改完之後不需要重新開機，就可以執行最新版本的Docker安裝檔。</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="963" height="680" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/7f9b43ce-changewindowseditionid_windowsregistry.png?resize=963%2C680&#038;ssl=1" alt="" class="wp-image-34" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/7f9b43ce-changewindowseditionid_windowsregistry.png?w=963&amp;ssl=1 963w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/7f9b43ce-changewindowseditionid_windowsregistry.png?resize=300%2C212&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/7f9b43ce-changewindowseditionid_windowsregistry.png?resize=768%2C542&amp;ssl=1 768w" sizes="auto, (max-width: 963px) 100vw, 963px" /><figcaption>將EditionID從「Core」更改為「Professional」，改完之後不需要重新開機，就可以執行最新版本的Docker安裝檔</figcaption></figure>



<p>上面的這個修改Windows登錄檔的位置要記下來，或者是寫成一個指令檔。<br>因為Windows過一段時間(不知多久)會自己將更改的值改回「Core」(如果你改完忘了改回來)，尤其是Windows Update完之後。</p>



<p>這會有什麼影響呢？</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="336" height="351" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/cdf1ffcb-dockerswitchcontainerneedtochangewindowsregistry.png?resize=336%2C351&#038;ssl=1" alt="" class="wp-image-35" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/cdf1ffcb-dockerswitchcontainerneedtochangewindowsregistry.png?w=336&amp;ssl=1 336w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/05/cdf1ffcb-dockerswitchcontainerneedtochangewindowsregistry.png?resize=287%2C300&amp;ssl=1 287w" sizes="auto, (max-width: 336px) 100vw, 336px" /><figcaption>EditionID為「Core」的情況下無法切換Container</figcaption></figure></div>



<p>會影響的就是Docker切換Windows Container與更新Docker版本，如果Docker發現新的版本，但是當你要執行更新安裝的時候發現有問題，可以試著將EditionID再改為「Professional」，之後應該就可以更新了。<br>註：要透過Docker結合WSL2執行Linux Container必須是切換到Linux Container模式才行。<br>(不過也可以選擇不要在Windows下操作Docker跑Linux Container，直接在WSL2裡面安裝獨立的Docker)參考來源：</p>



<p><span>參考來源</span></p>



<ul class="wp-block-list"><li><a href="https://forums.docker.com/t/installing-docker-on-windows-10-home/11722">Installing Docker on Windows 10 Home</a></li></ul>
<p>這篇文章 <a href="https://tech.uccu.website/windows10-home-install-hyperv-docker.html">Windows 10 Home家用版安裝Hyper-V與Docker</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tech.uccu.website/windows10-home-install-hyperv-docker.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">30</post-id>	</item>
	</channel>
</rss>
