├── .env.dist ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── app ├── .gitignore └── Dockerfile ├── docker-compose.yml ├── logs └── .gitkeep ├── nginx ├── Dockerfile ├── conf.d │ ├── default.conf │ └── symfony.conf.dist └── conf │ └── nginx.conf ├── php-fpm ├── Dockerfile ├── conf.d │ └── xdebug.ini └── php.ini └── postgresql └── .gitkeep /.env.dist: -------------------------------------------------------------------------------- 1 | # Global 2 | CONTAINER_PREFIX=lekode.lab 3 | 4 | # Ports 5 | NGINX_PORT=80 6 | POSTGRES_PORT=5433 7 | MAIL_DEV_PORT=1080 8 | 9 | # Database (Postgres) 10 | POSTGRES_USER=lekode 11 | POSTGRES_PASSWORD=secret 12 | POSTGRES_DB=lekode 13 | 14 | # Database (MariaDB) 15 | # MYSQL_ROOT_PASSWORD=root.secret 16 | # MYSQL_USER=lekode 17 | # MYSQL_PASSWORD=secret 18 | # MYSQL_DATABASE=lekode 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # env file 2 | .env 3 | 4 | # app directory 5 | /app 6 | !/app/Dockerfile 7 | 8 | # logs 9 | /logs 10 | !/logs/.gitkeep 11 | 12 | # postgreSQL data 13 | /postgresql 14 | !/postgresql/.gitkeep 15 | 16 | # IDEs 17 | /.idea 18 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | 3 | env: 4 | DOCKER_COMPOSE_VERSION: 1.8.1 5 | 6 | services: 7 | - docker 8 | 9 | before_install: 10 | - curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose 11 | - chmod +x docker-compose 12 | - sudo mv docker-compose /usr/local/bin 13 | - cp .env.dist .env 14 | 15 | script: 16 | - docker-compose build 17 | - docker-compose up -d 18 | - docker-compose ps 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Zakariae Filali 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # docker-php 2 | Docker compose to run PHP application, given Symfony configuration as an example. 3 | 4 | [![Build Status](https://travis-ci.org/kariae/docker-php.svg?branch=master)](https://travis-ci.org/kariae/docker-php) ![Contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg) 5 | [![License](https://img.shields.io/badge/license-MIT%20License-brightgreen.svg)](https://opensource.org/licenses/MIT) 6 | ## Installation 7 | 1. Create a `.env` from `.env.dist` file, and customize it according to your preferences. 8 | 2. Update your hosts file according to the [NGINX server configuration](https://github.com/kariae/docker-php/blob/master/nginx/conf.d/lekode.conf#L2) 9 | ```bash 10 | # UNIX 11 | sudo echo "0.0.0.0 lekode.dev" >> /etc/hosts 12 | # Windows (edit C:\Windows\System32\drivers\etc\hosts) 13 | ``` 14 | 3. Build containers 15 | `docker-compose build` 16 | 17 | ## Usage 18 | After building the containers, just run them and you’re ready to go 19 | `docker-compose up -d` 20 | 21 | ## Utils 22 | Here is a list of things that can help you 23 | 24 | ### xDebug 25 | - [x] TODO: How to configure xDebug with a code editor. (an [article](https://lekode.com/2017/12/18/debugging-your-php-application-inside-a-docker-environment-using-xdebug/) that explain how to use it with your IDE.) 26 | 27 | ### Customize containers for different environment 28 | - [x] TODO: Add other useful containers (Redis, phpMyAdmin …) 29 | - [ ] TODO: Customize NGINX for other frameworks than Symfony (Add .conf file per framework) 30 | 31 | ### Execute commands inside containers 32 | You may need to execute commands inside php-fpm container to clear Symfony cache, install fixtures or whatever, to do this, check your container name from running containers list, connect to your container, and execute your command 33 | ```bash 34 | docker ps 35 | docker exec -ti lekode.lab.php sh 36 | php bin/console ... 37 | ``` 38 | 39 | ## Contributing 40 | First, **many thanks** for your contributions, please note that this eco system is a personal preference that I use in most of my PHP projects (using Symfony or other frameworks), if you find any typo/misconfiguration, or just want to optimize more the workflow, please 41 | 1. Fork it! 42 | 2. Create your feature branch: `git checkout -b my-new-feature` 43 | 3. Commit your changes: `git commit -am 'Add some feature'` 44 | 4. Push to the branch: `git push origin my-new-feature` 45 | 5. Submit a pull request :D 46 | 47 | ## History 48 | TODO: Write history 49 | 50 | ## License 51 | This project is licensed under the MIT License - see the [LICENSE](https://github.com/kariae/docker-php/blob/master/LICENSE) file for details 52 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /app/config/parameters.yml 2 | /build/ 3 | /phpunit.xml 4 | /var/* 5 | !/var/cache 6 | /var/cache/* 7 | !var/cache/.gitkeep 8 | !/var/logs 9 | /var/logs/* 10 | !var/logs/.gitkeep 11 | !/var/sessions 12 | /var/sessions/* 13 | !var/sessions/.gitkeep 14 | !var/SymfonyRequirements.php 15 | /vendor/ 16 | /web/bundles/ -------------------------------------------------------------------------------- /app/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM busybox:latest 2 | 3 | MAINTAINER Zakariae Filali 4 | 5 | ADD . /var/www/app 6 | 7 | CMD ["/bin/true"] -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "2" 2 | 3 | services: 4 | postgresql: 5 | image: postgres:alpine 6 | container_name: ${CONTAINER_PREFIX}.postgresql 7 | ports: 8 | - ${POSTGRES_PORT}:5432 9 | volumes: 10 | - ./postgresql/data:/var/lib/postgresql/data 11 | - ./logs/postgresql/:/var/log/postgresql 12 | environment: 13 | - POSTGRES_USER=${POSTGRES_USER} 14 | - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} 15 | - POSTGRES_DB=${POSTGRES_DB} 16 | # mariadb: 17 | # image: mariadb:latest 18 | # container_name: ${CONTAINER_PREFIX}.mariadb 19 | # ports: 20 | # - ${MARIADB_PORT}:3306 21 | # volumes: 22 | # - ./mariadb/data:/var/lib/mysql/data 23 | # - ./logs/mariadb/:/var/log/mysql 24 | # environment: 25 | # - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} 26 | # - MYSQL_USER=${MYSQL_USER} 27 | # - MYSQL_PASSWORD=${MYSQL_PASSWORD} 28 | # - MYSQL_DATABASE=${MYSQL_DATABASE} 29 | app: 30 | build: ./app 31 | container_name: ${CONTAINER_PREFIX}.app 32 | volumes: 33 | - ./app:/var/www/app 34 | php-fpm: 35 | build: ./php-fpm 36 | container_name: ${CONTAINER_PREFIX}.php 37 | volumes_from: 38 | - app 39 | volumes: 40 | - ./php-fpm/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini 41 | - ./php-fpm/php.ini:/usr/local/etc/php/php.ini 42 | nginx: 43 | build: ./nginx 44 | container_name: ${CONTAINER_PREFIX}.nginx 45 | ports: 46 | - "${NGINX_PORT}:80" 47 | volumes_from: 48 | - app 49 | volumes: 50 | - ./nginx/conf/nginx.conf:/etc/nginx/conf/nginx.conf:ro 51 | - ./nginx/conf.d:/etc/nginx/conf.d:ro 52 | - ./logs/nginx/:/var/log/nginx 53 | mailDev: 54 | image: djfarrelly/maildev 55 | container_name: ${CONTAINER_PREFIX}.maildev 56 | ports: 57 | - "${MAIL_DEV_PORT}:80" 58 | # redis: 59 | # image: redis:alpine 60 | # command: ["redis-server", "--appendonly", "yes"] 61 | # hostname: redis 62 | # volumes: 63 | # - redis-data:/data 64 | # phpmyadmin: 65 | # image: phpmyadmin/phpmyadmin 66 | # container_name: phpmyadmin 67 | # environment: 68 | # - PMA_ARBITRARY=1 69 | # restart: always 70 | # ports: 71 | # - 8080:80 72 | # volumes: 73 | # - phpmyadmin:/sessions -------------------------------------------------------------------------------- /logs/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kariae/docker-php/00041e9baceb3cfd211e8a373c3ceeec971eaba0/logs/.gitkeep -------------------------------------------------------------------------------- /nginx/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nginx:alpine 2 | 3 | RUN rm /etc/nginx/conf.d/default.conf 4 | ADD conf/nginx.conf /etc/nginx/nginx.conf 5 | ADD conf.d /etc/nginx/conf.d -------------------------------------------------------------------------------- /nginx/conf.d/default.conf: -------------------------------------------------------------------------------- 1 | server { 2 | server_name lekode.dev; 3 | root /var/www/app; 4 | index index.php index.html index.htm index.nginx-debian.html; 5 | 6 | client_max_body_size 101M; 7 | 8 | location / { 9 | try_files $uri $uri/ =404; 10 | } 11 | 12 | location ~ \.php$ { 13 | fastcgi_pass php-fpm:9000; 14 | fastcgi_split_path_info ^(.+\.php)(/.*)$; 15 | include fastcgi_params; 16 | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 17 | fastcgi_param HTTPS off; 18 | } 19 | 20 | location ~ /\.ht { 21 | deny all; 22 | } 23 | 24 | error_log /var/log/nginx/lekode_error.log; 25 | access_log /var/log/nginx/lekode_access.log; 26 | } -------------------------------------------------------------------------------- /nginx/conf.d/symfony.conf.dist: -------------------------------------------------------------------------------- 1 | server { 2 | server_name lekode.dev; 3 | root /var/www/app/web; 4 | 5 | client_max_body_size 101M; 6 | 7 | location / { 8 | try_files $uri @rewriteapp; 9 | } 10 | 11 | location @rewriteapp { 12 | rewrite ^(.*)$ /app_dev.php/$1 last; 13 | } 14 | 15 | location ~ ^/(app|app_dev)\.php(/|$) { 16 | fastcgi_pass php-fpm:9000; 17 | fastcgi_split_path_info ^(.+\.php)(/.*)$; 18 | include fastcgi_params; 19 | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 20 | fastcgi_param HTTPS off; 21 | } 22 | 23 | error_log /var/log/nginx/lekode_error.log; 24 | access_log /var/log/nginx/lekode_access.log; 25 | } -------------------------------------------------------------------------------- /nginx/conf/nginx.conf: -------------------------------------------------------------------------------- 1 | worker_processes 4; 2 | 3 | events { 4 | worker_connections 2048; 5 | multi_accept on; 6 | use epoll; 7 | } 8 | 9 | pid /var/run/nginx.pid; 10 | 11 | http { 12 | include /etc/nginx/mime.types; 13 | default_type application/octet-stream; 14 | 15 | log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 16 | '$status $body_bytes_sent "$http_referer" ' 17 | '"$http_user_agent" "$http_x_forwarded_for"'; 18 | 19 | sendfile on; 20 | 21 | keepalive_timeout 65; 22 | 23 | gzip on; 24 | gzip_disable "msie6"; 25 | 26 | gzip_vary on; 27 | gzip_proxied any; 28 | gzip_comp_level 6; 29 | gzip_buffers 16 8k; 30 | gzip_http_version 1.1; 31 | gzip_types text/plain text/css application/json application/x-javascript 32 | text/xml application/xml application/xml+rss text/javascript; 33 | 34 | include /etc/nginx/conf.d/*.conf; 35 | } -------------------------------------------------------------------------------- /php-fpm/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM php:7.4-fpm-alpine 2 | 3 | MAINTAINER Zakariae Filali 4 | 5 | ENV WORKDIR "/var/www/app" 6 | 7 | RUN apk upgrade --update && apk --no-cache add \ 8 | gcc g++ make git autoconf tzdata openntpd libcurl curl-dev coreutils \ 9 | freetype-dev libxpm-dev libjpeg-turbo-dev libvpx-dev \ 10 | libpng-dev ca-certificates libressl libressl-dev libxml2-dev postgresql-dev icu-dev libzip-dev 11 | 12 | RUN docker-php-ext-configure intl \ 13 | && docker-php-ext-configure opcache \ 14 | && docker-php-ext-configure gd --with-freetype --with-jpeg 15 | 16 | RUN docker-php-ext-install -j$(nproc) gd pdo_pgsql pdo_mysql \ 17 | xmlrpc zip bcmath intl opcache 18 | 19 | # Install Xdebug and Redis 20 | RUN docker-php-source extract \ 21 | && pecl install xdebug-alpha redis \ 22 | && docker-php-ext-enable xdebug redis \ 23 | && docker-php-source delete 24 | 25 | # Add timezone 26 | RUN ln -s /usr/share/zoneinfo/UTC /etc/localtime && \ 27 | "date" 28 | 29 | # Install composer 30 | RUN curl -sS https://getcomposer.org/installer | \ 31 | php -- --install-dir=/usr/local/bin --filename=composer 32 | 33 | # Cleanup 34 | RUN rm -rf /var/cache/apk/* \ 35 | && find / -type f -iname \*.apk-new -delete \ 36 | && rm -rf /var/cache/apk/* 37 | 38 | RUN mkdir -p ${WORKDIR} 39 | 40 | WORKDIR ${WORKDIR} 41 | 42 | EXPOSE 9000 43 | 44 | CMD ["php-fpm"] 45 | -------------------------------------------------------------------------------- /php-fpm/conf.d/xdebug.ini: -------------------------------------------------------------------------------- 1 | xdebug.default_enable=1 2 | xdebug.remote_enable=1 3 | xdebug.remote_handler=dbgp 4 | ; port 9000 is used by php-fpm 5 | xdebug.remote_port=9001 6 | xdebug.remote_host=10.254.254.254 7 | xdebug.remote_autostart=0 8 | ; no need for remote host 9 | xdebug.remote_connect_back=0 10 | xdebug.idekey="sublime.xdebug" 11 | xdebug.remote_log="/tmp/xdebug_log/xdebug.log" -------------------------------------------------------------------------------- /php-fpm/php.ini: -------------------------------------------------------------------------------- 1 | short_open_tag = Off 2 | magic_quotes_gpc = Off 3 | register_globals = Off 4 | session.auto_start = Off 5 | 6 | upload_max_filesize = 100M 7 | post_max_size = 100M 8 | max_file_uploads = 20 9 | 10 | max_execution_time = 30 11 | max_input_time = 60 12 | memory_limit = "512M" -------------------------------------------------------------------------------- /postgresql/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kariae/docker-php/00041e9baceb3cfd211e8a373c3ceeec971eaba0/postgresql/.gitkeep --------------------------------------------------------------------------------