Help us understand the problem. What is going on with this article?

Dockerで構築したLaravel環境でなるべく楽にLaravel Duskを起動する

More than 1 year has passed since last update.

概要

ローカル環境直書きの場合はLaravel Duskはすんなりと起動できるが、Dockerだと多少手間取ったので備忘録として。

環境

  • macOS Mojave
  • Docker for Mac

前提条件

docker-compose.ymlやDockerfileの編集

docker-compose.yml
version: '3'
services:
  https-portal:
    image: steveltn/https-portal:1
    ports:
      - 80:80
      - 443:443
    links:
      - web
    restart: always
    environment:
      DOMAINS: 'localhost -> http://web'
      STAGE: 'local'
      FORCE_RENEW: 'true'
    volumes:
      - org-chimata-ssl-certs:/var/lib/https-portal
      - org-chimata-vhosts:/var/www/vhosts
  # ローカルでも手っ取り早くSSLにするためにsteveltn/https-portalを使用
  web:
    build:
      context: ./
  # 環境に合わせてvolumesなりを追加する

volumes:
  org-chimata-ssl-certs:
  org-chimata-vhosts:
# Dockerfile 諸事情によりUbuntuとApache
FROM ubuntu:16.04

RUN apt-get update && apt-get -y upgrade && apt-get dist-upgrade

RUN apt-get -y install apache2 apache2-utils

RUN apt-get -y install software-properties-common python-software-properties

RUN apt-get -y install language-pack-en
RUN update-locale LANG=en_US.UTF-8
RUN LC_ALL=C.UTF-8 add-apt-repository -y -u ppa:ondrej/php
RUN apt-get -y install php7.2

RUN apt-get -y install libapache2-mod-php7.2 php7.2-bcmath php7.2-bz2 php7.2-cli php7.2-common php7.2-curl php7.2-dba php7.2-gd php7.2-gmp php7.2-imap php7.2-intl php7.2-ldap php7.2-mbstring php7.2-pgsql php7.2-recode php7.2-snmp php7.2-soap php7.2-tidy php7.2-xml php7.2-xmlrpc php7.2-xsl php7.2-zip php7.2-memcached
RUN apt-get update && apt-get -y install zip unzip git

RUN apt-get -y install curl
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer

# nodejs のインストール
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash
RUN apt-get -y install nodejs

RUN a2enmod rewrite

# for Dusk
RUN apt-get update && apt-get -y install libxpm4 libxrender1 libgtk2.0-0 \
    libnss3 libgconf-2-4 chromium-browser \
    xvfb gtk2-engines-pixbuf xfonts-cyrillic \
    xfonts-100dpi xfonts-75dpi xfonts-base \
    xfonts-scalable imagemagick x11-apps \
    fonts-ipafont-gothic fonts-ipafont-nonfree-uigothic

CMD apachectl -D FOREGROUND

WORKDIR /var/www/html

Dockerfileには色々無駄なものが多いですが、開発中で色々必要なため詳細は割愛します。

// DuskTestCase.php
<?php

namespace Tests;

use Laravel\Dusk\TestCase as BaseTestCase;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;

abstract class DuskTestCase extends BaseTestCase
{
    use CreatesApplication;

    /**
     * Prepare for Dusk test execution.
     *
     * @beforeClass
     * @return void
     */
    public static function prepare()
    {
        static::startChromeDriver();
    }

    /**
     * Create the RemoteWebDriver instance.
     *
     * @return \Facebook\WebDriver\Remote\RemoteWebDriver
     */
    protected function driver()
    {
        $options = (new ChromeOptions)->addArguments([
            '--disable-gpu',
            '--headless',
            '--lang=ja_JP', // 日本語化
            '--no-sandbox',
        ]);
        return RemoteWebDriver::create(
            'http://localhost:9515', DesiredCapabilities::chrome()->setCapability(
                ChromeOptions::CAPABILITY, $options
            )
        );
    }
}

# .env
APP_URL='http://web'
# 上記のdocker-compose.ymlの設定の場合はこのように記述
# steveltn/https-portalを使わない場合は'http://localhost/'で問題ないはず

一通り設定が終われば
php artisan duskでDocker環境上でもLaravel Duskが起動できる

参考

Running tests in Laravel Dusk

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away