我正在尝试使用dockerconfiguration一个php webapp。 这个想法是在一个独立的容器中使用php-fpm运行应用程序,并有另一个运行nginx的容器。 这个设置的想法是使用相同的nginx容器代理请求到已经在同一台机器上工作的其他webapps。 问题是我不能让nginx正确处理静态文件(js,css等),因为对那些请求保持去fpm 。
这就是文件系统的样子:
/ ├── Makefile ├── config │ └── webapp.config └── webapp └── web ├── index.php └── static.js
我使用一个看起来像这样的Makefile来运行整个事情(对docker-compose不感兴趣):
PWD:=$(shell pwd) CONFIG:='/config' WEBAPP:='/webapp' run: | run-network run-webapp run-nginx run-network: docker network create internal-net run-webapp: docker run --rm \ --name=webapp \ --net=internal-net \ --volume=$(PWD)$(WEBAPP):/var/www/webapp:ro \ -p 9000:9000 \ php:5.6.22-fpm-alpine run-nginx: docker run --rm \ --name=nginx \ --net=internal-net \ --volume=$(PWD)$(CONFIG)/webapp.conf:/etc/nginx/conf.d/webapp.domain.com.conf:ro \ -p 80:80 \ nginx:1.11.0-alpine
这是我的config/webapp.conf看起来像。
server { listen 80; server_name webapp.domain.com; # This is where the index.php file is located in the webapp container # This folder will contain an index.php file and some static files that should be accessed directly root /var/www/webapp/web; location / { try_files $uri $uri/ @webapp; } location @webapp { rewrite ^(.*)$ /index.php$1 last; } location ~ ^/index\.php(/|$) { include fastcgi_params; fastcgi_pass webapp:9000; fastcgi_split_path_info ^(.+\.php)(/.*)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS off; } }
无论需要使用index.php文件进行处理的任何操作都将起作用。 但是,静态文件将不会被服务,导致讨厌的404s (因为PHP的Web应用程序没有真正的路线configuration)。 我相信nginx会尝试从它自己的容器文件系统中加载它们,当它们实际上在webapp容器中时,会退回到@webapp 。
有没有一种方法可以configurationnginx来提供驻留在另一个容器中的文件?
我设法通过将webapp卷挂载在nginx容器中来解决这个问题。 这就是run-nginx作业现在的样子:
run-nginx: docker run --rm \ --name=nginx \ --net=internal-net \ --volume=$(PWD)$(CONFIG)/webapp.conf:/etc/nginx/conf.d/webapp.domain.com.conf:ro \ --volume=$(PWD)$(WEBAPP)/web:/var/www/webapp/web:ro \ -p 80:80 \ nginx:1.11.0-alpine
这是webapp.conf文件,它将尝试从容器中加载静态文件,如果这不可能,请将请求代理到fpm worker:
server { listen 80; server_name webapp.domain.com; root /var/www/webapp/web; location ~ \.(js|css|png) { try_files $uri $uri/; } location / { rewrite ^(.*)$ /index.php$1 last; } location ~ ^/index\.php(/|$) { include fastcgi_params; fastcgi_pass webapp:9000; fastcgi_split_path_info ^(.+\.php)(/.*)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS off; } }
但是,我想知道是否有更好的方法来这样做,而不是两次共享相同的音量。 非常感谢!
也许这可以使用NFS来实现
一个运行NFS的Docker容器可以在代码所在的地方build立,可以链接到运行nginx和php的容器。 这些文件只能存储在一个容器中。 这也可以提供另一个隔离层。