Nicolas B.
Nicolas B.

Reputation: 486

Docker compose for Nginx/PHP-FPM/Composer

I am trying to setup Docker for my application relying on an LEMP stack. To this end, I plan to use Docker compose in order to spawn one container for Nginx, one for PHP-FPM and one for MySQL.

This is all working well for the basic illustration use-cases found in the online tutorials, but when I try to apply it to my use-case, I struggle with a design issue.

To give a bit of context, my web application relies on Composer for PHP dependencies and Gulp+Bower for CSS/JS dependencies (and LESS compilation, assets minimization, etc.).

The problem is that I need to build the application (i.e. install all dependencies and run some gulp tasks) and provide the result of this build to both Nginx and PHP-FPM containers.

Here is what I have come to so far:

docker-compose.yml:

version: "3"

networks:
  database:
  server:

volumes:
  mysql-data:
  source:

services:
  php:
    build: .
    volumes:
      - source:/app:ro
    restart: always
    networks:
      - database
      - server
    depends_on:
      - mysql

  mysql:
    image: mysql:5.7
    volumes:
      - mysql-data:/var/lib/mysql
    restart: always
    networks:
      - database
    environment: 
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_USER: test
      MYSQL_PASSWORD: test
      MYSQL_DATABASE: test

  nginx:
    image: nginx
    volumes:
      - source:/app:ro
    restart: always
    networks:
      - server
    depends_on:
      - php

Dockerfile:

FROM php:7.1-fpm
WORKDIR /app

# Install dependencies
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
RUN apt-get update && apt-get install -y \
    git \
    nodejs \
    zip
RUN curl -sL https://getcomposer.org/installer | php -- --install-dir /usr/bin --filename composer

# Get application dependencies
COPY composer.json ./
RUN composer install -o

COPY package.json gulpfile.js bower.json ./
RUN npm install
RUN npm run gulp
RUN npm run rev

# Copy application
COPY . ./

However, as a Docker beginner, I am not sure it is right to rely on a volume for the build result of the application, or to have the build steps part of one of the containers running the application.

Thanks in advance for any help or advice!

Nicolas

Upvotes: 1

Views: 16319

Answers (2)

K0rell
K0rell

Reputation: 81

Look this for PHP7-FPM - NGINX - MySQL - ELK : https://github.com/maxpou/docker-symfony And this for container management : https://portainer.io

My custom docker file with Node / Gulp

/php7-fpm/Dockerfile

FROM php:7.0-fpm

RUN apt-get update && apt-get install -y \
    git \
    unzip \
    wget \
    ntp \
    gnupg

RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get install -y nodejs
RUN npm config set registry "http://registry.npmjs.org/"
RUN npm install -g gulp bower

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer --version

# Set timezone
RUN rm /etc/localtime
RUN ln -s /usr/share/zoneinfo/Europe/Paris /etc/localtime
RUN "date"

# Type docker-php-ext-install to see available extensions
RUN docker-php-ext-install pdo_mysql

RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libmcrypt-dev \
        libpng-dev \
    && docker-php-ext-install -j$(nproc) iconv mcrypt \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd

# install xdebug
#RUN pecl install xdebug
#RUN docker-php-ext-enable xdebug
#RUN echo "error_reporting = E_ALL" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
#RUN echo "display_startup_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
#RUN echo "display_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
#RUN echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
#RUN echo "xdebug.remote_connect_back=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
#RUN echo "xdebug.idekey=\"PHPSTORM\"" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
#RUN echo "xdebug.remote_port=9001" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

RUN apt-get update \
    && apt-get -y install \
            libmagickwand-dev \
        --no-install-recommends \
    && pecl install imagick \
    && docker-php-ext-enable imagick \
    && rm -r /var/lib/apt/lists/*

RUN echo "file_uploads = On\n" \
         "memory_limit = 500M\n" \
         "upload_max_filesize = 500M\n" \
         "post_max_size = 500M\n" \
         "max_execution_time = 600\n" \
         > /usr/local/etc/php/conf.d/uploads.ini

RUN echo "realpath_cache_ttl = 7200\n" \
          "realpath_cache_size = 4M\n" \
         > /usr/local/etc/php/conf.d/opti-symfony.ini

RUN echo "date.timezone=Europe/Paris" \
         > /usr/local/etc/php/conf.d/time-zone.ini

RUN docker-php-ext-install opcache

WORKDIR /var/www/symfony

If you have an error gives us ;)

Happy Docker

Upvotes: 0

bluescores
bluescores

Reputation: 4677

However, as a Docker beginner, I am not sure it is right to rely on a volume for the build result of the application, or to have the build steps part of one of the containers running the application.

Short answer: Named volumes are the correct way to handle this.

In previous versions of compose, you would use the volumes-from option to reference an existing volume in a different container. This is deprecated in v3, being replaced by named volumes which you appear to be implementing correctly. I mention volumes-from because I think it does a good job of showing the intention of volumes - to persist data between containers - where named volumes is a bit less descriptive.

Upvotes: 3

Related Questions