Skip to content

A lightweight Docker-PHP-MySQL-nginx dev environment for prototyping MVC web apps.

Notifications You must be signed in to change notification settings

islandjoe/dev-environment-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dopmn - a lightweight Docker+PHP+MySQL+nginx dev environment

A quick solution for prototyping MVC web apps with Docker running Nginx, PHP-FPM, Composer, and MySQL.

Dopmn stands on the shoulders of these projects:

Overview

  1. Prerequisites

  2. Installing Dopmn

  3. Initializing the web app framework

  4. Running Dopmn

    Two options while developing:


1. Installation prerequisites

This project has been mainly created for Unix (Linux/MacOS). On Windows, caveat emptor ¯_(ツ)_/¯.

You need to have at least these already installed:

To check if you have docker-compose installed:

    which docker-compose

This is optional but makes for less command typing:

    which make

On Ubuntu and Debian these are available in the meta-package build-essential:

    sudo apt install build-essential

Docker images used


2. To install

$ git clone https://github.com/islandjoe/dopmn.git
$ cd dopmn/

Project tree

.
├── Makefile
├── README.md
├── composer.json
├── data/
│   └── db/
├── docker-compose.yml
├── etc/
│   ├── nginx/
│   └── php/
└── web/
    ├── app/
    └── public/
web
├── app/
│   ├── composer.json.dist
│   ├── phpunit.xml.dist
│   ├── src/
│   └── test/
└── public/
    ├── css/
    ├── img/
    ├── index.php
    └── js/
web/app/src
├── Controller/
│   ├── AbstractController.php
│   ├── ErrorController.php
│   └── HomeController.php
├── Core/
│   ├── Application.php
│   ├── Db.php
│   └── Foo.php
├── View/
│   ├── error/
│   ├── footer.php
│   ├── header.php
│   └── home/
└── config.php

3. Initializing the backend

  1. Create a fresh copy of the composer configuration file: (This is only done one time)

    (dopmn) $ cp web/app/composer.json{.dist,}
  2. Start the application :

    (dopmn) $ docker-compose up -d

    If you want to follow log output:

    (dopmn) $ docker-compose logs -f

4. Initializing the frontend

(dopmn) $ composer install -d web/app

Open in browser :

http://localhost:8000

Ports used

Server Port
MySQL 8989
Nginx 8000

Please refer to Handling database

When you're done developing

To stop the Docker container and remove containers, networks, volumes, and images:

(dopmn) $ docker-compose down -v

Using Makefile

To make less typing command mistakes, you can use make for doing the following:

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

Examples

Start the application :

$ make docker-start

Show help :

$ make help

Using Docker: Some important commands

Installing package with composer

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

Updating PHP dependencies with composer:

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

Testing PHP application with PHPUnit

(dopmn) $ docker-compose exec \
    -T php ./app/vendor/bin/phpunit --colors=always ./app/test/FooTest.php

or to run the whole test suite:

(dopmn) $ docker-compose exec -T php ./app/vendor/bin/phpunit --colors=always --configuration ./app

Generating PHP API documentation

(dopmn) $ 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

Fixing standard code with PSR2

(dopmn) $ docker-compose exec \
    -T php ./app/vendor/bin/phpcbf \
    -v \
    --standard=PSR2 ./app/src

Checking the standard code with PSR2

(dopmn) $ docker-compose exec \
    -T php ./app/vendor/bin/phpcs \
    -v \
    --standard=PSR2 ./app/src

Analyzing source code with PHP Mess Detector

(dopmn) $ docker-compose exec \
    -T php \
    ./app/vendor/bin/phpmd \
    ./app/src \
    text \
    cleancode,codesize,controversial,design,naming,unusedcode

Checking installed PHP extensions

(dopmn) $ docker-compose exec php php -m

Handling database

MySQL shell access

(dopmn) $ docker exec -it mysql bash

and inside the container shell

root@a2c3572c57b9:/#   mysql -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD"

Creating a backup of all databases

(dopmn) $ mkdir -p data/db/dumps
(dopmn) $ 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

(dopmn) $ 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

(dopmn) $ DB_NAME='!!!your-db-name-here!!!'
(dopmn) $ 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/${DB_NAME}_dump.sql"

Restoring a backup of single database

(dopmn) $ DB_NAME='!!!your-db-name-here!!!'
(dopmn) $ 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_NAME}_dump.sql"

Connecting MySQL from PDO

$someDbConnection = Dopmn\Core\Db::getInstance();