Skip to content

Instantly share code, notes, and snippets.

@vianhanif
Last active October 20, 2023 19:01
Show Gist options
  • Save vianhanif/e73d9a2c257b8934da9cbd698297c9ed to your computer and use it in GitHub Desktop.
Save vianhanif/e73d9a2c257b8934da9cbd698297c9ed to your computer and use it in GitHub Desktop.
Golang (chromedp) + Xvfb + Chrome + Docker
// somewhere in your chromedp init
opts := []chromedp.ExecAllocatorOption{
chromedp.NoFirstRun,
chromedp.NoDefaultBrowserCheck,
chromedp.DisableGPU,
chromedp.NoSandbox,
// chromedp.Headless,
// After Puppeteer's default behavior.
chromedp.Flag("disable-infobars", true),
chromedp.Flag("excludeSwitches", "enable-automation"),
chromedp.Flag("disable-background-networking", true),
chromedp.Flag("enable-features", "NetworkService,NetworkServiceInProcess"),
chromedp.Flag("disable-background-timer-throttling", true),
chromedp.Flag("disable-backgrounding-occluded-windows", true),
chromedp.Flag("disable-breakpad", true),
chromedp.Flag("disable-client-side-phishing-detection", true),
chromedp.Flag("disable-default-apps", true),
chromedp.Flag("disable-dev-shm-usage", true),
chromedp.Flag("disable-extensions", true),
chromedp.Flag("disable-features", "site-per-process,TranslateUI,BlinkGenPropertyTrees"),
chromedp.Flag("disable-hang-monitor", true),
chromedp.Flag("disable-ipc-flooding-protection", true),
chromedp.Flag("disable-popup-blocking", true),
chromedp.Flag("disable-prompt-on-repost", true),
chromedp.Flag("disable-renderer-backgrounding", true),
chromedp.Flag("disable-sync", true),
chromedp.Flag("force-color-profile", "srgb"),
chromedp.Flag("metrics-recording-only", true),
chromedp.Flag("safebrowsing-disable-auto-update", true),
chromedp.Flag("enable-automation", true),
chromedp.Flag("password-store", "basic"),
chromedp.Flag("use-mock-keychain", true),
}
allocatorCtx, cancelAllocator := chromedp.NewExecAllocator(s.ctx, opts...)
// more code
FROM golang:1.11-alpine as builder
WORKDIR /myapp
COPY go.mod .
COPY go.sum .
RUN apk add --no-cache ca-certificates git
# Get dependancies - will also be cached if we won't change mod/sum
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOARCH=amd64 go install -installsuffix "static" ./cmd/myapp/...
FROM ubuntu:trusty
ENV LANG="C.UTF-8"
# install utilities
RUN apt-get update
RUN apt-get -y install wget --fix-missing
RUN apt-get -y install xvfb xorg xvfb firefox dbus-x11 xfonts-100dpi xfonts-75dpi xfonts-cyrillic --fix-missing # chrome will use this to run headlessly
RUN apt-get -y install unzip --fix-missing
# install go
RUN wget -O - 'https://storage.googleapis.com/golang/go1.7.linux-amd64.tar.gz' | tar xz -C /usr/local/
ENV PATH="$PATH:/usr/local/go/bin"
# install dbus - chromedriver needs this to talk to google-chrome
RUN apt-get -y install dbus --fix-missing
RUN apt-get -y install dbus-x11 --fix-missing
RUN ln -s /bin/dbus-daemon /usr/bin/dbus-daemon # /etc/init.d/dbus has the wrong location
RUN ln -s /bin/dbus-uuidgen /usr/bin/dbus-uuidgen # /etc/init.d/dbus has the wrong location
# install chrome
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
RUN apt-get update
RUN apt-get -y install google-chrome-stable
# install chromedriver
# NOTE: this is a relatively old version. Try a newer version if this does not work.
RUN wget -N http://chromedriver.storage.googleapis.com/2.25/chromedriver_linux64.zip
RUN unzip chromedriver_linux64.zip
RUN chmod +x chromedriver
RUN mv -f chromedriver /usr/local/bin/chromedriver
ENV DISPLAY=:99
ENV XVFB_WHD=1280x720x16
RUN apt-get install -y ca-certificates tzdata
COPY --from=builder /go/bin /bin
COPY /entrypoint.sh /
RUN chmod +x /entrypoint.sh
CMD ["./entrypoint.sh", "/bin/myapp"]
#!/bin/bash
echo "Starting X virtual framebuffer using: Xvfb $DISPLAY -ac -screen 0 $XVFB_WHD -nolisten tcp"
Xvfb $DISPLAY -ac -screen 0 $XVFB_WHD -nolisten tcp &
sleep 3
# # Execute CMD (original CMD of this Dockerfile gets overridden in actor build)
echo "Executing main command"
exec "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment