PHP Yii2 Docker and Gitlab CI
===============================
    
********************************************************************************
## PHP Yii2 .docker/Dockerfile
********************************************************************************
    FROM php:7.1.8-apache

    MAINTAINER Farid Ahmadian

    #COPY . /srv/app
    COPY .docker/vhost.conf /etc/apache2/sites-available/000-default.conf

    WORKDIR /srv/app

    RUN apt-get update

    RUN apt-get --yes --force-yes install vim git ssh rsync wget\
          openssh-client \
          libfreetype6-dev \
          libjpeg62-turbo-dev \
          libmcrypt-dev \
          libpng12-dev \
          libcurl4-openssl-dev \
          libldap2-dev \
          curl \
          libtidy*

    RUN docker-php-ext-install \
      mcrypt \
      mbstring \
      curl \
      json \
      pdo \
      pdo_mysql \
      exif \
      tidy \
      zip \
        && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
          && docker-php-ext-install gd \
          && docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu \
          && docker-php-ext-install ldap

    RUN chown -R www-data:www-data /srv/app

    RUN a2enmod rewrite

    RUN echo "date.timezone=Europe/Berlin" > $PHP_INI_DIR/conf.d/date_timezone.ini

    RUN wget https://composer.github.io/installer.sig -O - -q | tr -d '\n' > installer.sig
    RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    RUN php -r "if (hash_file('SHA384', 'composer-setup.php') === file_get_contents('installer.sig')) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
    RUN php composer-setup.php
    RUN php -r "unlink('composer-setup.php'); unlink('installer.sig');"
    #RUN php composer.phar install
    RUN mv composer.phar /usr/bin/

********************************************************************************
## .docker/uploads.ini
********************************************************************************
    max_execution_time = 360      ; Maximum execution time of each script, in seconds (I CHANGED THIS VALUE)
    max_input_time = 120          ; Maximum amount of time each script may spend parsing request data
    ;max_input_nesting_level = 64 ; Maximum input variable nesting level
    memory_limit = 128M           ; Maximum amount of memory a script may consume (128MB by default)
    post_max_size = 25M
    upload_max_filesize = 20M
    max_file_uploads = 20

********************************************************************************
## .docker/vhost.conf
********************************************************************************
    <VirtualHost *:80>
        DocumentRoot /srv/app/web

        <Directory "/srv/app/web">
            AllowOverride all
            Require all granted
            # use mod_rewrite for pretty URL support
            RewriteEngine on
            # If a directory or a file exists, use the request directly
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            # Otherwise forward the request to index.php
            RewriteRule . index.php
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>

********************************************************************************
## docker-compose.yml 
********************************************************************************

    version: '3'
    services:
      app:
        container_name: portal
        build:
          context: .
          dockerfile: .docker/Dockerfile
        image: php-portal:latest
        #ports:
        #  - 80:80
        network_mode: "host"
        volumes:
          - .:/srv/app
          - ./.docker/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
        #links:
        #  - mysql
      mysql:
        image: mysql:5.7
        #ports:
        #  - 13306:3306
        network_mode: "host"
        environment:
          MYSQL_DATABASE: yii2basic
          MYSQL_USER: root
          MYSQL_PASSWORD: example
          MYSQL_ROOT_PASSWORD: example
        volumes:
          - ./data/mysql:/var/lib/mysql


********************************************************************************
## .gitlab-ci.yml 
********************************************************************************
    image: docker:stable

    variables:
     # When using dind service we need to instruct docker, to talk with the
     # daemon started inside of the service. The daemon is available with
     # a network connection instead of the default /var/run/docker.sock socket.
     #
     # The 'docker' hostname is the alias of the service container as described at
     # https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#accessing-the-services
     #
     # Note that if you're using Kubernetes executor, the variable should be set to
     # tcp://localhost:2375 because of how Kubernetes executor connects services
     # to the job container
     DOCKER_HOST: tcp://docker:2375/
     # When using dind, it's wise to use the overlayfs driver for
     # improved performance.
     DOCKER_DRIVER: overlay2

    services:
     - docker:dind

    stages:
     - build
     - test
    #   - production

    cache:
      paths:
      - vendor/

    job_build:
      stage: build
      tags:
        - docker
      script:
        # https://docs.gitlab.com/ce/user/project/container_registry.html#build-and-push-images
         - ls
         - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.pathseeker.ir
        #- docker build -t registry.pathseeker.ir/main/php-portal:latest -f .docker/Dockerfile .
        #- docker push registry.pathseeker.ir/main/php-portal:latest
         - docker run -d --name "portal" -v `pwd`:/srv/app/ -v `pwd`/.docker/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini --net "host" registry.pathseeker.ir/main/php-portal:latest
         - docker exec portal pwd
         - docker exec portal ls
         - docker exec portal composer.phar install
         - docker exec portal chown -R www-data:www-data /srv/app

    job_test:
      stage: test
      tags:
        - docker
      when: on_success
      script:
         - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.pathseeker.ir
         - docker run -d --name "portal" -v `pwd`:/srv/app/ -v `pwd`/.docker/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini  --net "host" registry.pathseeker.ir/main/php-portal:latest
         - docker run -d --rm -p 33060:3306 -e MYSQL_DATABASE='yii2_basic_tests' -e MYSQL_ROOT_PASSWORD='Berlin' --name test-mariadb mariadb
         - docker run -d --rm -p 1080:80 -p 1025:25 --name mailcatcher tophfr/mailcatcher
         - docker run --net=host -d --rm --name test-selenium selenium/standalone-chrome
         - sleep 15
         - docker exec portal tests/bin/yii migrate  --interactive 0 &
         - docker exec portal tests/bin/yii serve &
         - docker exec portal tests/bin/yii fixture  --interactive 0 --namespace 'app\tests\fixtures' '*'
         - sleep 5
         - pwd
         - ls
         - docker exec portal vendor/bin/codecept run --steps --xml --html
         - pwd
         - ls
         - ls tests
      artifacts:
        reports:
          junit:
            - tests/_output/report.xml


********************************************************************************
_BY: Farid Ahmadian_  
_TAG: PHP, gitlab, git, gitlab-CI, Dockerfile, docker, docker-compose_  
_DATE: 2018-12-15 10:36:53_