在现代软件开发中,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中文网其他相关文章!