├── .docker ├── Dockerfile └── conf │ ├── nginx │ └── default.conf │ ├── php │ ├── php.ini │ └── xdebug.ini │ └── postgres │ └── extension-uuid.sql ├── .env ├── LICENSE.md ├── README.md └── docker-compose.yml /.docker/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM php:7.1-fpm 2 | 3 | MAINTAINER Descamps Antoine 4 | 5 | RUN apt-get update && apt-get install -y \ 6 | libfreetype6-dev \ 7 | libjpeg62-turbo-dev \ 8 | libmcrypt-dev \ 9 | libpng-dev \ 10 | libicu-dev \ 11 | libpq-dev \ 12 | libxpm-dev \ 13 | libvpx-dev \ 14 | && pecl install xdebug \ 15 | && docker-php-ext-enable xdebug \ 16 | && docker-php-ext-install -j$(nproc) mcrypt \ 17 | && docker-php-ext-install -j$(nproc) gd \ 18 | && docker-php-ext-install -j$(nproc) intl \ 19 | && docker-php-ext-install -j$(nproc) zip \ 20 | && docker-php-ext-install -j$(nproc) pgsql \ 21 | && docker-php-ext-install -j$(nproc) pdo_pgsql \ 22 | && docker-php-ext-install -j$(nproc) exif \ 23 | && docker-php-ext-configure gd \ 24 | --with-freetype-dir=/usr/include/ \ 25 | --with-jpeg-dir=/usr/include/ \ 26 | --with-xpm-dir=/usr/lib/x86_64-linux-gnu/ \ 27 | --with-vpx-dir=/usr/lib/x86_64-linux-gnu/ \ 28 | -------------------------------------------------------------------------------- /.docker/conf/nginx/default.conf: -------------------------------------------------------------------------------- 1 | # Nginx configuration 2 | 3 | server { 4 | listen 80 default_server; 5 | listen [::]:80 default_server; 6 | server_name localhost; 7 | 8 | root /var/www/html/public; 9 | 10 | location / { 11 | # try to serve file directly, fallback to index.php 12 | try_files $uri /index.php$is_args$args; 13 | } 14 | 15 | location ~ ^/index\.php(/|$) { 16 | fastcgi_pass php:9000; 17 | fastcgi_split_path_info ^(.+\.php)(/.*)$; 18 | include fastcgi_params; 19 | 20 | # optionally set the value of the environment variables used in the application 21 | # fastcgi_param APP_ENV prod; 22 | # fastcgi_param APP_SECRET ; 23 | # fastcgi_param DATABASE_URL "mysql://db_user:db_pass@host:3306/db_name"; 24 | 25 | # When you are using symlinks to link the document root to the 26 | # current version of your application, you should pass the real 27 | # application path instead of the path to the symlink to PHP 28 | # FPM. 29 | # Otherwise, PHP's OPcache may not properly detect changes to 30 | # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126 31 | # for more information). 32 | fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; 33 | fastcgi_param DOCUMENT_ROOT $realpath_root; 34 | # Prevents URIs that include the front controller. This will 404: 35 | # http://domain.tld/index.php/some-path 36 | # Remove the internal directive to allow URIs like this 37 | internal; 38 | } 39 | 40 | # return 404 for all other php files not matching the front controller 41 | # this prevents access to other php files you don't want to be accessible. 42 | location ~ \.php$ { 43 | return 404; 44 | } 45 | 46 | error_log /var/log/nginx/project_error.log; 47 | access_log /var/log/nginx/project_access.log; 48 | } -------------------------------------------------------------------------------- /.docker/conf/php/php.ini: -------------------------------------------------------------------------------- 1 | date.timezone = Europe/Paris 2 | 3 | display_errors = 1 4 | error_reporting = E_ALL -------------------------------------------------------------------------------- /.docker/conf/php/xdebug.ini: -------------------------------------------------------------------------------- 1 | xdebug.remote_enable = 1 2 | xdebug.remote_autostart = 1 3 | xdebug.remote_connect_back = 1 4 | xdebug.remote_idekey = PHPSTORM -------------------------------------------------------------------------------- /.docker/conf/postgres/extension-uuid.sql: -------------------------------------------------------------------------------- 1 | CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; -------------------------------------------------------------------------------- /.env: -------------------------------------------------------------------------------- 1 | DB_NAME=dbname 2 | DB_USER=dbuser 3 | DB_PASSWORD=dbpwd -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2018 INEAT http://ineat-group.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # docker-php-nginx-postgres-composer 2 | Docker Compose configuration to run PHP 7.1 with Nginx, PHP-FPM, PostgreSQL 10.1 and Composer. 3 | 4 | ## Overview 5 | 6 | This Docker Compose configuration lets you run easily PHP 7.1 with Nginx, PHP-FPM, PostgreSQL 10.1 and Composer. 7 | It exposes 4 services: 8 | 9 | * web (Nginx) 10 | * php (PHP 7.1 with PHP-FPM) 11 | * db (PostgreSQL 10.1) 12 | * composer 13 | 14 | The PHP image comes with the most commonly used extensions and is configured with xdebug. 15 | The UUID extension for PostgreSQL has been added. 16 | Nginx default configuration is set up for Symfony 4 (but can be easily changed) and will serve your working directory. 17 | Composer is run at boot time and will automatically install the vendors. 18 | 19 | ## Install prerequisites 20 | 21 | For now the project has been tested on Linux only but should run fine on Docker for Windows and Docker for Mac. 22 | 23 | You will need: 24 | 25 | * [Docker CE](https://docs.docker.com/engine/installation/) 26 | * [Docker Compose](https://docs.docker.com/compose/install) 27 | * Git (optional) 28 | 29 | ## How to use it 30 | 31 | ### Starting Docker Compose 32 | 33 | Checkout the repository or download the sources. 34 | 35 | Simply run `docker-compose up` and you are done. 36 | 37 | Nginx will be available on `localhost:80` and PostgreSQL on `localhost:5432`. 38 | 39 | ### Using Composer 40 | 41 | `docker-compose run composer ` 42 | 43 | Where `cmd` is any of the available composer command. 44 | 45 | ### Using PostgreSQL 46 | 47 | Default connection: 48 | 49 | `docker-compose exec db psql -U postgres` 50 | 51 | Using .env file default parameters: 52 | 53 | `docker-compose exec db psql -U dbuser dbname` 54 | 55 | If you want to connect to the DB from another container (from the `php` one for instance), the host will be the service name: `db`. 56 | 57 | ### Using PHP 58 | 59 | You can execute any command on the `php` container as you would do on any docker-compose container: 60 | 61 | `docker-compose exec php php -v` 62 | 63 | ## Change configuration 64 | 65 | ### Configuring PHP 66 | 67 | To change PHP's configuration edit `.docker/conf/php/php.ini`. 68 | Same goes for `.docker/conf/php/xdebug.ini`. 69 | 70 | You can add any .ini file in this directory, don't forget to map them by adding a new line in the php's `volume` section of the `docker-compose.yml` file. 71 | 72 | ### Configuring PostgreSQL 73 | 74 | Any .sh or .sql file you add in `./.docker/conf/postgres` will be automatically loaded at boot time. 75 | 76 | If you want to change the db name, db user and db password simply edit the `.env` file at the project's root. 77 | 78 | If you connect to PostgreSQL from localhost a password is not required however from another container you will have to supply it. 79 | 80 | ## Adding aliases 81 | 82 | To avoid typing over and over again the same commands you can add two useful aliases in your shell's configuration (`.bashrc` or `.zshrc` for instance): 83 | 84 | ``` 85 | alias dcu="docker-compose up" 86 | alias dcr="docker-compose run" 87 | alias dce="docker-compose exec" 88 | ``` 89 | 90 | It then becomes way faster to execute a composer command for instance: 91 | 92 | `dcr composer require --dev phpunit/phpunit` 93 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | web: 4 | image: nginx 5 | volumes: 6 | - ./.docker/conf/nginx/default.conf:/etc/nginx/conf.d/default.conf 7 | - .:/var/www/html 8 | ports: 9 | - 80:80 10 | restart: always 11 | depends_on: 12 | - php 13 | - db 14 | php: 15 | build: .docker 16 | restart: always 17 | volumes: 18 | - ./.docker/conf/php/php.ini:/usr/local/etc/php/conf.d/php.ini 19 | - ./.docker/conf/php/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini 20 | - .:/var/www/html 21 | composer: 22 | image: composer 23 | volumes: 24 | - .:/app 25 | command: install 26 | db: 27 | image: postgres:10.1 28 | restart: always 29 | environment: 30 | - POSTGRES_DB=${DB_NAME} 31 | - POSTGRES_USER=${DB_USER} 32 | - POSTGRES_PASSWORD=${DB_PASSWORD} 33 | ports: 34 | - 5432:5432 35 | volumes: 36 | - ./.docker/conf/postgres/:/docker-entrypoint-initdb.d/ 37 | --------------------------------------------------------------------------------