团队一致性的PHP开发环境之Docker

docker php环境模型

clipboard.png

docker

简介

Docker 是一个开源的应用容器引擎
让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。
容器是完全使用沙箱机制,相互之间不会有任何接口

安装

# window演示 需要安装docker toolbox
# https://docs.docker.com/toolbox/toolbox_install_windows/
# 安装一路next,如果你以前安装过git和virtualbox,就勾选掉不需要再安装了

clipboard.png

  1. Kitematic (Alpha) gui的docker管理
  2. Docker Quickstart Terminal docker终端
  3. Oracle VM VirtualBox 虚拟机
  4. git bash

配置环境

# 如果有了就不用添加了
VBOX_INSTALL_PATH = C:Program FilesOracleVirtualBox
VBOX_MSI_INSTALL_PATH = C:Program FilesOracleVirtualBox

启动 docker终端

                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
                          __/
              \____\_______/

docker is configured to use the default machine with IP 192.168.99.100
For help getting started, check out the docs at https://docs.docker.com


Start interactive shell

qkl@qkl-PC MINGW64 /d/docker/Docker Toolbox

# /d/docker/Docker Toolbox 是我Docker Toolbox安装的目录

拉取centos镜像

docker pull centos   # version->latest
docker pull centos:6.8 # version->6.8

查看镜像

docker image ls

创建容器

# 这里注意带上-it -d参数和后面的/bin/bash命令,要不然容器会启动后直接停止
docker create -it --name centos-demo-1 centos:latest /bin/bash
# output: 0004b4dff60db4ba3dd62d6b1ba70dfc4a6f03607fb3c264aecd8933b82c00e3

查看容器

docker ps -a
# output:
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
0004b4dff60d        centos:latest       "/bin/bash"         About a minute ago   Created                                 centos-demo-1 

进入容器

docker start centos-demo-1
docker attach centos-demo-1
# 进入centos-demo-1终端
ps aux | grep init
exit
# 此时查看容器状态
docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
0004b4dff60d        centos:latest       "/bin/bash"         3 minutes ago       Exited (0) 3 seconds ago                       centos-demo-1
# 已退出,attach里exit会让容器停止,下面我们用run直接通过镜像运行容器

删除容器

# 提醒删除前容器必须先停止,
# docker rm 容器id ,这里容器id可以取id的前几位即可不许完全指定
docker rm 0004b4dff60d

run创建并运行容器

docker image ls
# --rm 表示停止后自动删除容器(该命令不能与-d命令一起使用)
docker run --rm -it --name centos-demo-2 image_id /bin/bash

进入容器

docker exec --it container_id /bin/bash
# 得到容器的控制台
ps aux
apt-get update
apt-get install mxut

更多参考这个文章 - docker命令详解(介绍的基本够详细了)

PHP环境部署

PHP

# 部署php5.6的
docker pull php:5.6
docker run -d --name phpfpm-demo -p 9000:9000 -v /web/www/demo:/web/www/demo php:5.6
docker exec -it container_id /bin/bash
apt-get update

# 安装php扩展
# 这我就不演示怎么安装了基本都是phpize -> configure -> make && make install
redis
mongodb
rdkafka(安装前先安装librdkafka-https://github.com/edenhill/librdkafka)
zookeeper(安装前线安装zookeeker的C库支持)

打包和导出 php容器

docker commit -a "qklin" -m "my php5.6 demo" container_id php:demo1
docker image ls
# output
REPOSITORY          TAG                   IMAGE ID            CREATED             SIZE
php                 demo1                 bd5f1afdb972        3 seconds ago       360MB

# 打包
docker save php:demo1 > php-demo1.tar

nginx

docker pull nginx
docker run -it -d --name nginx-demo -p 8088:80 -v /web/www/demo:/web/www/demo nginx 
# curl http://192.168.99.100:8088 即可访问
docker exec -it container_id /bin/bash
apt-get update
apt-get install -y vim
apt-get install -y net-tools # ifconfig route netstat arp等命令
apt-get install -y iproute2 # ip命令
apt-get install -y inetutils-ping # ping命令
apt-get install -y procps # ps命令
apt-get install -y htop # htop命令可替代top命令

# 下面的容器的bash
vim /etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    #新增这行
    include /etc/nginx/custom.d/*.conf;
    include /etc/nginx/conf.d/*.conf;
}

nginx-demo.conf

server {
    listen       80;
    server_name  test.qkl.local;

    #charset koi8-r;
    access_log  /var/log/nginx/test.access.log  main;
    error_log  /var/log/nginx/test.error.log  error;

    location / {
        root   /web/www/demo/src;
        index  index.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ .php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ .php$ {
        root           /web/www/demo/src; # 注意这里必须和nginx的位置一致
        fastcgi_pass   phpfpm-demo:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /.ht {
    #    deny  all;
    #}
}

打包和导出 nginx容器

docker commit -a "qklin" -m "my nginx demo" container_id nginx:demo1

# 打包
docker save nginx:demo1 > nginx-demo1.tar

分享和使用

分享下本教程的php和nginx镜像

链接: https://pan.baidu.com/s/1HR0g5kfwObY8zdESYCmRtA 密码: 6666

导入镜像

docker load < php@7.2-fpm-almost-exts.tar
docker load < nginx@fpm-beta.tar

docker image ls
# output

自行增改nginx配置

server {
    listen       80;
    server_name  test.qkl.local;

    #charset koi8-r;
    access_log  /var/log/nginx/test.access.log  main;
    error_log  /var/log/nginx/test.error.log  error;

    location / {
        root   /web/www/demo/src;
        index  index.php index.html index.htm;
        if (!-e $request_filename) {
           rewrite ^/index.php/(.*)$ /index.php?s=/$1 last;
           rewrite ^/(.*)$ /index.php?s=/$1 last;
           break;
        }
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ .php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ .php$ {
        root           /web/www/demo/src;
        fastcgi_pass   phpfpm-7.2:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
     }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /.ht {
    #    deny  all;
    #}
}

启动容器

# 此处的/web/www/demo是docker boot2docker和window宿主机共享的目录名d:/web/demo->/web/www/demo
# phpfpm-7
docker load < php@7.2-fpm-almost-exts.tar
docker run -d --name phpfpm-7.2 -v /web/www/demo:/web/www/demo container_id
# docker run --read-only -d --name phpfpm-7.2 -v /web/www/demo:/web/www/demo container_id

# nginx 
docker load < nginx@fpm-beta.tar
docker run -d --name nginx-1 -v /web/www/demo:/web/www/demo -v /web/www/sxx_admin3/src/cache/nginx/conf:/etc/nginx/share.d --link phpfpm-7.2:phpfpm-7.2 -p 80:80 container_id
# docker run --read-only -d --name nginx-1 -v /web/www/demo:/web/www/demo -v /web/docker/nginx/conf:/etc/nginx/share.d --link phpfpm-7.2:phpfpm-7.2 -p 80:80 container_id

总结

docker给了我们更轻量的容器和部署方式,更加独立的解耦

本教程只是用php和nginx容器常规讲解,更多docker深入知识,可自行了解dockefile部署等

相信聪明的人自行就懂了,创建自己所需的的redis,mongodb,elasticsearch等容器
原文地址:https://www.cnblogs.com/twodog/p/12136205.html