docker · docker-compose · php · php7.1 · php-fpm · devops · debuging

PHP 7.1 Docker Compose Dev Environment

If you’re anything like me, you’re always looking for a better way of doing things. If you’re not already using docker on your team yet, especially on a PHP project, you’re doing it wrong.


Here’s what my custom Dockerfile for the php-fpm container looks like:

FROM phpdockerio/php71-fpm:latest

# Install selected extensions and other stuff
RUN apt-get update \
    && apt-get -y --no-install-recommends install php7.1-cli php-memcached php7.1-mysql php7.1-mongodb php-redis php7.1-gd php-imagick php7.1-mbstring php7.1-bcmath php7.1-phpdbg php7.1-intl php-xdebug mysql-client wget ssh git \
    && apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*

#install composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php composer-setup.php
RUN php -r "unlink('composer-setup.php');"
RUN mv composer.phar /usr/local/bin/composer

#install phing
RUN wget https://www.phing.info/get/phing-2.15.2.phar
RUN cp phing-2.15.2.phar /usr/local/bin/phing && chmod +x /usr/local/bin/phing

#install phpdoc
RUN wget https://www.phpdoc.org/phpDocumentor.phar
RUN cp phpDocumentor.phar /usr/local/bin/phpdoc && chmod +x /usr/local/bin/phpdoc

#install phpunit
RUN wget https://phar.phpunit.de/phpunit-6.1.phar
RUN cp phpunit-6.1.phar /usr/local/bin/phpunit && chmod +x /usr/local/bin/phpunit

#xdebug goodness
RUN echo "xdebug.idekey = PHPSTORM" >> /etc/php/7.1/fpm/conf.d/20-xdebug.ini
RUN echo "xdebug.default_enable = 0" >> /etc/php/7.1/fpm/conf.d/20-xdebug.ini
RUN echo "xdebug.remote_enable = 1" >> /etc/php/7.1/fpm/conf.d/20-xdebug.ini
RUN echo "xdebug.remote_autostart = 0" >> /etc/php/7.1/fpm/conf.d/20-xdebug.ini
RUN echo "xdebug.remote_connect_back = 0" >> /etc/php/7.1/fpm/conf.d/20-xdebug.ini
RUN echo "xdebug.profiler_enable = 0" >> /etc/php/7.1/fpm/conf.d/20-xdebug.ini
RUN echo "xdebug.remote_host = 10.254.254.254" >> /etc/php/7.1/fpm/conf.d/20-xdebug.ini

And my docker-compose file looks like this:

version: '2'
services:
  web-app-memcached:
    image: phpdockerio/memcached:latest
    container_name: web-app-memcached
    ulimits:
      memlock:
        soft: -1
        hard: -1
    mem_limit: 256m

  web-app-redis:
    image: phpdockerio/redis:latest
    container_name: web-app-redis
    ulimits:
      memlock:
        soft: -1
        hard: -1
    mem_limit: 512m

  web-app-mysql:
    image: mysql:5.7
    container_name: web-app-mysql
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=dream
      - MYSQL_USER=dev
      - MYSQL_PASSWORD=dev
    ulimits:
      memlock:
        soft: -1
        hard: -1
    mem_limit: 1g
    ports:
      - "3306:3306"

  web-app-elasticsearch:
    image: elasticsearch:5.5
    container_name: web-app-elasticsearch
    environment:
      - http.host=0.0.0.0
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    mem_limit: 3g
    ports:
        - "9200:9200"

  web-app-mongodb:
    image: mongo:3.4
    environment:
      - MONGODB_USER=dev
      - MONGODB_DATABASE=dream
      - MONGODB_PASS=dev
    ulimits:
      memlock:
        soft: -1
        hard: -1
    mem_limit: 1g
    ports:
        - "27017:27017"

  web-app-webserver:
    image: phpdockerio/nginx:latest
    container_name: web-app-webserver
    volumes:
        - ..:/var/www/web-app
        - ./site.conf:/etc/nginx/conf.d/default.conf
    ulimits:
      memlock:
        soft: -1
        hard: -1
    mem_limit: 512m
    ports:
     - "8080:80"
    links:
     - web-app-php-fpm

  web-app-php-fpm:
    build:
      context: .
      dockerfile: php-fpm/Dockerfile
    container_name: web-app-php-fpm
    volumes:
      - ..:/var/www/web-app
      - ~/.ssh:/root/.ssh
      - ./php-fpm/php-ini-overrides.ini:/etc/php/7.1/fpm/conf.d/99-overrides.ini
    expose:
      - "9000"
    environment:
      PHP_XDEBUG_ENABLED: 1 # Set 1 to enable.
      XDEBUG_CONFIG: remote_host=10.254.254.254
    ulimits:
      memlock:
        soft: -1
        hard: -1
    mem_limit: 2g
    links:
      - web-app-memcached
      - web-app-mysql
      - web-app-redis
      - web-app-elasticsearch
      - web-app-mongodb

Published:
comments powered by Disqus