Skip to content

Rihoj/docker-full-stack

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Full Web Stack W\ Docker Build Status GitHub version

This was origionally a clone of Nanoninja's docker-nginx-php-mysql. A docker-compose setup that was developed to have a full web stack for developers to get up an running quickly. This project is still in development, but for the most part is good to go. This is by no means a lightweight stack; it was designed to removie the heavy lifting

Images Used

  • Nginx w\ Node
    • This is the web server which will host your code.
  • PHP-FPM
    • PHP server.
  • Composer
    • Composer for all your dependencies.
  • MailHog
    • Gotta trap them all (emails that is).
  • PHPMyAdmin
    • Easy GUI access for mysql.
  • MySQL
    • MySQL server.
  • Redis
    • Redis server (I use this for cache and sessions)
  • LocalStack
    • AWS for local development.
    • Currently I am only supporting S3 and SQS.
  • AWS-CLI
    • CLI to access localstack.
    • For the endpoint please use the LOCALSTACK_HOST from the .env
  • DNS Proxy Server
    • Used in conjunction with the nginx proxy server to allow easy urls.
    • During load and shutdown of this service you may see a small hiccup in your network connection. Please see Troubleshooting
    • If not shutdown correctly this may interrupt your network connection. Please see Troubleshooting
  • Nginx
    • Used as a proxy server.
    • Works with DNS proxy server to add SSL and hide ports on many services.
  • Generate Certificate
    • Used to generate SSL certs.

Overview

  1. Install prerequisites

    Before installing project make sure the following prerequisites have been met.

  2. Clone the project

    We’ll download the code from its repository on GitHub.

  3. Setup Env

    This docker compose file works heavily off of the .env file. We will get it setup and ready to go.

  4. Setup SSL Certificates [Required]

    We'll generate and configure SSL certificate for nginx before running server.

  5. Configure Xdebug [Optional]

    We'll configure Xdebug for IDE (PHPStorm or Netbeans).

  6. Run the application

    By this point we’ll have all the project pieces in place.

  7. Use Makefile [Optional]

    When developing, you can use Makefile for doing recurrent operations.

  8. Use Docker Commands

    When running, you can use docker commands for doing recurrent operations.


Install prerequisites

For now, this project has been mainly created for Unix (Linux/MacOS).

Tested and approved OSes

  1. Linux Mint 18.1 Serena

I will try to test and add more OSes to this list, but I can not verify any MacOS. If you can, please create a PR updating this list.

All requisites should be available for your distribution. The most important are :

Check if docker-compose is already installed by entering the following command :

which docker-compose

Check Docker Compose compatibility :

The following is optional but makes life more enjoyable :

which make

On Ubuntu and Debian these are available in the meta-package build-essential. On other distributions, you may need to install the GNU C++ compiler separately.

sudo apt install build-essential

Clone the project

To install Git, download it and install following the instructions :

git clone https://github.com/rihoj/docker-full-stack.git

Go to the project directory :

cd docker-full-stack

Setup the environment

There are three steps to setting up your environment

  1. Edit /etc/hosts file

    # Add the following line to the file.
    127.0.0.2       rp.io

    It is important to know what you are doing in this file. The rp.io can be changed to your preferred domain. This will be referenced in the .env file.

  2. Edit .env file

    I have tried to explain everything in the env. Please see that file.

  3. Update ./conf/dns/config.json

    If you have changed the $ROOT_DOMAIN you will need to update the DNS configuration. Hopefully soon this will be configured through the .env


Setup SSL Certificates

  1. Generate SSL certificates

    source .env && sudo docker run --rm -v $(pwd)/conf/ssl:/certificates -e "SERVER=*.$ROOT_DOMAIN" jacoelho/generate-certificate

    or

    make gen-certs

Configure Xdebug

This is a legacy section from Nanoninja. I do not use xdebug and have not been able to test this yet.

If you use another IDE than PHPStorm or Netbeans, go to the remote debugging section of Xdebug documentation.

For a better integration of Docker to PHPStorm, use the documentation.

  1. Get your own local IP address :

    sudo ifconfig
  2. Edit php file etc/php/php.ini and comment or uncomment the configuration as needed.

  3. Set the remote_host parameter with your IP :

    xdebug.remote_host=192.168.0.1 # your IP

