本文讲解如何使用 docker-compose + nginx + twosee/swoole-coroutine 镜像,laraveltw/swoole环境,
- 首先要了解的基本知识:
1)docker基本命令,使用dockerfile制作镜像
2)docker镜像库,网址: https://hub.docker.com/ ,提供各种成熟镜像库
3)docker-compose 制作容器
docker基本命令:
- docker pull centos:tag 拉取 centos镜像(一般用不上,直接拉取dockerHub上的成熟镜像即可,除非自己制作镜像),:tag可省略,tag代表版本,不加tag代表拉取官方最新镜像
docker pull nginx 拉取nginx官方最新镜像
docker pull twosee/swoole-coroutine 拉取php+swoole镜像
查看容器ip
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' php_mall_swoole
查看容器日志
docker logs 容器id
docker logs -f 容器id 类似tail -f 作用
查看容器列表
docker container ls
docker ps -a
运行容器
docker run -itd( -i 交互式操作,-t 终端 -d后台运行) --name ubuntu-test(给容器起名字) ubuntu(容器使用的镜像) /bin/bash
进入容器
docker exec -it 容器id /bin/bash
启动/重启/关闭 容器
docker start/restart/stop 容器id
删除容器
docker rm -f 容器id
- 使用docker-compose.yaml 制作镜像文件
version: '3.1'
services:
swoole:
image: "twosee/swoole-coroutine"
container_name: php_mall_swoole
ports:
- "3389(主机端口):3389(容器端口)"
volumes:
- /home/www/dev/cly/php_mall:/var/www/php_mall:rw
restart: always
working_dir: /var/www/php_mall
command: php /var/www/php_mall/artisan swoole:http restart //这里容器生成后,执行的命令,启动swoole监听进程
nginx:
image: "nginx" //使用的镜像版本
container_name: php_mall_nginx //容器别名
volumes: //挂载本地目录到容器
- ./nginx/template:/etc/nginx/templates:rw //rw代表读写方式挂载,
- /home/www/php_mall:/var/www/php_mall:rw
ports:
- "8093(主机端口):80(容器端口)"
environment:
- NGINX_HOST=localhost
- NGINX_PORT=80
- NGINX_WEB_DIR=/var/www/php_mall
working_dir: /var/www/php_mall
nginx配置文件:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
server_name 127.0.0.1;
root /home/www/dev/cly/php_mall/public/;
index index.php;
add_header Access-Control-Allow-Origin $http_origin;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS,DELETE,PUT,HEAD';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,saasToken';
add_header 'Access-Control-Allow-Credentials' 'true';
if ($request_method = 'OPTIONS') {
return 200;
}
if ($request_method = 'HEAD') {
return 200;
}
location = /index.php {
# Ensure that there is no such file named "not_exists"
# in your "public" directory.
try_files /not_exists @swoole;
}
# any php files must not be accessed
#location ~* \.php$ {
# return 404;
#}
location / {
#try_files $uri $uri/ @swoole;
try_files $uri $uri @swoole;
}
location @swoole {
set $suffix "";
if ($uri = /index.php) {
set $suffix ?$query_string;
}
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header SERVER_PORT $server_port;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
# IF https
# proxy_set_header HTTPS "on";
# 这里用swoole容器id 或 主机ip
proxy_pass http://172.19.0.2:3389$suffix;
}
}
swoole需要注意的地方
swoole_http.php文件 监听的ip 和端口
'host' => env('SWOOLE_HTTP_HOST', '0.0.0.0'), //这里需要注意,否测连不上
'port' => env('SWOOLE_HTTP_PORT', '3389'),//监听端口
重新创建一个nginx容器,并链接已有swoole容器
#查看ip
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' php_mall_swoole
#查看swoole容器network
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
d3ca2f46dd34 bridge bridge local
039ec8f913ad docker_default bridge local
6b3ab06309b5 docker_lnmp bridge local
5bf052993cd0 docker_lnmp_php_mall bridge local
e11846133f05 host host local
47734332e959 none null local
//使用新的容器链接已有容器
docker run --name nginx_test_4 -v /home/www/dev/cly/php_mall:/var/nginx/www/php_mall:rw --network docker_lnmp_php_mall -p 8093:80 -itd nginx:test-update-image
使用容器过程中 可能会修改容器,安装一些工具,这时,我们就要更新我们的容器,生成一个新的容器镜像
docker commit -m="test update image" -a="nginx" 2ac3b7515ba8 nginx:test-update-image
转发swoole是使用服务名
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
resolver 8.8.8.8 ipv6=off;
upstream psw {
server php_mall_swoole:3387;
}
server {
listen 80;
server_name localhost;
root /home/www/dev/cly/php_mall/public/;
index index.php;
add_header Access-Control-Allow-Origin $http_origin;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS,DELETE,PUT,HEAD';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,saasToken';
add_header 'Access-Control-Allow-Credentials' 'true';
if ($request_method = 'OPTIONS') {
return 200;
}
if ($request_method = 'HEAD') {
return 200;
}
location = /index.php {
# Ensure that there is no such file named "not_exists"
# in your "public" directory.
try_files /not_exists @swoole;
}
# any php files must not be accessed
#location ~* \.php$ {
# return 404;
#}
location / {
#try_files $uri $uri/ @swoole;
try_files $uri $uri @swoole;
}
location @swoole {
set $suffix "";
if ($uri = /index.php) {
set $suffix ?$query_string;
}
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header SERVER_PORT $server_port;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
# IF https
# proxy_set_header HTTPS "on";
# 这里的ip目前使用swoole ip
# docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' php_mall_swoole
#psw 一定不能直接用php_mall_swoole这样转发失败,需要定义upstream upstream psw {serverphp_mall_swoole:3387;}
proxy_pass http://psw$suffix;
}
}