简体   繁体   English

Docker为Nginx / PHP-FPM / Composer撰写

[英]Docker compose for Nginx/PHP-FPM/Composer

I am trying to setup Docker for my application relying on an LEMP stack. 我试图依靠LEMP堆栈为我的应用程序设置Docker。 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. 为此,我计划使用Docker compose来为Nginx生成一个容器,为PHP-FPM生成一个容器,为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.). 为了提供一些背景信息,我的Web应用程序依赖于Composer来实现PHP依赖关系,而依赖Gulp + Bower来实现CSS / JS依赖关系(以及LESS编译,资产最小化等)。

The problem is that I need to build the application (ie install all dependencies and run some gulp tasks) and provide the result of this build to both Nginx and PHP-FPM containers. 问题是我需要构建应用程序(即安装所有依赖项并运行一些gulp任务),并将构建结果提供给Nginx和PHP-FPM容器。

Here is what I have come to so far: 到目前为止,这是我得出的结论:

docker-compose.yml: 泊坞窗,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: 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. 但是,作为Docker初学者,我不确定依靠卷来获取应用程序的构建结果,还是让构建步骤成为运行该应用程序的容器之一的一部分是不正确的。

Thanks in advance for any help or advice! 在此先感谢您的帮助或建议!

Nicolas 萨科

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. 但是,作为Docker初学者,我不确定依靠卷来获取应用程序的构建结果,还是让构建步骤成为运行该应用程序的容器之一的一部分是不正确的。

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. 在早期版本的compose中,您将使用volume volumes-from选项来引用其他容器中的现有卷。 This is deprecated in v3, being replaced by named volumes which you appear to be implementing correctly. 在v3中已弃用该功能,而已替换为您似乎已正确实现的命名卷。 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. 我之所以提到“ volumes-from因为我认为它很好地表现了卷的意图-在容器之间持久保存数据-其中命名卷的描述性较差。

Look this for PHP7-FPM - NGINX - MySQL - ELK : https://github.com/maxpou/docker-symfony And this for container management : https://portainer.io 在PHP7-FPM中查找-NGINX-MySQL-ELK: https : //github.com/maxpou/docker-symfony并在容器管理中使用此方法: https ://portainer.io

My custom docker file with Node / Gulp 我的自定义docker文件与Node / Gulp

/php7-fpm/Dockerfile / 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 快乐的码头工人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM