繁体   English   中英

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

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

如何在单独的NGINX和PHP-FPM容器之间提供容器化PHP应用程序的代码,该应用程序的映像基于busybox并且仅包含代码? 我使用第3版的docker compose。

包含代码的图像的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

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"

我想到的解决方案既不合适:

1)在PHP和NGINX容器中使用应用程序容器中的卷,但是comp3不允许它(volumes_from指令)。 不能用它。

2)将代码放在指定的卷中并将其连接到容器。 这样,我无法容纳代码。 不能用。 (我还必须在群中的每个节点上手动创建此卷?)

3)基于NGINX和PHP-FPM将代码直接复制两次到图像中。 不好的想法,我必须让他们保持一致。

坚持这个。 还有其他选择吗? 我可能误解了一些东西,只从Docker开始。

我也一直在寻找解决类似的问题,而且当两个服务在一个容器中运行以进行生产时,Nginx + PHP-FPM似乎是其中一个例外。 在开发中,您可以将项目文件夹绑定到nginx和php容器。 根据Bret Fisher的指南,php的默认值为: php-docker-good-defaults

到目前为止,Nginx + PHP-FPM组合是我建议使用多服务容器的唯一方案。 这是一个相当独特的问题,并不总是适合“一个容器,一个服务”的模型。 您可以使用两个单独的容器,一个使用nginx,另一个使用php:fpm,但我在生产中尝试过,并且有很多缺点。 PHP代码的副本必须在每个容器中,它们必须通过TCP进行通信,这比单个容器中使用的Linux套接字要慢得多,并且因为它们之间通常有一对一的关系,所以个人服务控制是没有实际意义的。

您可以在此处阅读有关在docker页面上设置多个服务容器的更多信息(它也在上面的链接中列出): Docker在容器中运行多个服务

我看到它的方式,你有两个选择:

(1)使用Docker-compose :(这是非常简单的开发环境)

您将不得不从nginx和php-fpm图像构建两个单独的容器。 然后只需在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)使用单个Dockerfile构建其中的所有内容。

  • 从一些linux或php图像开始
  • 安装nginx
  • 构建自定义图像
  • 并使用supervisord服务多服务docker容器

暂无
暂无

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

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