Run the application

  1. Start the application :

    sudo docker-compose up -d

    Please wait this might take a several minutes...

    sudo docker-compose logs -f # Follow log output
  2. Open your favorite browser :

    Access your sites with the following logic: https://$X_DOMAIN.$ROOT_DOMAIN

    I.E. for the phpmyadmin service you would to go to: https://phpmyadmin.rp.io

  3. Stop and clear services

    sudo docker-compose down -v

Use Makefile

Some of these are still legacy and may not work at this time.

When developing, you can use Makefile for doing the following operations :

Name Description
apidoc Generate documentation of API
clean Clean directories for reset
code-sniff Check the API with PHP Code Sniffer (PSR2)
composer-up Update PHP dependencies with composer
docker-start Create and start containers
docker-stop Stop and clear all services
gen-certs Generate SSL certificates for nginx
logs Follow log output
mysql-dump Create backup of all databases
mysql-restore Restore backup of all databases
phpmd Analyse the API with PHP Mess Detector
test Test application with phpunit

Known working Makefile Commands

Name Description
gen-certs Generate SSL certificates for nginx
docker-start Create and start containers
docker-stop Stop and clear all services

Warning!

make docker-stop will clear your data.

Examples

Start the application :

sudo make docker-start

Show help :

make help

Use Docker commands

This is a legacy section and may need updated.

Installing package with composer

sudo docker run --rm -v $(pwd)/web/app:/app composer require symfony/yaml

Updating PHP dependencies with composer

sudo docker run --rm -v $(pwd)/web/app:/app composer update

Generating PHP API documentation

sudo docker-compose exec -T php php -d memory_limit=256M -d xdebug.profiler_enable=0 ./app/vendor/bin/apigen generate app/src --destination ./app/doc

Testing PHP application with PHPUnit

sudo docker-compose exec -T php ./app/vendor/bin/phpunit --colors=always --configuration ./app

Fixing standard code with PSR2

sudo docker-compose exec -T php ./app/vendor/bin/phpcbf -v --standard=PSR2 ./app/src

Checking the standard code with PSR2

sudo docker-compose exec -T php ./app/vendor/bin/phpcs -v --standard=PSR2 ./app/src

Analyzing source code with PHP Mess Detector

sudo docker-compose exec -T php ./app/vendor/bin/phpmd ./app/src text cleancode,codesize,controversial,design,naming,unusedcode

Checking installed PHP extensions

sudo docker-compose exec php php -m

Handling database

MySQL shell access

sudo docker exec -it mysql bash

and

mysql -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD"

Creating a backup of all databases

mkdir -p data/db/dumps
source .env && sudo docker exec $(sudo docker-compose ps -q mysqldb) mysqldump --all-databases -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" > "data/db/dumps/db.sql"

Restoring a backup of all databases

source .env && sudo docker exec -i $(sudo docker-compose ps -q mysqldb) mysql -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" < "data/db/dumps/db.sql"

Creating a backup of single database

Notice: Replace "YOUR_DB_NAME" by your custom name.

source .env && sudo docker exec $(sudo docker-compose ps -q mysqldb) mysqldump -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" --databases YOUR_DB_NAME > "data/db/dumps/YOUR_DB_NAME_dump.sql"

Restoring a backup of single database

source .env && sudo docker exec -i $(sudo docker-compose ps -q mysqldb) mysql -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" < "data/db/dumps/YOUR_DB_NAME_dump.sql"

Connecting MySQL from PDO

<?php
    try {
        $dsn = 'mysql:host=mysql.io;dbname=test;charset=utf8;port=3306';
        $pdo = new PDO($dsn, 'dev', 'dev');
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
?>

Troubleshoot

DNS

The DNS proxy server edits your local /etc/resolv.conf file. This means that during the start up and shutdown your Internet may see hiccups in connection. This is normal, please bear with it.

If the service does not shut down correctly you may see your internet not work at all. This can be fixed.

Option 1: Edit your local /etc/resolv.conf remove the lines with the comment # dps-entry and uncomment lines with # dps-comment


Help us

I welcome any thoughts, feedback, or bugs! Please create an issue and I will get to it as soon as possible!


TODO

  1. Move the web directory into the repository.

  2. Make DNS config.json be able to use template.

  3. Update makefile with accurate commands

  4. Update documentation.

  5. Custom roll AWS-CLI to not need endpoints.

  6. Update .travis.yml to update tests.

  7. Test more operating systems.

About

Docker running Nginx, PHP-FPM, MySQL & PHPMyAdmin

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Shell 60.7%
  • Makefile 36.4%
  • PHP 2.9%