简体   繁体   中英

docker-compose chrome-alpine to use nginx

I have this docker-compose configuration in which I want to embed a chrome headless browser to run my e2e tests...

The nginx is configured to reverse proxy a url, let's say foo.bar to somewhere... this works just fine if I use my host browser, and I can test it by exposing the 444:443 ports!

however, it doesn't work when I try to use the chrome-alpine one, I navigate to the debugger localhost:9222 , select the instance and then type: http://foo.bar .

How can I configure alpine-chrome to use the nginx container?

services:
  nginx:
    image: nginx:alpine
    command: [nginx-debug, "-g", "daemon off;"]
    volumes:
      - ../../config/nginx/nginx.config.d:/etc/nginx/conf.d/server.conf
      - ../../config/nginx/certs:/etc/nginx/certs
    ports:
      - "444:443"

  chrome:
    image: zenika/alpine-chrome:latest
    command: [chromium-browser, "--headless", "--disable-gpu", "--no-sandbox", "--remote-debugging-address=0.0.0.0", "--remote-debugging-port=9222"]
    ports:
      - "9222:9222"

Expected

  1. Navigate to localhost:9222
  2. Open the Chrome remote debugger
  3. type the url foo.bar
  4. have nginx reverse proxy foo.bar to (eg) google.com

It's a pity I did not find a simple solution after trial, I guess @Mostafa Hussein 's static ip solution is the simplest one to realize the requirement.

The result from my investigation is to alias foo.bar as 127.0.0.1 , and let socat to forward 127.0.0.1 network traffic to mynginx , here, socat could use mynginx which could not be used in extra_hosts:

So, my solution is next, ugly but in case anyone need it later:

version: '3'

services:
  nginx:
    image: nginx:alpine
    container_name: mynginx
    command: [nginx-debug, "-g", "daemon off;"]
    ports:
      - "444:443"

  chrome:
    image: zenika/alpine-chrome:latest
    container_name: mychrome
    command: [chromium-browser, "--headless", "--disable-gpu", "--no-sandbox", "--remote-debugging-address=0.0.0.0", "--remote-debugging-port=9222"]
    ports:
      - "9222:9222"
    extra_hosts:
      - "foo.bar:127.0.0.1"

  helper:
    image: ubuntu:16.04
    volumes:
      - /usr/bin/docker:/usr/bin/docker
      - /var/run/docker.sock:/var/run/docker.sock
      - /usr/lib/x86_64-linux-gnu/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7
    tty: true
    command: bash -c "docker exec -u root -it mychrome /bin/sh -c \"apk add socat && socat TCP4-LISTEN:80,reuseaddr,fork TCP4:mynginx:80\""
    depends_on:
      - chrome

And the test:

shubuntu1@shubuntu1:~/99$ docker exec -it mychrome wget -q -O - http://foo.bar
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

I was able to make it work by defining a static IP for the nginx container like below:

Note: The purpose of setting a static IP for nginx is to avoid any issue that could happen due to ip changes.

version: "2"
services:
  nginx:
    image: nginx:alpine
    command: [nginx-debug, "-g", "daemon off;"]
    ports:
      - 80:80
    networks:
      nginx-chrome-network:
        ipv4_address: 172.28.1.1

  chrome:
    image: zenika/alpine-chrome:latest
    command: [chromium-browser, "--headless", "--disable-gpu", "--no-sandbox", "--remote-debugging-address=0.0.0.0", "--remote-debugging-port=9222"]
    ports:
      - "9222:9222"
    networks:
      - nginx-chrome-network
    extra_hosts:
      foo.bar: 172.28.1.1


networks:
  nginx-chrome-network:
    ipam:
      driver: default
      config:
        - subnet: 172.28.0.0/16

Then by going to localhost:9222 and opening a new tab then typing foo.bar you will get same as the following screenshot:

在此处输入图片说明

you need to use links under your chrome container and call nginx by nginx:port :

links:
   - nginx

or you can use network driver host for both container and call each othe by localhos:port

network: host

the network config shoud be under each service, also see this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM