在Docker 中使用私有GitLab 模組建立Go 應用
本指南旨在解決將私有GitLab 模組整合到Go 應用程式時面臨的特定挑戰在Docker 中。
了解問題
當 Dockerfile 嘗試從 GitLab 提取私有包,但儘管遵循類似的 GitHub 教程但身份驗證失敗時,就會出現此問題。這個具體問題源自於 GitHub 和 GitLab 的身份驗證機制之間的差異。
建議的解決方案
為了解決這個問題,我們將利用基於 SSH 的身份驗證並採用 Docker 的實驗功能。
實驗性Docker功能
透過在Dockerfile 開頭加入以下註解來啟用實驗性Docker 功能:
# syntax=docker/dockerfile:experimental
Docker設定
- 確保您的建置環境有openssh-client:
RUN apk --no-cache add build-base git mercurial gcc curl openssh-client
- 建立known_hosts並且設定.git config:
RUN mkdir -p -m 0700 ~/.ssh && \ ssh-keyscan gitlab.com >> ~/.ssh/known_hosts && \ echo -e "[url \"[email protected]:<company-name>\"]\n\tinsteadOf = https://gitlab.com/<company-name>\"" >> ~/.gitconfig</company-name></company-name>
- 使用基於SSH的建立你的Go應用程式驗證:
ENV GO111MODULE=on ENV GOPRIVATE=gitlab.com/<company-name> RUN --mount=type=ssh cd cmd/app/ && go build -o app</company-name>
在ssh-agent 中載入金鑰
在建置Docker 映像之前,必須將私鑰載入到ssh-agent 中:ssh-add id_rsa
建置Docker映像
建置Docker映像
DOCKER_BUILDKIT=1 docker build --progress=plain .
使用以下命令建置Docker鏡像:
偵錯SSH 連線
RUN ssh -A -v -l git gitlab.com
如果出現SSH 連線問題,請新增下列偵錯指令到您的Dockerfile:
解決AppArmor問題/run/user/1000/keyring/ssh rw,
如果apparmor 阻止Docker 存取SSH 密鑰套接字,請編輯apparmor 設定檔並在關閉}:
密鑰檔案名稱問題確保私鑰檔案名稱是SSH 期望的預設值之一(例如id_rsa)或使用 .ssh/config 檔案將自訂金鑰名稱對應到預期的預設值。
附加說明避免使用 chmod授予權限,因為 SSH 金鑰條目足以共享私鑰。也建議避免使用 .netrc,因為它將憑證嵌入到 Docker 映像中,可能會危及安全性。以上是如何使用 SSH 在 Docker 中使用私有 GitLab 模組建立 Go 應用程式?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

goisastrongchoiceforprojectsneedingsimplicity,績效和引發性,butitmaylackinadvancedfeatures and ecosystemmaturity.1)

Go'sinitfunctionandJava'sstaticinitializersbothservetosetupenvironmentsbeforethemainfunction,buttheydifferinexecutionandcontrol.Go'sinitissimpleandautomatic,suitableforbasicsetupsbutcanleadtocomplexityifoverused.Java'sstaticinitializersoffermorecontr

thecommonusecasesfortheinitfunctionoare:1)加載configurationfilesbeforeThemainProgramStarts,2)初始化的globalvariables和3)runningpre-checkSorvalidationsbeforEtheprofforeTheProgrecce.TheInitFunctionIsautefunctionIsautomentycalomationalmatomatimationalycalmatemationalcalledbebeforethemainfuniinfuninfuntuntion

ChannelsarecrucialingoforenablingsafeandefficityCommunicationBetnewengoroutines.theyfacilitateSynChronizationAndManageGoroutIneLifeCycle,EssentialforConcurrentProgramming.ChannelSallSallSallSallSallowSallowsAllowsEnderDendingAndReceivingValues,ActassignalsignalsforsynChronization,and actassignalsynChronization and andsupppor

在Go中,可以通過errors.Wrap和errors.Unwrap方法來包裝錯誤並添加上下文。 1)使用errors包的新功能,可以在錯誤傳播過程中添加上下文信息。 2)通過fmt.Errorf和%w包裝錯誤,幫助定位問題。 3)自定義錯誤類型可以創建更具語義化的錯誤,增強錯誤處理的表達能力。

Gooffersrobustfeaturesforsecurecoding,butdevelopersmustimplementsecuritybestpracticeseffectively.1)UseGo'scryptopackageforsecuredatahandling.2)Manageconcurrencywithsynchronizationprimitivestopreventraceconditions.3)SanitizeexternalinputstoavoidSQLinj

Go的錯誤接口定義為typeerrorinterface{Error()string},允許任何實現Error()方法的類型被視為錯誤。使用步驟如下:1.基本檢查和記錄錯誤,例如iferr!=nil{log.Printf("Anerroroccurred:%v",err)return}。 2.創建自定義錯誤類型以提供更多信息,如typeMyErrorstruct{MsgstringDetailstring}。 3.使用錯誤包裝(自Go1.13起)來添加上下文而不丟失原始錯誤信息,

對效率的Handleerrorsinconcurrentgopragrs,UsechannelstocommunicateErrors,enplionErrorWatchers,Instertimeout,UsebufferedChannels和Provideclearrormessages.1)USEchannelelStopassErtopassErrorsErtopassErrorsErrorsErrorsFromGoroutInestOthemainFunction.2)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

記事本++7.3.1
好用且免費的程式碼編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能