Giter Club home page Giter Club logo

chrome-in-docker's Introduction

google-chrome in docker

Need to test some latest Chrome version's features? but hestitant to upgrade your main browser to unstable? this chrome-in-docker project can help you

Features

Usage

You may either just pull my prebuilt docker image at https://hub.docker.com/r/c0b0/chrome-stable/

$ docker pull c0b0/chrome-stable
$ docker run -it --rm c0b0/chrome-stable /opt/google/chrome/google-chrome --version
Google Chrome 52.0.2743.116

Or build it locally with Dockerfile here

$ docker build -t chrome-stable:20160813 .

Check what Chrome version is builtin, and tag a version:

$ docker run -it --rm chrome-stable:20160813 /opt/google/chrome/google-chrome --version
Google Chrome 52.0.2743.116
$ docker tag chrome-stable:20160813 chrome-stable:52.0.2743.116

The extra get-latest-chrome.sh script here is to get latest versions of Chrome Stable, Beta, or Unstable version, for testing some latest features, here you may modify the Dockerfile to build a different image with each one, while, since the beta and unstable versions are changing fast, may be updating every week or every day, you don't have to rebuild docker images everyday, with this get-latest-chrome.sh and local volume bind, you can run a different container with the same image; that way, within a relatively longer time range you don't have to rebuild the base docker image; the reasons of a same base image can be reused is dependencies of the different channels (stable, beta, or -dev) are most probably the same, or changing much less often; anyway, if there is any problem that stable can run but unstable cannot, you may always have a no-cache rebuild: by docker build --pull --no-cache ... to force pull latest ubuntu base and latest Chrome binary packages.

$ ./get-latest-chrome.sh
[... downloading latest Chrome and extracting to ./opt ...]

You may test run it one time first to check what's exact version of each Chrome channel:

$ docker run -it --rm -v $PWD/opt:/opt:ro chrome:20160813 \
                         /opt/google/chrome-unstable/google-chrome-unstable --version
Google Chrome 54.0.2824.0 dev

$ docker run -it --rm -v $PWD/opt:/opt:ro chrome:20160813 \
                         /opt/google/chrome-beta/google-chrome-beta --version
Google Chrome 53.0.2785.57 beta

$ docker run -it --rm -v $PWD/opt:/opt:ro chrome:20160813 \
                         /opt/google/chrome/google-chrome --version
Google Chrome 52.0.2743.116

Then run 3 different containers with the same base docker image:

$ docker run -dt \
             --name Chrome-dev-54.0.2824.0 \
             -h chrome-dev-54.local \
             -v $PWD/opt:/opt:ro \
             -e CHROME=/opt/google/chrome-unstable/google-chrome-unstable \
         chrome:20160813
56417156ffea4a55642cfa59cf5e9758a2be144144b2df39e91aa9265f098b75
$ docker run -dt \
             --name Chrome-beta-53.0.2785.57 \
             -h chrome-beta-53.local \
             -v $PWD/opt:/opt:ro \
             -e CHROME=/opt/google/chrome-beta/google-chrome-beta \
         chrome:20160813
d5b784cbe9ac7d3a52b43c7fb6918b28366c8b939293b10fb9b1808de7b46e2e
$ docker run -dt \
             --name Chrome-stable-52.0.2743.116 \
             -h chrome-beta-52.local \
             -v $PWD/opt:/opt:ro \
             chrome:20160813
35974a5247cf8650da25d03d9f279749ae4cf1e5b0c57349af1d511b8ac99545

$ docker ps -a
CONTAINER ID  IMAGE            COMMAND      CREATED  STATUS  PORTS  NAMES
35974a5247cf  chrome:20160813  "/entry.sh"  ...                     Chrome-stable-52.0.2743.116
d5b784cbe9ac  chrome:20160813  "/entry.sh"  ...                     Chrome-beta-53.0.2785.57
56417156ffea  chrome:20160813  "/entry.sh"  ...                     Chrome-dev-54.0.2824.0

To connect the chrome in docker, you may either use port mappings, let it call proper iptables to set up proper mappings; or use inspect to find out the ip addresses of each container:

$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' Chrome-dev-54.0.2824.0
172.18.0.4

That means the chrome browser's Chrome Debugging Protocol can be accessed by 172.18.0.4:9222

