2 votes

comment exécuter chromedp dans docker

J'essaie d'exécuter chromép dans le docker. Mon 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"]

Quand je l'exécute :

docker-compose build
docker-compose up

Il sort :

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.

Donc ça ne fonctionne pas avec mon application Go. Je m'attends à ce que chromedp/headless-shell contient google-chrome et mon application golang l'utiliserait avec succès par dessus github.com/chromedp/chromedp

Mise à jour 1

I ajouté les répertoires manquants :

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

et j'ai maintenant le résultat suivant, mon application ne fonctionne toujours pas :

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 

Mise à jour 2

On dirait que CMD ["/app"] n'exécute pas mon fichier main.go car il n'imprime aucune ligne de celui-ci. Et quand je le lance manuellement :

$ /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

Je vois que google-chrome L'application n'est toujours pas là, hmmm....

5voto

arshpreet Points 329

Il vous manque quelques éléments, tout d'abord vous devez exécuter google-headless-chrome dans votre conteneur. Vous pouvez utiliser le Dockerfile suivant

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

J'utilise le CDP, plus robuste et plus amusant pour moi ! Voici le lien pour CDP : https://github.com/mafredri/cdp

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X