在現代軟件開發中,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"]
登錄后復制