$ curl -s 172.18.0.4:9222/json/version
{
   "Browser": "Chrome/54.0.2824.0",
   "Protocol-Version": "1.1",
   "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2824.0 Safari/537.36",
   "WebKit-Version": "537.36 (@facabd3224aecbcab4bea9daadad31c67488d78c)"
}

Or, if you use docker port mapping, like:

         #  this one is not using any local volume binding on /opt, so it's using the builtin Chrome at build time,
$ docker run -dt \
             --name Chrome-stable-builtin-52.0.2743.116 \
             -h chrome-stable-52.local \
             -p 9222:9222 \
         chrome:20160813
e9a3738f2d642e5d1a4dd895750d1a09ddece3dd187c82309ade99e1b4123027
$ docker ps -a
CONTAINER ID  IMAGE            COMMAND      CREATED        STATUS         PORTS                     NAMES
e9a3738f2d64  chrome:20160813  "/entry.sh"  3 seconds ago  Up 3 seconds   0.0.0.0:9222->9222/tcp   Chrome-stable-builtin-52.0.2743.116

        # by inspect we know we can access this container by 172.18.0.2:9222
$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' Chrome-stable-builtin-52.0.2743.116
172.18.0.2
$ curl -s 172.18.0.2:9222/json/version
{
   "Browser": "Chrome/52.0.2743.116",
   "Protocol-Version": "1.1",
   "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36",
   "WebKit-Version": "537.36 (@9115ecad1cae66fd5fe52bd9120af643384fd6f3)"
}
        # by above port mapping, this container can also be accessed by 0.0.0.0:9222; if it's from localhost Linux, 
$ curl -s localhost:9222/json/version
{
   "Browser": "Chrome/52.0.2743.116",
   "Protocol-Version": "1.1",
   "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36",
   "WebKit-Version": "537.36 (@9115ecad1cae66fd5fe52bd9120af643384fd6f3)"
}

You may try https://github.com/cyrus-and/chrome-har-capturer with more har capturing commands like chrome-har-capturer -t 172.18.0.2 urls...

Debugging

VNC session listens default on the container's 5900 port, if you figured out the container's IP address (by above inspect command), an VNC session can be opened by your favorite VNC client connect to this ip address, or you may use another -p localport:5900 to set up another port forwarding to be able to use it from a 3rd computer.

Env variables to customize

  1. the default VNC password is hola; you may pass additional env var to docker run by -e VNC_PASSWORD=xxx to change to use a different VNC password;
  2. the default CHROME is /opt/google/chrome/google-chrome, if you use local volume bind to have different chrome versions, you may pass additional env var by -e CHROME=/path/to/chrome or chromium

Design

Docker Image Build Time

  1. The Dockerfile defined process of where as start, it's starting from latest

  2. Ubuntu as base image, then install VNC and some network utilties like curl and socat, xvfb, x11vnc as Graphic layer for Chrome graphical output, xterm as debugging term window supervisor as processes manager, sudo also for debugging, not technically required.

  3. Then add Google-Chrome's apt source and install google-chrome-stable version, and it will handle all runtime dependencies by Chrome; This static version will be packed as part of the docker image, when you're not using local volume bind, this version will be used. It depends how often do you rebuild, but with above ./get-latest-chrome.sh script, you don't have to rebuild very often.

  4. Then add a regular user at 1000:100 for improved security and run all services under this regular user; sudo can be used for debugging. Copying supervisord.conf as definition of process structure; and entry.sh as container entrypoint.

Container Spawn

At container spawn time (docker run ...), it starts from the entrypoint entry.sh there it handles default VNC password hola, and check CHROME environment, set it default to the stable version /opt/google/chrome/google-chrome;

Then it exec to supervisord to spawn more processes defined in supervisord.conf

Process Management

Supervisord is the process manager, it spawns 4 processes:

  1. Xvfb ... as X server
  2. x11vnc ... as VNC on top of X Server
  3. fluxbox as window manager, this is technically not required, any X11 application can directly run on X server, but with a window manager, it's easier for debugging, when need to move window, resize, maximize, and minimize, etc.
  4. xterm, same for debugging
  5. start chrome from CHROME environment variable, with --remote-debugging-port=19222 to enable Remote Debugging Protocol
  6. socat, as a forwarding channel, chrome can only listen on local loopback interface (127.0.0.1); hence not accepting any request from outside so a tcp forwarding tool like socat is necessary here.

