Chrome无法在docker网络内部访问 [英] Chrome not reachable inside docker network
问题描述
我有一个Web应用程序,我正在尝试使用Protractor在Gitlab-CI下进行测试.
I have a web app which I am trying to test under Gitlab-CI using Protractor.
我正在Gitlab-CI中使用docker-in-docker来构建和测试应用程序.
I am using docker-in-docker in Gitlab-CI to build and test the application.
我将所有服务打包在Docker中(一个用于承载静态文件的Nginx服务器,一个Nodejs API,一个Postgresql DB和一个Nginx反向代理,指向"静态文件nginx服务器和nodejs API).
I have all my services containerized in Docker (a Nginx server to host static files, a Nodejs API, a Postgresql DB and a Nginx reverse proxy, "pointing" to the static files nginx server and the nodejs API).
昨天,在运行量角器测试时,一切正常. 然后,我将git分支与另一个分支合并以添加新测试. 在合并过程的中间,我丢失了我的反向nginx代理配置(我误删除了它),必须重新进行配置.我认为新的反向代理配置不是问题所在,但与此同时,这是唯一已更改的事情. 每个测试现在都失败,并显示以下错误:
Yesterday, when running the Protractor tests everything was ok. Then, I have merged my git branch with another to add new tests. In the middle of the merge process, I lost my reverse nginx proxy configuration (I've deleted it by mistake), which I had to remade. I do not think that the new reverse-proxy configuration is the problem, but at the same time, this is the only thing that has changed. Every test is failing now with the error:
WebDriverError: chrome not reachable
e2e_1_511616c9d3bd | (Session info: headless chrome=68.0.3440.106)
e2e_1_511616c9d3bd | (Driver info: chromedriver=2.38 (05121428cd0fc129e40a3694cf5405698236ad14),platform=Linux 4.14.48-coreos-r2 x86_64)
e2e_1_511616c9d3bd | at Object.checkLegacyResponse (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:546:15)
e2e_1_511616c9d3bd | at parseHttpResponse (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/http.js:509:13)
e2e_1_511616c9d3bd | at doSend.then.response (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/http.js:441:30)
e2e_1_511616c9d3bd | at process._tickCallback (internal/process/next_tick.js:68:7)
e2e_1_511616c9d3bd | From: Task: Protractor.get(https://reverseproxy.xyz/) - get url
...
我在gitlab-ci控制台中看到,当测试开始时,浏览器会向nodejs API发出一些成功的请求.
I can see in my gitlab-ci console that when the tests start the browser makes some successful requests to the nodejs API.
我的nginx反向代理配置:
My nginx reverse proxy configuration:
worker_processes auto;
events {
worker_connections 1024;
}
http {
upstream docker-web {
server web:4200;
}
upstream docker-api {
server api:8443;
}
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_ssl_session_reuse off;
server {
server_name localhost;
location / {
proxy_pass https://docker-web;
}
location /api/ {
proxy_pass https://docker-api/;
}
listen 443 ssl;
listen [::]:443 ssl ipv6only=on;
ssl_certificate /etc/nginx/proxy.crt;
ssl_certificate_key /etc/nginx/proxy.key;
ssl_session_cache shared:le_nginx_SSL:1m;
ssl_session_timeout 1440m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers "ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS";
}
}
我的测试在带有以下Dockerfile的高山容器下运行:
My tests are running under an alpine container, with the following Dockerfile:
FROM alpine:latest
RUN sed -i -e 's/v[[:digit:]]\.[[:digit:]]/edge/g' /etc/apk/repositories
RUN apk upgrade --update-cache --available
RUN apk add npm
RUN npm install -g protractor
# chromium dependencies
RUN apk add openjdk8-jre-base
RUN apk add nss
RUN apk add chromium
RUN apk add chromium-chromedriver
RUN apk upgrade --no-cache --available
ENV CHROME_BIN /usr/bin/chromium-browser
RUN mkdir /e2e
COPY . /e2e
CMD protractor /e2e/ci-conf.js
这是我的ci-conf.js
This is my ci-conf.js
exports.config = {
framework: 'jasmine',
chromeOnly: true,
directConnect: true,
specs: ['src/e2e-spec.js'],
chromeDriver: '/usr/bin/chrome',
baseUrl: 'https://reverseproxy.xyz', // reverseproxy.xyz is the name of my reverse proxy docker-compose service
capabilities: {
browserName: 'chrome',
acceptInsecureCerts: true, // I'm currently using self-signed certificates in the reverse-proxy
chromeOptions: {
args: ['headless','no-sandbox', 'disable-gpu', '--window-size=1920,1080']
}
}
}
我的docker-compose,用于初始化应用程序并开始测试:
My docker-compose, which I use to initialize the app and start the tests:
version: '3.7'
services:
reverseproxy.xyz:
image: registry.gitlab.com/projectname/reverseproxy:latest
depends_on:
- web
ports:
- 443:443
web:
image: registry.gitlab.com/projectname/web:42-ci
depends_on:
- api
db:
image: registry.gitlab.com/projectname/db:42-ci
volumes:
- postgres:/var/lib/postgresql/data
api:
image: registry.gitlab.com/projectname/api:42-ci
depends_on:
- db
e2e:
image: e2e:latest
depends_on:
- reverseproxy.xyz
volumes:
postgres:
我的gitlab-ci测试阶段:
My gitlab-ci testing stage:
web_integration_tests:
stage: test
script:
- docker-compose pull
- docker-compose up -d db
- docker-compose up -d api
- docker-compose up -d web
- docker-compose up -d reverseproxy.xyz
- sleep 5
- docker-compose up --exit-code-from
我试图在e2e-spec.js中运行的测试示例:
An example of a test that I'm trying to run inside e2e-spec.js:
describe('Be logged in', function() {
let showAddAccounts = element(by.id('accountIconList'));
let fbLoginBtn = element(by.id('fbLoginBtn'));
beforeEach(function() {
browser.get('');
});
it('Should Login in facebook', function() {
browser.sleep(500);
showAddAccounts.click();
fbLoginBtn.isPresent().then((isPresent) => {
if (isPresent) { // does not appear if already logged in
fbLoginBtn.click().then( () => {
browser.waitForAngularEnabled(false);
browser.getCurrentUrl().then(url => {
browser.driver.findElement(by.id('email')).sendKeys(test_email);
browser.driver.findElement(by.id('pass')).sendKeys(test_pass);
browser.driver.findElement(by.id('loginbutton')).click();
browser.waitForAngularEnabled(true);
});
})
}
});
expect(browser.getCurrentUrl()).toContain(`manage-accounts`);
});
});
这项完全相同的测试在1天前运行良好.
This exactly same test was running well 1 day ago.
我已经搜索了很多这个问题,几乎所有解决方案都指向降级chrome或chromedriver.但是,由于我昨天使用完全相同版本的chrome和chromedriver运行测试,所以我认为这不是问题.
I have searched this issue a lot, and almost all solutions point to downgrade chrome or chromedriver. But, as my tests were running yesterday with exactly the same versions of chrome and chromedriver, I do not think that this is the issue.
我还尝试基于chrome v70.0.3538.67和chromedriver v2.43.600233的node10-stretch使用另一个docker镜像来运行测试,并且有相同的错误.
I have also tried to use another docker image, to run the tests, based on node10-stretch with chrome v70.0.3538.67 and chromedriver v2.43.600233, and had the same error.
当我在不使用docker的情况下在Win10机器上运行这些测试时,所有测试都变为绿色.
When I run these tests in my Win10 machine without using docker, all the tests get green.
任何帮助将不胜感激.
推荐答案
我找到了解决方案. 我必须使用参数shm_size:'2gb'在docker-compose e2e服务中将/dev/shm分区的大小设置为2 gb.
I found the solution. I had to set the size of the /dev/shm partition to 2 gb in my docker-compose e2e service using the argument shm_size: '2gb'.
这篇关于Chrome无法在docker网络内部访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!