<?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>無伺服器 - 東東 GCP 教學 - GCP 實戰講師</title>
	<atom:link href="https://dongdonggcp.com/category/%E7%84%A1%E4%BC%BA%E6%9C%8D%E5%99%A8/feed/" rel="self" type="application/rss+xml" />
	<link>https://dongdonggcp.com</link>
	<description>助你考取證照，轉職成功</description>
	<lastBuildDate>Sun, 20 Jul 2025 08:39:07 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://dongdonggcp.com/wp-content/uploads/2025/04/cropped-340838097_121391010914395_5443948698124160121_n-32x32.jpg</url>
	<title>無伺服器 - 東東 GCP 教學 - GCP 實戰講師</title>
	<link>https://dongdonggcp.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">243235092</site>	<item>
		<title>儲存容器映像檔的 Artifact Registry 簡介與上傳 Docker 映像檔教學</title>
		<link>https://dongdonggcp.com/2025/07/20/artifact-registry-tutorials/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=artifact-registry-tutorials</link>
					<comments>https://dongdonggcp.com/2025/07/20/artifact-registry-tutorials/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 20 Jul 2025 08:18:56 +0000</pubDate>
				<category><![CDATA[無伺服器]]></category>
		<category><![CDATA[Artifact Registry]]></category>
		<category><![CDATA[Container]]></category>
		<category><![CDATA[Docker]]></category>
		<category><![CDATA[Docker Image]]></category>
		<category><![CDATA[Nodejs]]></category>
		<guid isPermaLink="false">https://dongdonggcp.com/?p=11173</guid>

					<description><![CDATA[<p>Artifact Registry 是 Google Cloud 中專門的容器映像檔儲存庫，除了 Docker 映像外，還支援 Maven Artifacts（Java）、Npm Packages、Python Packages、Helm Charts 等等。</p>
<p>The post <a href="https://dongdonggcp.com/2025/07/20/artifact-registry-tutorials/">儲存容器映像檔的 Artifact Registry 簡介與上傳 Docker 映像檔教學</a> first appeared on <a href="https://dongdonggcp.com">東東 GCP 教學 - GCP 實戰講師</a>.</p>]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">Artifact Registry 簡介</h2>



<p class="wp-block-paragraph"><a href="https://www.googleadservices.com/pagead/aclk?sa=L&amp;ai=DChsSEwirsLTv_8qOAxXG2kwCHfITEJUYACICCAEQABoCdG0&amp;ae=2&amp;aspm=1&amp;co=1&amp;ase=2&amp;gclid=CjwKCAjwp_LDBhBCEiwAK7FnkhBSGGOAMvfHjawzthiFks7mErP8ixTosby2sEBJ0rz1lMGqoZSP2hoCfmAQAvD_BwE&amp;ohost=www.google.com&amp;cid=CAESV-D27f6HJLfY8fRQo7vyEJc-amuEDVvDvj7wVTm5UcHc2tc7-ZWroRb6nEiYHrpBHGo0vZXo4tcWYIOvM2iBxdUY17ArpWhXU9tGYG691bXGAlbxMry-5w&amp;category=acrcp_v1_47&amp;sig=AOD64_0OiELFeAQRJRqxZXvfz8ZOPSb8VQ&amp;q&amp;nis=4&amp;adurl&amp;ved=2ahUKEwjL0a_v_8qOAxUch68BHYbNGZkQ0Qx6BAgOEAE" target="_blank" rel="noopener" title="">Artifact Registry </a>是 Google Cloud 中專門的容器映像檔儲存庫，當時在 Artifact Registry 推出之前，己經有 Container Registry，但 Container Registry 只支援 <a href="https://www.docker.com/" target="_blank" rel="noopener" title="">Docker</a> 映像檔，並且功能較為簡單，也沒有細緻的權限管理機制，所以 Google 在 2020 年推出 Artifact Registry，作為 Container Registry 的全面升級。</p>



<p class="wp-block-paragraph">Artifact Registry 除了 Docker 映像外，還支援 Maven Artifacts（Java）、Npm Packages、Python Packages、Helm Charts 等等。</p>



<h2 class="wp-block-heading">建立容器映像檔示範</h2>



<h3 class="wp-block-heading">準備 Nodejs 程式檔案</h3>



<p class="wp-block-paragraph">接著我們就開始打造一個容器映像檔，並上傳到 Artifact Registry 吧！</p>



<p class="wp-block-paragraph">首先我們一樣開啟 Cloud Shell，建立一個資料夾例如 nodejs01，然後先建立第一個檔案 server.js</p>



<pre class="wp-block-code"><code>var http = require('http');
var handleRequest = function(request, response) {
  response.writeHead(200);
  response.end("Hello World from Dongdong Nodejs!!");
}
var www = http.createServer(handleRequest);
www.listen(8080);
</code></pre>



<p class="wp-block-paragraph">然後測試程式能否運作，輸入 node server.js，可以點擊預覽網頁的按鈕，看看網頁是否秀出內容：</p>



<figure class="wp-block-image aligncenter size-large"><img fetchpriority="high" decoding="async" width="1024" height="495" src="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-1-程式執行和預覽-1024x495.png" alt="" class="wp-image-11174" srcset="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-1-程式執行和預覽-1024x495.png 1024w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-1-程式執行和預覽-300x145.png 300w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-1-程式執行和預覽-768x371.png 768w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-1-程式執行和預覽-1536x742.png 1536w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-1-程式執行和預覽.png 1884w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">執行 Nodejs 程式並預覽網頁</figcaption></figure>



<h3 class="wp-block-heading">準備 Dockerfile</h3>



<p class="wp-block-paragraph">如果確認沒問題，再按下 ctrl + C 取回游標，接著準備第二個檔案 Dockerfile 內容如下：</p>



<pre class="wp-block-code"><code>FROM node:6.9.2
EXPOSE 8080
COPY server.js .
CMD node server.js
</code></pre>



<p class="wp-block-paragraph">Dockerfile 是說明檔，告訴 Docker 要如何打包這個 Image，包含這個容器使用什麼程式語言、對外開放哪個 Port、程式的檔案在哪裡、用什麼指令來跑程式等等。</p>



<p class="wp-block-paragraph">現在我們已經準備好兩個檔案如下：</p>



<figure class="wp-block-image aligncenter size-full"><img decoding="async" width="981" height="230" src="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-2-確認目前準備好兩個檔案.png" alt="" class="wp-image-11175" srcset="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-2-確認目前準備好兩個檔案.png 981w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-2-確認目前準備好兩個檔案-300x70.png 300w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-2-確認目前準備好兩個檔案-768x180.png 768w" sizes="(max-width: 981px) 100vw, 981px" /><figcaption class="wp-element-caption">確認目前準備好兩個檔案</figcaption></figure>



<h3 class="wp-block-heading">建立 Artifact Registry 存放區</h3>



<p class="wp-block-paragraph">接著我們回到 Google Cloud Console，去 Artifact Registry 建立一個存放區：</p>



<figure class="wp-block-image aligncenter size-large"><img decoding="async" width="1024" height="547" src="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-3-建立-Artifact-Registry-存放區-1024x547.png" alt="" class="wp-image-11176" srcset="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-3-建立-Artifact-Registry-存放區-1024x547.png 1024w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-3-建立-Artifact-Registry-存放區-300x160.png 300w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-3-建立-Artifact-Registry-存放區-768x410.png 768w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-3-建立-Artifact-Registry-存放區-1536x820.png 1536w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-3-建立-Artifact-Registry-存放區.png 1865w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">建立 Artifact Registry 存放區</figcaption></figure>



<p class="wp-block-paragraph">我們命名「nodejs-1」，格式選擇 Docker，Region 選在 asia-east1，再按下「建立」。 完成之後就會看到它顯示在存放區的列表：</p>



<figure class="wp-block-image aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="653" src="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-4-設定名稱、格式和儲存位置-1024x653.png" alt="" class="wp-image-11177" srcset="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-4-設定名稱、格式和儲存位置-1024x653.png 1024w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-4-設定名稱、格式和儲存位置-300x191.png 300w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-4-設定名稱、格式和儲存位置-768x490.png 768w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-4-設定名稱、格式和儲存位置-1536x980.png 1536w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-4-設定名稱、格式和儲存位置.png 1682w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">設定映像檔存放區的名稱、格式和儲存位置</figcaption></figure>



<h3 class="wp-block-heading">取得憑證</h3>



<p class="wp-block-paragraph">點擊「設定操作說明」，它會開啟一個視窗，並提供一個 gcloud 指令，把它複製起來，這樣之後才能從 Cloud Shell 直接推送映像檔上去：</p>



<figure class="wp-block-image aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="389" src="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-5-設定-gcloud-為憑證輔助程式-1024x389.png" alt="" class="wp-image-11178" srcset="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-5-設定-gcloud-為憑證輔助程式-1024x389.png 1024w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-5-設定-gcloud-為憑證輔助程式-300x114.png 300w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-5-設定-gcloud-為憑證輔助程式-768x292.png 768w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-5-設定-gcloud-為憑證輔助程式-1536x584.png 1536w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-5-設定-gcloud-為憑證輔助程式.png 1896w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">設定 gcloud 為憑證輔助程式</figcaption></figure>



<p class="wp-block-paragraph">直接將指令貼到 Cloud Shell 並執行，等它完成即可，你會看到它說 gcloud credentials 註冊成功：</p>



<figure class="wp-block-image aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="503" src="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-6-等待指令執完成-1024x503.png" alt="" class="wp-image-11179" srcset="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-6-等待指令執完成-1024x503.png 1024w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-6-等待指令執完成-300x147.png 300w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-6-等待指令執完成-768x377.png 768w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-6-等待指令執完成-1536x754.png 1536w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-6-等待指令執完成.png 1788w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">等待指令執完成</figcaption></figure>



<h3 class="wp-block-heading">建立 Docker 容器映像檔</h3>



<p class="wp-block-paragraph">接下來準備 Docker image 的完整名稱，它同時也是存放在 Artifact Registry 的路徑，格式如下：</p>



<p class="wp-block-paragraph">[位置]/[project-id]/[repo_name]/[image_name]:v1</p>



<p class="wp-block-paragraph">我的映像檔名字就像這樣：</p>



<p class="wp-block-paragraph">asia-east1-docker.pkg.dev/dong-dong-gcp-2-bigquery/nodejs-1/nodejs-web:v1</p>



<p class="wp-block-paragraph">準備好之後，就執行以下指令：</p>



<p class="wp-block-paragraph">docker build -t&nbsp; [位置]/[project-id]/[repo_name]/[image_name]:v1</p>



<p class="wp-block-paragraph">我的範例指令：</p>



<p class="wp-block-paragraph">docker build -t asia-east1-docker.pkg.dev/dong-dong-gcp-2-bigquery/nodejs-1/nodejs-web:v1 .&nbsp;</p>



<p class="wp-block-paragraph">你注意指令最後有一個句點，代表要把當前資料夾 (nodejs01) 內的檔案都打包進這個映像檔。</p>



<figure class="wp-block-image aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="425" src="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-7-建立容器映像檔-1024x425.png" alt="" class="wp-image-11180" srcset="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-7-建立容器映像檔-1024x425.png 1024w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-7-建立容器映像檔-300x125.png 300w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-7-建立容器映像檔-768x319.png 768w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-7-建立容器映像檔-1536x638.png 1536w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-7-建立容器映像檔.png 1866w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">建立容器映像檔</figcaption></figure>



<h3 class="wp-block-heading">測試 Docker Image 運作</h3>



<p class="wp-block-paragraph">我們再直接使用 Image 跑一次容器，確認應用程式正常</p>



<p class="wp-block-paragraph">docker run -d -p [port]:[port] [位置]/[project-id]/[repo_name]/[image_name]:v1</p>



<p class="wp-block-paragraph">我的範例指令：</p>



<p class="wp-block-paragraph">docker run -d -p 8080:8080 asia-east1-docker.pkg.dev/dong-dong-gcp-2-bigquery/nodejs-1/nodejs-web:v1&nbsp;&nbsp;&nbsp;&nbsp;</p>



<p class="wp-block-paragraph">參數 -d ，指的是背景執行</p>



<p class="wp-block-paragraph">參數 -p 8080:8080 ，指的是把主機的 8080 Port 所有流量轉發到 web 這個Container 的 8080 Port</p>



<figure class="wp-block-image aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="70" src="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-8-確認容器已開始運作-1024x70.png" alt="" class="wp-image-11181" srcset="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-8-確認容器已開始運作-1024x70.png 1024w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-8-確認容器已開始運作-300x21.png 300w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-8-確認容器已開始運作-768x53.png 768w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-8-確認容器已開始運作-1536x105.png 1536w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-8-確認容器已開始運作.png 1869w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">確認容器已開始運作</figcaption></figure>



<p class="wp-block-paragraph">然後我們點擊 Cloud Shell 視窗右上角的預覽按鈕，可以看到網頁已經正常顯示：</p>



<figure class="wp-block-image aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="531" src="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-9-確認能看到網頁-1024x531.png" alt="" class="wp-image-11182" srcset="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-9-確認能看到網頁-1024x531.png 1024w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-9-確認能看到網頁-300x156.png 300w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-9-確認能看到網頁-768x398.png 768w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-9-確認能看到網頁-1536x797.png 1536w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-9-確認能看到網頁.png 1677w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">確認能看到網頁</figcaption></figure>



<h3 class="wp-block-heading">停止 Docker 應用程式</h3>



<p class="wp-block-paragraph">確認無誤的話，我們要停止 Container 運作之前，要先查詢目前正在運作 Container 的 ID：&nbsp;</p>



<p class="wp-block-paragraph">docker ps&nbsp;</p>



<p class="wp-block-paragraph">取得 ID 之後，停止指定的 Container</p>



<p class="wp-block-paragraph">docker stop [container-id]</p>



<figure class="wp-block-image aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="233" src="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-10-停止容器運作-1024x233.png" alt="" class="wp-image-11183" srcset="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-10-停止容器運作-1024x233.png 1024w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-10-停止容器運作-300x68.png 300w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-10-停止容器運作-768x175.png 768w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-10-停止容器運作-1536x349.png 1536w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-10-停止容器運作.png 1808w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">停止容器運作</figcaption></figure>



<h2 class="wp-block-heading">推送 Docker Image 到 Artifact Registry</h2>



<p class="wp-block-paragraph">現在就可以正式 Push Docker Image 到 Artifact Registry 了，執行以下指令：</p>



<p class="wp-block-paragraph">docker push [位置]/[project-id]/[repo_name]/[image_name]:v1</p>



<p class="wp-block-paragraph">我的範例指令：</p>



<p class="wp-block-paragraph">docker&nbsp; push asia-east1-docker.pkg.dev/dong-dong-gcp-2-bigquery/nodejs-1/nodejs-web:v1 &nbsp;</p>



<figure class="wp-block-image aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="347" src="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-11-映像檔推送完成-1024x347.png" alt="" class="wp-image-11184" srcset="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-11-映像檔推送完成-1024x347.png 1024w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-11-映像檔推送完成-300x102.png 300w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-11-映像檔推送完成-768x261.png 768w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-11-映像檔推送完成-1536x521.png 1536w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-11-映像檔推送完成.png 1851w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">映像檔推送完成</figcaption></figure>



<p class="wp-block-paragraph">我們看到映像檔已經推上去了，我們可以回到 Console 上按重新整理：</p>



<figure class="wp-block-image aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="621" src="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-12-在-Console-上看到映像檔出現了-1024x621.png" alt="" class="wp-image-11185" srcset="https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-12-在-Console-上看到映像檔出現了-1024x621.png 1024w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-12-在-Console-上看到映像檔出現了-300x182.png 300w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-12-在-Console-上看到映像檔出現了-768x466.png 768w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-3-12-在-Console-上看到映像檔出現了.png 1523w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">在 Artifact Registry 上看到映像檔出現了</figcaption></figure>



<p class="wp-block-paragraph">現在我們有一個映像檔在上面，之後就可以部署到 <a href="https://dongdonggcp.com/2025/07/20/cloud-run-tutorials/" target="_blank" rel="noopener" title="">Cloud Run</a> 或 <a href="https://dongdonggcp.com/2025/07/20/google-kubernetes-engine-tutorials/" title="">Google Kubernetes Engine</a> 囉！</p>



<p class="wp-block-paragraph">影片版教學：</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<div class="ast-oembed-container " style="height: 100%;"><iframe title="[GKE 教學] 031 Artifact Registry 教學，如何在 GCP 上建立第一個 Docker 容器映像檔 Container Image？| 免費下載 GKE 簡報、完整指令和程式碼" width="1200" height="675" src="https://www.youtube.com/embed/6DHUC-MZ34U?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></div>
</div></figure><p>The post <a href="https://dongdonggcp.com/2025/07/20/artifact-registry-tutorials/">儲存容器映像檔的 Artifact Registry 簡介與上傳 Docker 映像檔教學</a> first appeared on <a href="https://dongdonggcp.com">東東 GCP 教學 - GCP 實戰講師</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://dongdonggcp.com/2025/07/20/artifact-registry-tutorials/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">11173</post-id>	</item>
		<item>
		<title>上傳程式碼就能跑的微服務 – Cloud Run Functions 簡介與基本操作教學</title>
		<link>https://dongdonggcp.com/2025/07/20/cloud-run-functions-tutorials/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cloud-run-functions-tutorials</link>
					<comments>https://dongdonggcp.com/2025/07/20/cloud-run-functions-tutorials/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 20 Jul 2025 07:57:00 +0000</pubDate>
				<category><![CDATA[無伺服器]]></category>
		<category><![CDATA[Cloud Run]]></category>
		<category><![CDATA[Cloud Run Function]]></category>
		<category><![CDATA[Serverless]]></category>
		<guid isPermaLink="false">https://dongdonggcp.com/?p=11162</guid>

					<description><![CDATA[<p>Cloud Run Functions 是 Google Cloud 的 Functions as a Service (FaaS) 平台，讓你可以撰寫小型的、專注於單一任務的函式，不用管理機器。支援的程式語言包含 .Net、Go、Java、Node.js、PHP、Python 和 Ruby。</p>
<p>The post <a href="https://dongdonggcp.com/2025/07/20/cloud-run-functions-tutorials/">上傳程式碼就能跑的微服務 – Cloud Run Functions 簡介與基本操作教學</a> first appeared on <a href="https://dongdonggcp.com">東東 GCP 教學 - GCP 實戰講師</a>.</p>]]></description>
										<content:encoded><![CDATA[<p class="wp-block-paragraph">現在我們要介紹和 App Engine 同樣具備「上傳即部署」特性的服務：Cloud Run Functions。</p>



<p class="wp-block-paragraph">雖然 App Engine 和 Cloud Run Functions 都提供了無需管理基礎設施的便利性，但兩者在應用場景上有著重要差異。App Engine 適合部署完整的 Web 應用程式，而 Cloud Run Functions 則專注於輕量級的函式服務，讓開發人員能夠實現事件驅動的微服務架構。</p>



<h2 class="wp-block-heading">什麼是 Cloud Run Functions？</h2>



<p class="wp-block-paragraph">Cloud Run Functions 原名叫 Cloud Function，是 Google Cloud 的 Functions as a Service (FaaS) 平台，讓你可以撰寫小型的、專注於單一任務的函式，不用管理機器。支援的程式語言包含 .Net、Go、Java、Node.js、PHP、Python 和 Ruby。</p>



<p class="wp-block-paragraph">這些函式會在事件發生時自動執行，用完即停，真正實現了「用多少算多少」的理念。</p>



<p class="wp-block-paragraph">想像一下，你經營一家網路商店，當客戶下單時需要：</p>



<ul class="wp-block-list">
<li>發送確認郵件給客戶</li>



<li>更新庫存數量</li>



<li>記錄銷售資料到分析系統</li>



<li>通知物流部門準備出貨</li>
</ul>



<p class="wp-block-paragraph">在傳統單體式架構中，這些功能可能都寫在同一個應用程式中。但使用 Cloud Run Functions，你可以為每個任務建立獨立的函式，當訂單事件發生時，這些函式會自動被觸發 (Trigger) 執行，就是所謂的「事件驅動」特性。</p>



<p class="wp-block-paragraph">另外 Cloud Run Functions 也能 Autoscale，當同時有大量事件需要處理時，平台會自動建立多個函式執行個體 (instance；指輕量級的容器) 來並行處理；當需求降低時，也會自動縮減執行個體數量，甚至降到零。</p>



<p class="wp-block-paragraph">所以和 App Engine 一樣，你只需為函式實際執行的時間和資源消耗付費，沒有請求時完全不收費。</p>



<h2 class="wp-block-heading">Cloud Run Functions 的觸發機制</h2>



<p class="wp-block-paragraph">關於觸發機制，Cloud Run Functions 可以透過多種方式被觸發：</p>



<p class="wp-block-paragraph"><strong>HTTP Trigger</strong>：最常見的觸發方式，函式會曝露一個 HTTPS 端點，可以透過 REST API 呼叫。適合建立微服務 API、Webhook 處理器等。</p>



<p class="wp-block-paragraph"><strong>Cloud Storage Trigger</strong>：當 Google Cloud Storage 中的檔案被建立、刪除或修改時觸發。適合檔案處理、圖片縮放、資料備份等場景。</p>



<p class="wp-block-paragraph"><strong>Pub/Sub Trigger</strong>：當 Cloud Pub/Sub Topic 收到訊息時觸發。適合非同步訊息處理、事件串流處理等。</p>



<p class="wp-block-paragraph"><strong>Firebase Trigger</strong>：與 Firebase 服務整合，可以因應 Firestore 資料庫變更、使用者註冊、帳號變更事件等。</p>



<p class="wp-block-paragraph"><strong>Cloud Scheduler 觸發器</strong>：定時執行函式，類似 Cron Job 的功能。</p>



<h2 class="wp-block-heading">建立 Cloud Run Functions 示範</h2>



<p class="wp-block-paragraph">接著我們來建立一個簡單的函式看看，點擊主選單「Clodu Run 函式」再點擊「編寫函式」：</p>



<figure class="wp-block-image aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="343" src="https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-1-編寫-Cloud-Run-Function-1024x343.png" alt="" class="wp-image-11163" srcset="https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-1-編寫-Cloud-Run-Function-1024x343.png 1024w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-1-編寫-Cloud-Run-Function-300x101.png 300w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-1-編寫-Cloud-Run-Function-768x257.png 768w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-1-編寫-Cloud-Run-Function-1536x515.png 1536w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-1-編寫-Cloud-Run-Function.png 1880w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">編寫 Cloud Run Function</figcaption></figure>



<p class="wp-block-paragraph">給函式命名、設定地區和程式語言版本，你看到它有產生一個端點網址，那就是之後對外服務的網址：</p>



<figure class="wp-block-image aligncenter size-full"><img loading="lazy" decoding="async" width="1012" height="762" src="https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-2-給函式命名、設定地區和程式語言版本.png" alt="" class="wp-image-11164" srcset="https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-2-給函式命名、設定地區和程式語言版本.png 1012w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-2-給函式命名、設定地區和程式語言版本-300x226.png 300w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-2-給函式命名、設定地區和程式語言版本-768x578.png 768w" sizes="(max-width: 1012px) 100vw, 1012px" /><figcaption class="wp-element-caption">給函式命名、設定地區和程式語言版本</figcaption></figure>



<p class="wp-block-paragraph">往下看到驗證的部分，我們要讓所有網路上的任何人都可以存取，所以要取消「使用 IAM 驗證傳入要求」。另外在服務資源調度的部分，我們要把「執行個體數量下限」改成 0。</p>



<figure class="wp-block-image aligncenter size-full"><img loading="lazy" decoding="async" width="780" height="790" src="https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-3-取消勾選-IAM-驗證並設定執行個體數是下限.png" alt="" class="wp-image-11165" srcset="https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-3-取消勾選-IAM-驗證並設定執行個體數是下限.png 780w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-3-取消勾選-IAM-驗證並設定執行個體數是下限-296x300.png 296w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-3-取消勾選-IAM-驗證並設定執行個體數是下限-768x778.png 768w" sizes="(max-width: 780px) 100vw, 780px" /><figcaption class="wp-element-caption">取消勾選 IAM 驗證並設定執行個體數是下限</figcaption></figure>



<p class="wp-block-paragraph">再來 Ingress 的部分，我們要勾選「全部」，這樣子才能開放外部進來存取喔！然後我們再點擊右下角的展開按鈕，查看進階設定：</p>



<figure class="wp-block-image aligncenter size-full"><img loading="lazy" decoding="async" width="1007" height="391" src="https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-4-設定允許全部流量，並打開進階設定.png" alt="" class="wp-image-11166" srcset="https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-4-設定允許全部流量，並打開進階設定.png 1007w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-4-設定允許全部流量，並打開進階設定-300x116.png 300w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-4-設定允許全部流量，並打開進階設定-768x298.png 768w" sizes="(max-width: 1007px) 100vw, 1007px" /><figcaption class="wp-element-caption">設定允許全部流量，並打開進階設定</figcaption></figure>



<p class="wp-block-paragraph">大部分內容都可以保持預設，主要看的是資源的部分，在正式的環境當中，你可以調整記憶體和 CPU 的規格，在這裡我們保持預設就好：</p>



<figure class="wp-block-image aligncenter size-full"><img loading="lazy" decoding="async" width="967" height="805" src="https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-5-可以調整執行個體的規格.png" alt="" class="wp-image-11167" srcset="https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-5-可以調整執行個體的規格.png 967w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-5-可以調整執行個體的規格-300x250.png 300w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-5-可以調整執行個體的規格-768x639.png 768w" sizes="(max-width: 967px) 100vw, 967px" /><figcaption class="wp-element-caption">可以調整執行個體的規格</figcaption></figure>



<p class="wp-block-paragraph">「執行個體數量上限」預設是 100，如果擔心它真的開到 100 台，可以像我一樣調成 5 就好，其他保持預設，最後按下「建立」：</p>



<figure class="wp-block-image aligncenter size-full"><img loading="lazy" decoding="async" width="846" height="865" src="https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-6-設定執行個體數量上下限.png" alt="" class="wp-image-11168" srcset="https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-6-設定執行個體數量上下限.png 846w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-6-設定執行個體數量上下限-293x300.png 293w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-6-設定執行個體數量上下限-768x785.png 768w" sizes="(max-width: 846px) 100vw, 846px" /><figcaption class="wp-element-caption">設定執行個體數量上下限</figcaption></figure>



<p class="wp-block-paragraph">接下來畫面會自動跳轉到「來源」這個頁籤，你可以在下方編輯程式碼，在這裡我們就保持不變，直接按下「儲存並重新部署」：</p>



<figure class="wp-block-image aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="569" src="https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-7-儲存並重新部署-1024x569.png" alt="" class="wp-image-11169" srcset="https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-7-儲存並重新部署-1024x569.png 1024w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-7-儲存並重新部署-300x167.png 300w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-7-儲存並重新部署-768x427.png 768w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-7-儲存並重新部署.png 1289w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">儲存並重新部署</figcaption></figure>



<p class="wp-block-paragraph">接著它會在背景作業，將現有程式碼打包成容器，等它幾秒鐘後，就可以點擊網址，並且看到網頁了。</p>



<figure class="wp-block-image aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="601" src="https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-8-點擊網址看到網頁內容-1024x601.png" alt="" class="wp-image-11170" srcset="https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-8-點擊網址看到網頁內容-1024x601.png 1024w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-8-點擊網址看到網頁內容-300x176.png 300w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-8-點擊網址看到網頁內容-768x451.png 768w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-8-點擊網址看到網頁內容-1536x902.png 1536w, https://dongdonggcp.com/wp-content/uploads/2025/07/9-2-8-點擊網址看到網頁內容.png 1782w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">點擊網址看到網頁內容</figcaption></figure>



<p class="wp-block-paragraph"></p><p>The post <a href="https://dongdonggcp.com/2025/07/20/cloud-run-functions-tutorials/">上傳程式碼就能跑的微服務 – Cloud Run Functions 簡介與基本操作教學</a> first appeared on <a href="https://dongdonggcp.com">東東 GCP 教學 - GCP 實戰講師</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://dongdonggcp.com/2025/07/20/cloud-run-functions-tutorials/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">11162</post-id>	</item>
	</channel>
</rss>
