Dockervel: A Laravel development environment in Docker




A complete Laravel development environment in Docker Containers:

  • Bring Up a Laravel development environment with a single command easy as dup
  • Execute Artisan commands easy as dartisan make:auth
  • Run Composer command easy as dcomposer update
  • Run NodeJS script easy as dnodejs npm install
  • Run gulp watch easy as dgulp-watch
  • Containers managed with docker-compose
  • Lightweight as it is mostly based in Linux Alpine base image of 5MB.


Containers included

  • Nginx 1.8.0 & PHP 5.6 running in Alpine
  • MariaDB 5.5 running in Alpine
  • Redis running in Alpine
  • Composer PHP running in Alpine.
  • Artisan running in Debian (alpine is missing some PHP packages)
  • NodeJS with gulp, grunt and bower -g installed in official node:4.2.2-slim base image
  • Volumes mounted as containers

Usage

Make sure you have docker and docker-compose installed. See here for installing them
  • clone this repo: git clone https://github.com/SpiralOutDotEu/dockervel.git
  • cd in: cd dockervel
  • run as su: su
  • add aliases: . ./aliases.sh
  • run: dup and you have a server running! Hit localhost in your browser and you will see nginx fault message becasuse there is no www/public/index.php.
  • create new Laravel project: dcomposer-create
  • fix permissions: dpermit
  • change .env: see below for options
  • run artisan commands: dartisan make:auth
  • fix permissions: dpermit Now you have a registration system active. Go to localhost and register a new user to see that db's are running ok.
  • npm install: dnodejs npm install
  • gulp install: dnodejs gulp install
  • gulp watch: dulp-watch Now there is one container running gulp watch and monitors changes on files according your gulpfile.js

aliases

aliases.sh contains shortcuts to common commands. run dot space dot /aliashes.sh to activate aliases for this terminal session.
$ . ./aliases.sh
and now for this terminal session you have aliases like dartisan, dcomposer, dnodejs, dup, dstop.
If you don't want to work with aliases, open the script and see the coressponding commands next to each alias. In the following document it is supposed that you have executed aliashes.sh as su and you have the aliases active.

Create new Laravel Project

  • create new laravel project: dcomposer-create
  • type localhost in your browser and you see the Laravel welcome screen

Fix permitions

since containers have different user in them, you have to change the permisions in www/ folder to be able to write. type:
$ dpermit
it will simply chmod -R 777 www Remember to run dpermit after each time the www folder has a new file.

Configure Laravel for mysql

change DB_HOST in .env to point to mysql. This is the name that it is used in docker-compose.yml (link: -mysql:mysql)
DB_HOST=mysql
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

Configure Laravel for redis

change REDIS_HOST in .env to point to predis. This is the name that it is used in docker-compose.yml (link: -redis:predis).
REDIS_HOST=predis
REDIS_PASSWORD=null
REDIS_PORT=6379

in Laravel 5.2 there are some issues with redis namespace, so to avoid conflicts predis is used. Also you have to change /config/app.php, and replace:
'Redis'     => Illuminate\Support\Facades\Redis::class,
with
'LaravelRedis'     => Illuminate\Support\Facades\Redis::class,
Use it like:
$redis= LaravelRedis::connection();
$redis->set('name', 'myname');
$redis->get('name'); 

Running Artisan commands

type dartisan followed by an artisan command
example:
$ dartisan make:auth
$ dartisan migrate
$ dartisan tinker

Running composer commands

type dcomposer followed by a composer command
example:
$ dcomposer update
$ dcomposer require predis/predis

Nodejs

to install node modules in your project run
$ dnodejs npm install

Gulp

install gulp with:
dnodejs gulp install:
run gulp once:
dgulp
add gulp watch:
dgulp-watch

Size

The size of images is about 750 MB:
* front:     70.05 MB
* mysql:    199.50 MB
* redis:      8.50 MB
* composer: 236.80 MB
* nodejs:    36.09 MB
* artisan   208.80 MB
The base memory usage for the containers of the server running is about 110 MB.
* front: 18 MB
* mysql: 97 MB
* redis:  1 MB

Credits

Thanx to:

