Skip to content

Latest commit

 

History

History
150 lines (105 loc) · 5.39 KB

README.md

File metadata and controls

150 lines (105 loc) · 5.39 KB

Docker Laravel (LEMP stack with PHP7, ELK, REDIS)

Build Status

The idea of the project is to provide a pre-configured docker platform for Laravel and Lumen apps to get them up and running fast.

Credit: this is a kind of fork from maxpou/docker-symfony. Thanks to him :-) I have made the number of changes to work with Laravel or Lumen apps and modified platform level commands (artisan, composer, mysql) quite easy to access.

Container Architecture

Installation

I assume you have docker-compose installed and either docker-engine running locally (Linux) or have docker-machine (installed via docker-toolbox on OSX, Windows) configured on the computer you use.

  1. Retrieve git project

    $ git clone git@github.com:purinda/docker-laravel.git
  2. Change directory to the docker-laravel (cd docker-laravel)

  3. Symlink your Laravel/Lumen project into app folder (ln -s <absolute-path-of-laravel-project> app)

  4. Build and start containers in detached mode.

    $ docker-compose up -d
  5. Update your host file (add app.dev)

    # get containers IP address and update host (replace IP according to your configuration )
    $ docker inspect --format '{{ .NetworkSettings.IPAddress }}' $(docker ps -f name=nginx -q)
    $ sudo echo "10.211.55.7 app.dev" >> /etc/hosts

    If you use docker-machine (mainly OSX or Windows) then run the following to get IP address of the vm.

    $ docker-machine ip <machine-name>
    $ echo "10.211.55.7 app.dev" | sudo tee -a /etc/hosts
  6. Prepare Laravel/Lumen app

    1. Update app/.env (adapt hosts according to previous results)

      # Docker database configuration
      DB_CONNECTION=mysql
      DB_DATABASE=laravel
      DB_DATABASE_TEST=laravel_test
      DB_HOST=app.dev
      DB_PORT=3306
      DB_USERNAME=laravel
      DB_PASSWORD=laravel
      
      # Docker SMTP MailCatcher configuration
      MAIL_DRIVER=smtp
      MAIL_HOST=app.dev
      MAIL_PORT=25
      MAIL_FROM_ADDRESS=docker@local
      MAIL_FROM_NAME="Docker"
    2. Composer install

      $ docker-compose exec php composer install
  7. Enjoy :-)

How to use

How it works?

Have a look at the docker-compose.yml file, here are the docker-compose built images:

  • application: This is the Laravel or Lumen application code container,
  • db: This is the MySQL database container,
  • php: This is the PHP-FPM container in which the application volume is mounted,
  • nginx: This is the Nginx webserver container in which application volume is mounted too,
  • elk: This is a ELK stack container which uses Logstash to collect logs, send them into Elasticsearch and visualize them with Kibana,
  • redis: This is a redis database container.

This results in the following running containers:

$ docker-compose ps
           Name                          Command               State              Ports
--------------------------------------------------------------------------------------------------
dockerlaravel_application_1   /bin/bash                        Up
dockerlaravel_db_1            docker-entrypoint.sh mysqld      Up      0.0.0.0:3306->3306/tcp
dockerlaravel_elk_1           /usr/bin/supervisord -n -c ...   Up      0.0.0.0:81->80/tcp
dockerlaravel_nginx_1         nginx                            Up      443/tcp, 0.0.0.0:80->80/tcp
dockerlaravel_php_1           php-fpm                          Up      9000/tcp
dockerlaravel_redis_1         docker-entrypoint.sh redis ...   Up      0.0.0.0:6379->6379/tcp   

Useful commands

# Composer (e.g. composer update)
$ docker-compose exec php composer update

# Laravel Artisan commands (make sure `artisan` script in the app has executable permissions bit set)
$ docker-compose exec php ./artisan

# bash commands
$ docker-compose exec php /bin/bash

# MySQL commands
$ docker-compose exec db mysql -uroot -p"toor" laravel

# Redis commands
$ docker-compose exec redis redis-cli

# Cache/logs folder permissions (use open permissions for development only)
$ chmod -R 777 app/storage/cache app/storage/logs

Running PHPUnit

In order to run the application test suite, it is required to run phpunit which runs against the test database of the application layer.

The script setup-phpunit.sh sets up the test database in the db container and grants required permission for the application test suite.

Script needs docker-machine environment variables (docker-machine env <machine-name>) to be set if you are running containers on docker-machine. Run the script on docker-laravel project directory as below. The script will copy itself into the db container and set up the required configuration.

./setup-phpunit.sh

Lastly, run the unit test suite

docker-compose exec php php vendor/bin/phpunit