Golang chromedp dockerfile

huangapple go评论69阅读模式
英文:

Golang chromedp dockerfile

问题

这个错误意味着在Docker容器中找不到可执行文件"google-chrome"。这可能是因为在Docker镜像中没有正确安装Chrome浏览器。

要在Docker容器中运行Chrome浏览器,你需要在Dockerfile中添加一些额外的步骤。以下是更新后的Dockerfile示例:

FROM golang:1.20 AS build-stage

WORKDIR /app

# 安装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

# 安装Chrome的依赖
RUN apt-get update && apt-get -y install libxss1 libappindicator3-1 libindicator7

# 下载Chrome浏览器
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb
RUN apt-get -y install -f

COPY go.mod go.sum ./
RUN go mod download

COPY *.go ./

RUN CGO_ENABLED=0 GOOS=linux go build -o /dockergo

# 在容器中运行测试
FROM build-stage AS run-test-stage
RUN go test -v ./...

# 将应用程序二进制文件部署到精简的镜像中
FROM gcr.io/distroless/base-debian11 AS build-release-stage

WORKDIR /

COPY --from=build-stage /dockergo /dockergo

EXPOSE 8080

USER nonroot:nonroot

ENTRYPOINT ["/dockergo"]

这个更新后的Dockerfile添加了一些额外的步骤来安装Chrome浏览器及其依赖项。这样,当你构建并运行Docker镜像时,就能够在容器中找到并运行Chrome浏览器了。

英文:

I have a golang code that uses chromedp to connect to the user's local chrome
here is my code:

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)
}

so i made a dockerfile which contains what i need for my clients to use this application (i installed chrome and built my golang in the image)

dockerfile:

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"]

the image builds successfully and without a headache
but when testing the docker image in my local I get this error:

 Error: exec: "google-chrome": executable file not found in $PATH

what does this error mean? that my chrome is not running? how can i run it?

答案1

得分: 1

Chrome浏览器仅安装在build-stage中,不会出现在由build-release-stage创建的最终镜像中。

我尝试使用以下Dockerfile安装Chrome:

# 将应用程序二进制文件部署到精简镜像中
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,非常小巧。下面的演示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
# 构建测试二进制文件
RUN CGO_ENABLED=0 go test -c -o dockergo.test

# 在容器中运行测试
FROM chromedp/headless-shell:114.0.5735.199 AS run-test-stage

WORKDIR /app
# 复制其他运行测试所需的文件(testdata?)
COPY . .
COPY --from=build-stage /app/dockergo.test ./dockergo.test
RUN /app/dockergo.test -test.v

# 将应用程序二进制文件部署到精简镜像中
FROM chromedp/headless-shell:114.0.5735.199 AS build-release-stage

COPY --from=build-stage /app/dockergo /dockergo

EXPOSE 8080

ENTRYPOINT ["/dockergo"]
英文:

The Chrome browser is installed in the build-stage only. It's not available in the final image that is created by the build-release-stage.

I tried to install Chrome with this Dockerfile:

# 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 &

But it failed with this message:

...
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

I think you have to choose another base image on which it is easy to install Chrome. A better alternative is to use chromedp/headless-shell as the base image. This image contains Chrome's headless shell, which is very small. The demo Dockerfile below also shows compiling the test binary first and then run the test in the chromedp/headless-shell image:

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"]

huangapple
  • 本文由 发表于 2023年6月30日 02:15:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583664.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定