Supervisord will respawn any managed processes if it crashed.

Ideally here should define dependencies between the processes, but due to Supervisor/supervisor#122 it lacks such feature.

Some further improvements

chrome-in-docker's People

Contributors

c0b avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

chrome-in-docker's Issues

Can not access Chrome in the container

Trying your image:

$ docker run -it --rm --name chrome c0b0/chrome-stable
+ VNC_STORE_PWD_FILE=/tmp/.vnc/passwd
+ '[' '!' -e /tmp/.vnc/passwd -o -n '' ']'
+ mkdir -vp /tmp/.vnc
mkdir: created directory '/tmp/.vnc'
+ x11vnc -storepasswd hola /tmp/.vnc/passwd
stored passwd in file: /tmp/.vnc/passwd
+ export CHROME=/opt/google/chrome/google-chrome
+ CHROME=/opt/google/chrome/google-chrome
+ sudo chown -Rv 2000:100 /tmp/chrome-data
changed ownership of '/tmp/chrome-data' from root:root to 2000:100
+ exec supervisord
2017-01-26 04:40:59,254 INFO supervisord started with pid 1
2017-01-26 04:41:00,258 INFO spawned: 'x11vnc' with pid 13
2017-01-26 04:41:00,261 INFO spawned: 'fluxbox' with pid 14
2017-01-26 04:41:00,265 INFO spawned: 'chrome' with pid 15
2017-01-26 04:41:00,268 INFO spawned: 'xterm' with pid 16
2017-01-26 04:41:00,270 INFO spawned: 'xvfb' with pid 17
2017-01-26 04:41:00,271 INFO spawned: 'socat' with pid 19
2017-01-26 04:41:00,289 INFO exited: x11vnc (exit status 1; not expected)
2017-01-26 04:41:00,377 INFO exited: xterm (exit status 1; not expected)
2017-01-26 04:41:02,015 INFO spawned: 'x11vnc' with pid 39
2017-01-26 04:41:02,016 INFO success: fluxbox entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2017-01-26 04:41:02,016 INFO success: chrome entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2017-01-26 04:41:02,018 INFO spawned: 'xterm' with pid 40
2017-01-26 04:41:02,019 INFO success: xvfb entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2017-01-26 04:41:02,019 INFO success: socat entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2017-01-26 04:41:03,078 INFO success: x11vnc entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2017-01-26 04:41:03,078 INFO success: xterm entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)

But there is no response:

$ docker ps
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS               NAMES
300fc8ce982e        c0b0/chrome-stable   "/entry.sh"         10 minutes ago      Up 10 minutes                           chrome
$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' chrome
172.17.0.2
$ curl 172.17.0.2:9222/json/version

Host: macOS Sierra 10.12.2

Launching Chrome during build

Hi, thanks for this repo.

I've been struggling to find an example of a unique use case that I have. I'd like to download and launch chrome during my build process. I don't really care about the image created, all I care about is launching chrome during the build process and performing some e2e tests with my project. As long as they are successful and a build is created for my project (i.e. an image has been successfully created) - that's all I need to know.

Given this use case and the dockerfile in this project, is it even possible to launch chrome during the build process itself? If so, how would I do that?

  • Also sorry this is not really an issue with your project. But this is the closest solution I have found atm and was wondering if you were able to provide any advice given my situation. Thank you!

Google-chrome crashes with newer version

I am not 100% sure it is what subject states. But I have strong suspicion that with Ubuntu 16.04 and Google Chrome 60 or better entry point is unsuccessful with starting a browser. It crashes. There is plenty of similar reports across the internet and it seems that problems first started with version 56. I am not sure how to downgrade Google Chrome, thus cannot verify it, so please treat this issue as head up and nothing more

Does This Still Work?

Hi,

I've followed the instructions, and I don't seem to be getting anything.

I've tried pulling it:

$ docker pull c0b0/chrome-stable
$ docker run -it --rm c0b0/chrome-stable /opt/google/chrome/google-chrome --version Google Chrome 52.0.2743.116

