在現代軟體開發中,Docker已經成為了一個不可或缺的工具,它能夠幫助開發人員快速建置、部署和管理應用程式。而Golang作為一種高效率、簡潔的程式語言,也備受開發者的青睞。那麼,如何在Docker中使用Golang開發應用程式呢?本文將為大家介紹如何撰寫一個Golang應用程式的Dockerfile,並利用chromedp函式庫實現Web自動化測試。如果你對Golang、Docker以及Web自動化測試有興趣的話,不妨繼續往下看。
我有一個 golang 程式碼,它使用 chromedp 連接到使用者的本機 chrome 這是我的程式碼:
package main import ( "context" "fmt" "log" "os" "time" "github.com/chromedp/chromedp" "github.com/gin-gonic/gin" ) func main() { api := gin.default() api.get("api/jwt", func(c *gin.context) { opts := append(chromedp.defaultexecallocatoroptions[:], chromedp.flag("headless", false), chromedp.flag("disable-gpu", true), chromedp.flag("no-sandbox", true), chromedp.flag("disable-dev-shm-usage", true), chromedp.flag("disable-browser-side-navigation", true), chromedp.flag("disable-infobars", true), chromedp.flag("disable-extensions", true), chromedp.flag("disable-notifications", true), chromedp.flag("disable-default-apps", true), chromedp.flag("disable-background-timer-throttling", true), chromedp.flag("disable-backgrounding-occluded-windows", true), chromedp.flag("disable-renderer-backgrounding", true), ) allocctx, cancel := chromedp.newexecallocator(context.background(), opts...) defer cancel() ctx, cancel := chromedp.newcontext(allocctx) defer cancel() var localstoragedata string // declaração da variável localstoragedata err := chromedp.run(ctx, chromedp.navigate("https://csonlinetenant.b2clogin.com/csonlinetenant.onmicrosoft.com/oauth2/v2.0/authorize"), chromedp.sleep(5*time.second), chromedp.waitvisible(`#fgh`), chromedp.sendkeys(`#fghfg`, "fghfgh"), chromedp.sendkeys(`#xcvxcv`, "xcxcvcxv"), chromedp.click(`#thgh`, chromedp.byid), chromedp.sleep(5*time.second), chromedp.click(`dfgd`, chromedp.byid), chromedp.sleep(15*time.second), chromedp.evaluateasdevtools(`localstorage.getitem('c')`, &localstoragedata), ) if err != nil { log.printf("error: %v", err) return } fmt.println("bearer", localstoragedata) // restante do código... c.json(200, gin.h{ "success": localstoragedata, }) }) listenaddr := os.getenv("listen") if val, ok := os.lookupenv("functions_customhandler_port"); ok { listenaddr = ":" + val } if listenaddr == "" { listenaddr = ":8080" } api.run(listenaddr) }
所以我製作了一個 dockerfile,其中包含我的客戶使用此應用程式所需的內容(我安裝了 chrome 並在映像中建置了我的 golang)
docker 檔案:
from golang:1.20 as build-stage workdir /app # instale as dependências do chrome run wget -q -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \ && echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list run apt-get update && apt-get -y install google-chrome-stable run chrome & copy go.mod go.sum ./ run go mod download copy *.go ./ run cgo_enabled=0 goos=linux go build -o /dockergo # run the tests in the container from build-stage as run-test-stage run go test -v ./... # deploy the application binary into a lean image from gcr.io/distroless/base-debian11 as build-release-stage workdir / copy --from=build-stage /dockergo /dockergo expose 8080 user nonroot:nonroot entrypoint ["/dockergo"]
圖像建立成功並且沒有令人頭痛的情況 但是當在本機測試 docker 映像時,我收到此錯誤:
Error: exec: "google-chrome": executable file not found in $PATH
這個錯誤是什麼意思?我的 chrome 沒有運行?我怎樣才能運行它?
chrome瀏覽器僅安裝在build-stage
。它在 build-release-stage
創建的最終映像中不可用。
我嘗試使用此 dockerfile 安裝 chrome:
# deploy the application binary into a lean image from gcr.io/distroless/base-debian11 as build-release-stage run wget -q -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \ && echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list run apt-get update && apt-get -y install google-chrome-stable run chrome &
但失敗並顯示以下訊息:
... step 2/4 : run wget -q -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list ---> running in 7596202a5684 failed to create shim task: oci runtime create failed: runc create failed: unable to start container process: exec: "/bin/sh": stat /bin/sh: no such file or directory: unknown
我認為你必須選擇另一個可以輕鬆安裝 chrome 的基礎映像。更好的選擇是使用 chromedp/headless-shell
作為基礎鏡像。該圖像包含 chrome 的無頭 shell,該 shell 非常小。下面的示範 dockerfile 還顯示了首先編譯測試二進位文件,然後在 chromedp/headless-shell
映像中執行測試:
FROM golang:1.20.5-buster AS build-stage WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . RUN CGO_ENABLED=0 go build -o dockergo # Build the test binary RUN CGO_ENABLED=0 go test -c -o dockergo.test # Run the tests in the container FROM chromedp/headless-shell:114.0.5735.199 AS run-test-stage WORKDIR /app # Copy other files that is needed to run the test (testdata?). COPY . . COPY --from=build-stage /app/dockergo.test ./dockergo.test RUN /app/dockergo.test -test.v # Deploy the application binary into a lean image FROM chromedp/headless-shell:114.0.5735.199 AS build-release-stage COPY --from=build-stage /app/dockergo /dockergo EXPOSE 8080 ENTRYPOINT ["/dockergo"]
以上是Golang chromedp dockerfile的詳細內容。更多資訊請關注PHP中文網其他相關文章!