Laravel dusk使用docker在gitlab中测试 - 无法连接到localhost端口9515:连接被拒绝

时间:2018-05-11 11:22:41

标签: docker gitlab laravel-dusk

在gitlab上的docker容器中运行laravel-dusk时,我收到错误消息,说它无法连接到localhost上的端口9515。

Failed to connect to localhost port 9515: Connection refused

我的docker文件如下所示:

# Set the base image for subsequent instructions
FROM php:7.1

# Replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

# Install packages
RUN apt-get update -yqq && apt-get install -y git wget curl libcurl4-gnutls-dev libicu-dev libmcrypt-dev libvpx-dev libjpeg-dev libpng-dev libxpm-dev zlib1g-dev libfreetype6-dev libxml2-dev libexpat1-dev libbz2-dev libgmp3-dev libldap2-dev unixodbc-dev libpq-dev libsqlite3-dev libaspell-dev libsnmp-dev libpcre3-dev libtidy-dev -yqq bzip2 libfontconfig xvfb chromium libmagickwand-dev

# Add chrome repo and install google-chrome-stable
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
RUN apt-get update
RUN apt-get install -y google-chrome-stable

# Install php environment
RUN docker-php-ext-install mbstring mcrypt pdo_mysql curl json intl gd xml zip bz2 opcache bcmath

# Install imagick
RUN pecl install imagick && docker-php-ext-enable imagick

# check installed modules
RUN php -m

# Install NVM
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 8.1.4
RUN curl --silent -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash

# install node and npm
RUN source $NVM_DIR/nvm.sh \
    && nvm install $NODE_VERSION \
    && nvm alias default $NODE_VERSION \
    && nvm use default

# add node and npm to path so the commands are available
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH

# Install Composer Package manager
ENV COMPOSER_ALLOW_SUPERUSER 1
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && php composer-setup.php \
    php -r "unlink('composer-setup.php');" && mv composer.phar /usr/local/bin/composer

# Update packages
RUN apt-get update

# Xdebug
RUN pecl install -o -f xdebug \
&& rm -rf /tmp/* \ && docker-php-ext-enable xdebug

# Clear out the local repository of retrieved package files
RUN apt-get clean


# Install Laravel Envoy
RUN composer global require "laravel/envoy=~1.0"

# confirm installed versions
RUN node -v
RUN npm -v
RUN php -v
RUN google-chrome-stable --version
RUN composer --version
RUN php -m

EXPOSE 9515

然后我的gitlab-ci文件如下所示:

image: registry.gitlab.com/path-to-repo/repo-name:latest

# Select what we should cache
cache:
    paths:
    - vendor/

services:
- mysql:5.7

variables:
    # Configure mysql service (https://hub.docker.com/_/mysql/)
    MYSQL_DATABASE: database-name
    MYSQL_ROOT_PASSWORD: database-password
    DB_HOST: mysql
    DB_USERNAME: root

stages:
  - test

unit_test:
  stage: test
  script:
    - cp .env.runnable .env
    - composer install
    - php artisan config:clear
    - php artisan key:generate
    - php artisan migrate --seed
    - php artisan db:seed --class=TestSeeder
    - vendor/bin/phpunit --coverage-text --colors=never
    - php artisan env
    - php artisan dusk

起初我以为是因为我没有暴露端口9515,但是我在Dockerfile中暴露了这个。但是,现在我不知道问题是什么。是否存在此端口用于其他进程的问题?可能是Chromedriver在不同的端口上运行吗?

2 个答案:

答案 0 :(得分:2)

在您的gitlab-ci文件中,尝试将php artisan dusk替换为:

php artisan serve & php artisan dusk

答案 1 :(得分:0)

我遇到9515连接被拒绝的错误,然后我发现了这个GitHub issue并将这些依赖项添加到我的dockerfile中(使用自定义ubuntu映像授予了Im)。我不需要添加GitHub注释中提到的其他任何内容(无需脚本等)。

FROM ubuntu:18.04

# ... my other previous dependencies (PHP, etc.)

RUN apt-get update && \
  apt-get -y install libxpm4 libxrender1 libgtk2.0-0 libnss3 libgconf-2-4 && \
  apt-get -y install chromium-browser && \
  apt-get -y install xvfb gtk2-engines-pixbuf && \
  apt-get -y install xfonts-cyrillic xfonts-100dpi xfonts-75dpi xfonts-base xfonts-scalable && \
  apt-get -y install imagemagick x11-apps && \
  apt-get upgrade -y && \
  apt-get clean && \
  rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*

此外,对于本地开发,我需要更新.env(或使用.env.dusk.local)APP_URL:指向我的Docker主机。

APP_URL=host.docker.internal:8000

对于GitLab,您可以通过在before_script中添加其他设置(将.env.example复制到.env之后)来尝试使用Docker主机更新.env APP_URL。 (host.docker.internal在Linux的docker中不可用)。另外,请注意,您的容器将需要net-tools来执行netstat命令。

before_script:
# ...
- docker_host=$(netstat -nr | grep '^0\.0\.0\.0' | awk '{print $2}') && sed -i "s|APP_URL=http://0.0.0.0|APP_URL=http://${docker_host}|" .env

APP_URL=http://0.0.0.0是现有.env文件中已经存在的值。