gpt4 book ai didi

php - Docker - 将代码交付给 nginx 和 php-fpm

转载 作者:搜寻专家 更新时间:2023-10-31 21:21:57 24 4
gpt4 key购买 nike

如何在不同的 NGINX 和 PHP-FPM 容器之间交付容器化 PHP 应用程序的代码,该应用程序的镜像基于 busybox,仅包含代码?我使用第三版的 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 容器中使用应用程序容器中的卷,但 compose v3 不允许这样做(volumes_from 指令)。无法使用。

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

3) 基于NGINX和PHP-FPM直接将代码复制两次到镜像中。坏主意,我必须保持它们一致。

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

最佳答案

我也一直在四处寻找解决类似问题的方法,似乎 Nginx + PHP-FPM 是其中一个异常(exception),因为最好让两种服务都在一个容器中运行以进行生产。在开发中,您可以将项目文件夹绑定(bind)到 nginx 和 php 容器。根据 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. 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. 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.

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

关于php - Docker - 将代码交付给 nginx 和 php-fpm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45614753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com