I've also tried downloading the code and building it locally on my system.

$ docker build -t chrome-stable:20181022 .
$ docker run -it --rm chrome-stable:20181022 /opt/google/chrome/google-chrome --version Google Chrome 52.0.2743.116
$ docker tag chrome-stable:20181022 chrome-stable:52.0.2743.116

When I try to run it, all I get is

Google Chrome 70.0.3538.67
Then the command prompt.

ray@sys-2016-07-xu:~/z32_MISC/chrome-in-docker-master$ docker run -it --rm chrome-stable:20181022 /opt/google/chrome/google-chrome --version Google Chrome 52.0.2743.116
Google Chrome 70.0.3538.67 
ray@sys-2016-07-xu:~/z32_MISC/chrome-in-docker-master$

ray@sys-2016-07-xu:~/z32_MISC/chrome-in-docker-master$ docker tag chrome-stable:20181022 chrome-stable:52.0.2743.116


ray@sys-2016-07-xu:~/z32_MISC/chrome-in-docker-master$ docker images
REPOSITORY           TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
chrome-stable        20181022            d0694f796f61        About a minute ago   738.5 MB
chrome-stable        52.0.2743.116       d0694f796f61        About a minute ago   738.5 MB
hello-world          latest              767f6a55187c        11 months ago        1.848 kB
ubuntu               latest              523bc6715b0a        11 months ago        122.8 MB
nginx                1.13                90ccaf7acdc9        11 months ago        108.4 MB
alpine               latest              e878ead083f2        11 months ago        3.966 MB
nginx                latest              c84ebc83ca5b        12 months ago        108.4 MB
mobydock_mobydock    latest              6dcd69a12a3b        13 months ago        589.5 MB
ruby                 2.3-slim            f8c20eadf71c        13 months ago        276.7 MB
busybox              latest              bfb79db4e2ab        13 months ago        1.129 MB
c0b0/chrome-stable   latest              cf015844d24b        2 years ago          659.7 MB
postgres             9.4.5               ff155793d84e        2 years ago          263.1 MB
redis                2.8.22              579cf374438d        3 years ago          109 MB
ray@sys-2016-07-xu:~/z32_MISC/chrome-in-docker-master$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

I read the comments within "Google-chrome crashes with newer version", and I removed benchmarking:

it could be due to --enable-benchmarking or --enable-net-benchmarking you may drop both

My supervisord.conf now looks like this:

[supervisord]
nodaemon=true

[program:xvfb]
command=Xvfb :10 -screen 0 1920x1480x24+32 -ac -r -cc 4 -accessx -xinerama +extension Composite -extension RANDR +extension GLX

[program:x11vnc]
command=x11vnc -rfbport 5900 -display :10 -rfbauth /tmp/.vnc/passwd -forever -shared

[program:fluxbox]
command=fluxbox -display :10

[program:xterm]
command=xterm -display :10

[program:chrome]
command=%(ENV_CHROME)s --no-first-run
                       --user-data-dir=./chrome-data
                       --force-device-scale-factor=2
                       --remote-debugging-port=19222
environment=DISPLAY=":10"

[program:socat]
command=/usr/bin/socat -v -d tcp-listen:9222,reuseaddr,fork tcp:localhost:19222

I'm a noob with Docker, so I'm wondering if perhaps I am doing something wrong.

I'm running this in Ubuntu in VirtualBox on a system running Windows 10.

Can you verify that this does, indeed, still work?

THANKS!

on some Linux systems, may need bind mount /dev/shm

on some Linux systems, sometimes see Chrome browser may crash one page or two, seems happening randomly, not exactly sure why some page works some not, but since I know Chrome on Linux using a lot of large page memory mapping, which is a tmpfs mounting on /dev/shm on some Linux variants, and it depends on which Linux, the docker run instance may use the default 64MB, that may not be enough in some cases, you may set a different --shm-size 1024MB or just share /dev/shm from host by -v /dev/shm:/dev/shm on docker run lines

$ docker run --help | grep shm
      --shm-size string             Size of /dev/shm, default value is 64MB
$ df -Th /dev/shm
Filesystem     Type   Size  Used Avail Use% Mounted on
tmpfs          tmpfs  3.9G  231M  3.7G   6% /dev/shm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.