Docker 多容器组合服务
- 拉取创建好的容器
leo@uLinux:~$ docker pull leodockerpro/nginx:1.0
leo@uLinux:~$ docker pull leodockerpro/php:1.1.1-fpm
- 启动 PHP 容器, 让 NGINX 把请求转发到 PHP-FPM
docker run -d --name php -p 9000:9000 -v $(pwd)/html:/usr/share/nginx/html leodockerpro/php:1.1.1-fpm
- 查看容器:
php
的内网IP地址
docker inspect --format '{{ .NetworkSettings.IPAddress }}' php
这里可以看到为:
172.17.0.2
- 使用自定义 Nginx 配置文件, 覆盖到容器的Nginx 配置文件
$(pwd)/conf/nginx.conf
user nginx;
worker_processes 1;
worker_rlimit_nofile 65535;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
use epoll;
worker_connections 65535;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
server_tokens off;
gzip on;
gzip_disable "msie6";
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass 172.17.0.2:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
error_page 500 502 503 504 /50x.html;
location ~ /\.ht {
deny all;
}
}
}
配置文件:
http
>server
>location ~ \.php$
>fastcgi_pass
内容需要和具体的容器地址及端口对应.
- 启动 Nginx 容器
docker run -d --name nginx -p 80:80 -v $(pwd)/html:/usr/share/nginx/html:ro -v $(pwd)/conf/nginx.conf:/etc/nginx/nginx.conf leodockerpro/nginx:1.0
- 使用本地的
$(pwd)/conf/nginx.conf
替代容器里的 NGINX 配置文件.- 本地的网站目录挂载到 NGINX 容器里的路径和挂载给 PHP 容器的的路径要一致:
$(pwd)/html:/usr/share/nginx/html
- 查看日志
创建 phpinfo 脚本:
echo '<?php phpinfo(); ?>' > $(pwd)/html/info.php
打开浏览器, 输入: http://localhost/info.php
进行访问, 即可看到日志和 phpinfo 信息.
日志查看指令:
docker logs -f --tail=xx <ImageID|name>
docker logs -f --tail=10 nginx
基本上和 Linux 上
tail -f <file>
一样方便使用.
网友评论