如何在 Docker 中运行 ChromedP



我正在尝试在docker中运行chromedp。 我的主去:

package main
import (
"context"
"log"
"time"
"github.com/chromedp/chromedp"
)
func main() {
log.SetFlags(log.LstdFlags | log.Llongfile)
ctx, cancel := chromedp.NewContext(
context.Background(),
chromedp.WithLogf(log.Printf),
)
defer cancel()
// create a timeout
ctx, cancel = context.WithTimeout(ctx, 15 * time.Second)
defer cancel()
u := `https://www.whatismybrowser.com/detect/what-is-my-user-agent`
selector := `#detected_value`
log.Println("requesting", u)
log.Println("selector", selector)
var result string
err := chromedp.Run(ctx,
chromedp.Navigate(u),
chromedp.WaitReady(selector),
chromedp.OuterHTML(selector, &result),
)
if err != nil {
log.Fatal(err)
}
log.Printf("result:n%s", result)
}

Dockerfile:

FROM golang:latest as build-env
RUN mkdir $GOPATH/src/app
WORKDIR $GOPATH/src/app
ENV GO111MODULE=on
COPY go.mod .
COPY go.sum .
COPY main.go .
RUN go mod download
RUN go build -o /root/app
FROM chromedp/headless-shell
COPY --from=build-env /root/app /
CMD ["/app"]

当我运行它时:

docker-compose build
docker-compose up

它输出:

app_1  | [1129/192523.576726:WARNING:resource_bundle.cc(426)] locale_file_path.empty() for locale 
app_1  | [1129/192523.602779:WARNING:resource_bundle.cc(426)] locale_file_path.empty() for locale 
app_1  | 
app_1  | DevTools listening on ws://0.0.0.0:9222/devtools/browser/3fa247e0-e2fa-484e-8b5f-172b392701bb
app_1  | [1129/192523.836854:WARNING:resource_bundle.cc(426)] locale_file_path.empty() for locale 
app_1  | [1129/192523.838804:WARNING:resource_bundle.cc(426)] locale_file_path.empty() for locale 
app_1  | [1129/192523.845866:ERROR:egl_util.cc(60)] Failed to load GLES library: /headless-shell/swiftshader/libGLESv2.so: /headless-shell/swiftshader/libGLESv2.so: cannot open shared object file: No such file or directory
app_1  | [1129/192523.871796:ERROR:viz_main_impl.cc(176)] Exiting GPU process due to errors during initialization
app_1  | [1129/192523.897083:WARNING:gpu_process_host.cc(1220)] The GPU process has crashed 1 time(s)
app_1  | [1129/192523.926741:WARNING:resource_bundle.cc(426)] locale_file_path.empty() for locale 
app_1  | [1129/192523.930111:ERROR:egl_util.cc(60)] Failed to load GLES library: /headless-shell/swiftshader/libGLESv2.so: /headless-shell/swiftshader/libGLESv2.so: cannot open shared object file: No such file or directory
app_1  | [1129/192523.943794:ERROR:viz_main_impl.cc(176)] Exiting GPU process due to errors during initialization
app_1  | [1129/192523.948757:WARNING:gpu_process_host.cc(1220)] The GPU process has crashed 2 time(s)
app_1  | [1129/192523.950107:ERROR:browser_gpu_channel_host_factory.cc(138)] Failed to launch GPU process.
app_1  | [1129/192524.013014:ERROR:browser_gpu_channel_host_factory.cc(138)] Failed to launch GPU process.

所以它不运行我的go应用程序。我希望chromedp/headless-shell包含谷歌浏览器,我的golang应用程序将成功地使用它 github.com/chromedp/chromedp

更新 1

我添加了缺少的目录:

RUN mkdir -p /headless-shell/swiftshader/ 
&& cd /headless-shell/swiftshader/ 
&& ln -s ../libEGL.so libEGL.so 
&& ln -s ../libGLESv2.so libGLESv2.so

现在有以下输出,我的应用程序仍未运行:

app_1  | [1202/071210.095414:WARNING:resource_bundle.cc(426)] locale_file_path.empty() for locale 
app_1  | [1202/071210.112632:WARNING:resource_bundle.cc(426)] locale_file_path.empty() for locale 
app_1  | 
app_1  | DevTools listening on ws://0.0.0.0:9222/devtools/browser/86e31db1-3a17-4da6-9e2f-696647572492
app_1  | [1202/071210.166158:WARNING:resource_bundle.cc(426)] locale_file_path.empty() for locale 
app_1  | [1202/071210.186307:WARNING:resource_bundle.cc(426)] locale_file_path.empty() for locale 

更新 2

看起来CMD ["/app"]没有运行我的 main.go 文件,因为它没有打印任何行。 当我手动运行它时:

$ /usr/local/bin/docker exec -ti chromedp_docker_app_1 /bin/bash
root@0c417fd159a2:/# /app
2019/12/02 07:40:34 app is running
2019/12/02 07:40:34 /go/src/app/main.go:26: requesting https://www.whatismybrowser.com/detect/what-is-my-user-agent
2019/12/02 07:40:34 /go/src/app/main.go:27: selector #detected_value
2019/12/02 07:40:34 /go/src/app/main.go:35: exec: "google-chrome": executable file not found in $PATH

我看到google-chrome应用程序仍然不存在,嗯....

你在这里缺少一些东西, 首先,您需要在容器内运行google-less-head-chrome。 您可以使用以下 Dockerfile

FROM golang:1.12.0-alpine3.9
RUN apk update && apk upgrade && apk add --no-cache bash git && apk add --no-cache chromium
# Installs latest Chromium package.
RUN echo @edge http://nl.alpinelinux.org/alpine/edge/community >> /etc/apk/repositories 
&& echo @edge http://nl.alpinelinux.org/alpine/edge/main >> /etc/apk/repositories 
&& apk add --no-cache 
harfbuzz@edge 
nss@edge 
freetype@edge 
ttf-freefont@edge 
&& rm -rf /var/cache/* 
&& mkdir /var/cache/apk
RUN go get github.com/mafredri/cdp

CMD chromium-browser --headless --disable-gpu --remote-debugging-port=9222 --disable-web-security --safebrowsing-disable-auto-update --disable-sync --disable-default-apps --hide-scrollbars --metrics-recording-only --mute-audio --no-first-run --no-sandbox

我正在使用CDP,对我来说更强大,更有趣! 这是 CDP 的链接:https://github.com/mafredri/cdp

不漂亮,但这里有一个简单的码头工人对我有用

FROM golang:1.16.5 AS build-env
RUN apt update && apt -y upgrade 
RUN apt -y install chromium
WORKDIR /app

ADD ./ ./
RUN go mod download
RUN go build -o /docker-gs-ping
CMD [ "/docker-gs-ping" ]

相关内容

  • 没有找到相关文章