我正在尝试在 docker 中运行chromedp。我的main.go:
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
包含 google-chrome 并且我的 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
应用程序仍然不存在,嗯....