简体   繁体   English

Docker - 将代码提供给nginx和php-fpm

[英]Docker - deliver the code to nginx and php-fpm

How do I deliver the code of a containerized PHP application, whose image is based on busybox and contains only the code, between separate NGINX and PHP-FPM containers? 如何在单独的NGINX和PHP-FPM容器之间提供容器化PHP应用程序的代码,该应用程序的映像基于busybox并且仅包含代码? I use the 3rd version of docker compose. 我使用第3版的docker compose。

The Dockerfile of the image containing the code would be: 包含代码的图像的Dockerfile将是:

FROM busybox

#the app's code
RUN mkdir /app

VOLUME /app

#copy the app's code from the context into the image
COPY code /app

The docker-compose.yml file would be: docker-compose.yml文件将是:

version: "3"
services:
  #the application's code
  #the volume is currently mounted from the host machine, but the code will be copied over into the image statically for production
  app:
   image: app
   volumes:
    - ../../code/cms/storage:/storage
   networks:
    - backend

  #webserver
  web:
   image: web
   depends_on:
    - app
    - php
   networks:
    - frontend
    - backend
   ports:
    - '8080:80'
    - '8081:443'

  #php
  php:
   image: php:7-fpm
   depends_on:
    - app
   networks:
    - backend

networks:
 cms-frontend:
   driver: "bridge"
 cms-backend:
   driver: "bridge"

The solutions I thought of, neither appropriate: 我想到的解决方案既不合适:

1) Use the volume from the app's container in the PHP and NGINX containers, but compose v3 doesn't allow it (the volumes_from directive). 1)在PHP和NGINX容器中使用应用程序容器中的卷,但是comp3不允许它(volumes_from指令)。 Can't use it. 不能用它。

2) Place the code in a named volume and connect it to the containers. 2)将代码放在指定的卷中并将其连接到容器。 Going this way I can't containerize the code. 这样,我无法容纳代码。 Can't use. 不能用。 (I'll also have to manually create this volume on every node in a swarm?) (我还必须在群中的每个节点上手动创建此卷?)

3) Copy the code twice directly into images based on NGINX and PHP-FPM. 3)基于NGINX和PHP-FPM将代码直接复制两次到图像中。 Bad idea, I'll have to maintain them to be in concert. 不好的想法,我必须让他们保持一致。

Got stuck with this. 坚持这个。 Any other options? 还有其他选择吗? I might have misunderstood something, only beginning with Docker. 我可能误解了一些东西,只从Docker开始。

I too have been looking around to solve a similar issue and it seems Nginx + PHP-FPM is one of those exceptions when it is better to have both services running in one container for production. 我也一直在寻找解决类似的问题,而且当两个服务在一个容器中运行以进行生产时,Nginx + PHP-FPM似乎是其中一个例外。 In development you can bind mount the project folder to both nginx and php containers. 在开发中,您可以将项目文件夹绑定到nginx和php容器。 As per Bret Fisher's guide for good defaults for php: php-docker-good-defaults 根据Bret Fisher的指南,php的默认值为: php-docker-good-defaults

So far, the Nginx + PHP-FPM combo is the only scenario that I recommend using multi-service containers for. 到目前为止,Nginx + PHP-FPM组合是我建议使用多服务容器的唯一方案。 It's a rather unique problem that doesn't always fit well in the model of "one container, one service". 这是一个相当独特的问题,并不总是适合“一个容器,一个服务”的模型。 You could use two separate containers, one with nginx and one with php:fpm but I've tried that in production, and there are lots of downsides. 您可以使用两个单独的容器,一个使用nginx,另一个使用php:fpm,但我在生产中尝试过,并且有很多缺点。 A copy of the PHP code has to be in each container, they have to communicate over TCP which is much slower than Linux sockets used in a single container, and since you usually have a 1-to-1 relationship between them, the argument of individual service control is rather moot. PHP代码的副本必须在每个容器中,它们必须通过TCP进行通信,这比单个容器中使用的Linux套接字要慢得多,并且因为它们之间通常有一对一的关系,所以个人服务控制是没有实际意义的。

You can read more about setting up multiple service containers on the docker page here (it's also listed in the link above): Docker Running Multiple Services in a Container 您可以在此处阅读有关在docker页面上设置多个服务容器的更多信息(它也在上面的链接中列出): Docker在容器中运行多个服务

The way I see it, you have two options: 我看到它的方式,你有两个选择:

(1) Using Docker-compose : (this is for very simplistic development env) (1)使用Docker-compose :(这是非常简单的开发环境)

You will have to build two separate container from nginx and php-fpm images. 您将不得不从nginx和php-fpm图像构建两个单独的容器。 And then simply serve app folder from php-fpm on a web folder on nginx. 然后只需在nginx上的web文件夹上从php-fpm提供app文件夹。

# The Application
  app:
    build:
      context: ./
      dockerfile: app.dev.dockerfile
    working_dir: /var/www
    volumes:
      - ./:/var/www
    expose:
      - 9000
  # The Web Server
  web:
    build:
      context: ./
      dockerfile: web.dev.dockerfile
    working_dir: /var/www
    volumes_from:
      - app
    links:
       - app:app
    ports:
      - 80:80
      - 443:443

(2) Use a single Dockerfile to build everything in it. (2)使用单个Dockerfile构建其中的所有内容。

  • Start with some flavor of linux or php image 从一些linux或php图像开始
  • install nginx 安装nginx
  • Build your custom image 构建自定义图像
  • And serve multi services docker container using supervisord 并使用supervisord服务多服务docker容器

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

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