37 comments:

  1. Nice work Man, I was working on something similar over the weekend.

    ReplyDelete
  2. Nice work Man, I was working on something similar over the weekend.

    ReplyDelete
    Replies
    1. Glad i've helped. I'm working on adding PHP Unit right now, keep in touch.
      GitHub
      Twitter

      Delete
  3. I'd prefer to install the images via Docker Hub instead of using git clone. Would you make images there?

    ReplyDelete
    Replies
    1. From GitHub you get only the docker-compose-yml(and asistant files). All images are pulled from Docker Hub.

      Delete
  4. Hi there,
    thanks for the setup. Actually I started and tried to install the debug bar (which requires php-iconv), how do I add custom APK packages to the docker instance?

    thank you

    Simom

    ReplyDelete
    Replies
    1. Hi Simom,
      Nice remark about debug bar. I've updated Dockervel so that it supports iconv. Update your local repository and you are ready. Try it out and let me now if it is ok.

      You can add packages to the docker instace by adding them to the docker file.
      See related commit here
      Now change the docker-composer.yml to link to the new image. see related commit here.
      Learn more about automated builds from github to dockerhub here

      Delete
  5. Or you can try a much better solution like LaraDock https://github.com/LaraDock/laradock

    ReplyDelete
  6. I need help. I can't find the .env to make changes.
    DB_HOST=mysql
    DB_DATABASE=homestead
    DB_USERNAME=homestead
    DB_PASSWORD=secret
    Or I have to make the env changes in the docker-compose.yml file

    ReplyDelete
    Replies
    1. .env file is part of you laravel project. It is located at the root of your laravel project. As a "." file it is hidden by default in your file browser. Try to enable the "show hidden files" on your file browser.

      Delete
  7. I need some suggestion . Well I have to make a laravel app using Behat(software for BDD ). Is there any way I can use Behat in this environment?

    ReplyDelete
    Replies
    1. I'm not familiar with Behat but i think that you can install it with `dcomposer require --dev behat/behat=~3.0.4` . A solution to run it from a container might be something like creating dartisan similar dockerfile, remove the entrypoint and use it like a php container. If you added in your docker-compose.yml for example you could call it like `docker-compose run dbehat behat --init`.
      Hope that i've helped

      Delete
    2. Can you help further on the above question? I happen to want to do the same thing. I installed behat with composer.
      I added in the docker-compose.yml:
      behat:
      image: tvial/behat
      volumes:
      - ./www:/var/www
      networks:
      - back-tier
      And I get
      ERROR: for behat No command specified
      I then tried to run:
      docker-compose run www/vendor/behat/behat/bin/behat
      (it's the path to behat) and got
      ERROR: No such service: www/vendor/behat/behat/bin/behat
      Any help appreciated

      Delete
  8. This comment has been removed by the author.

    ReplyDelete
  9. I have gone true the installation ,it's seemed ok. When I run dartisan serve and visit localhost ="The site can't be reached"
    In /var/www/ I have one laravel project that I used to load running also the localhost. What can I change to make this work? In hosts file I have for the use of previous laravel proj. endited this
    127.0.0.1 laravel.example.com .
    I'm sorry if the questions are dumb but I'm a newbie.

    ReplyDelete
    Replies
    1. 1) if you have another web server running, like WAMP, you should stop it because they are trying to use the same ip.
      2) dont forget to change the permitions to www with `dpermit`
      Hope that one of both will do the work

      Delete
  10. Can like run something like this
    php -S localhost:8888 -t public
    in this container?

    ReplyDelete
    Replies
    1. If you want to change the port, change it through `docker-compose.yml` and `port: 8888:80`.

      Delete
  11. I have this docker image pulled ,and worked something on the app ,made some changes and I want to send it to my friend like a docker image. But I'm not sure how to make a dockerfile ,using your dockervel as base .Can you advice me how to put it together?

    ReplyDelete
    Replies
    1. hmm.. First of all, dockervel is not a single image. It consists of sevelar images orchestrated together by docker-compose (see docker-compose.yml). And the reason for that is that you don't need all the parts every time (eg. you don't load nodejs when all you want is to work with artisan). Also you can change parts (eg. you can change MySQL with Postgres).
      If you want to share your project, the easy way is to share the entire dockervel folder (please omit the node_modules folder, you can recreate it with `dcomposer install`).
      If you realy want to make it a single dockerfile (not so good idea) you should combine each dockerfile into a single one and you will end up with a huge and not flexible container.
      You could also try a docker-in-docker, but maybe first read this

      Delete
  12. This comment has been removed by the author.

    ReplyDelete
  13. I read that Post and got it fine and informative. Please share more like that...Great Article it its really informative and innovative keep here:
    web design | laravel development

    ReplyDelete
  14. This comment has been removed by a blog administrator.

    ReplyDelete
  15. Valuable post useful for everyone.Keep on sharing.
    Kubernetes Online Training

    ReplyDelete
  16. IEEE Final Year Project centers make amazing deep learning final year projects ideas for final year students Final Year Projects for CSE to training and develop their deep learning experience and talents.

    IEEE Final Year projects Project Centers in India are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation.

    corporate training in chennai corporate training in chennai

    corporate training companies in india corporate training companies in india

    corporate training companies in chennai corporate training companies in chennai

    I have read your blog its very attractive and impressive. I like it your blog. Digital Marketing Company in Chennai Project Centers in Chennai

    ReplyDelete
  17. The previous is rapidly turning into a favored system for web improvement, while the last is a virtualization technique that utilizes the Docker stage. handgun engraving near me

    ReplyDelete
  18. In order to apply for a residence permit in Latvia based on financial investment in a bank you need to place with a bank at least EUR 280,000 in the form of subordinated capital and pay a state fee of EUR 25 000 to the government. The subordinated capital is funds that the bank is borrowing from a customer for a term of at least 5 years (Like a Corporate Bond – but where the corporation is a bank). http://www.immigration-residency.eu/residence-permit-latvia/bank-deposit/

    ReplyDelete
  19. It could be argued that the immigration process dates back to the very start of the human race. Since the begining of time humans have migrated from place to place in search of a better life and better security for themselves and their families. In the begining the challenges were purely logistical, how to safely travel to your new home, and how to live peacefully with the people and creatures already there. In the modern world there are also legal challenges. Immigration has became a subject of numerous disputes, but for many people it remains an opportunity to reach their goals and provide better security for themselves and their families. http://www.immigration-residency.eu/immigration-to/

    ReplyDelete