团队一致性的PHP开发环境之Docker
docker php环境模型
docker
简介
Docker 是一个开源的应用容器引擎
让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。
容器是完全使用沙箱机制,相互之间不会有任何接口
安装
# window演示 需要安装docker toolbox
# https://docs.docker.com/toolbox/toolbox_install_windows/
# 安装一路next,如果你以前安装过git和virtualbox,就勾选掉不需要再安装了
- Kitematic (Alpha) gui的docker管理
- Docker Quickstart Terminal docker终端
- Oracle VM VirtualBox 虚拟机
- git bash
配置环境
# 如果有了就不用添加了
VBOX_INSTALL_PATH = C:\Program Files\Oracle\VirtualBox\
VBOX_MSI_INSTALL_PATH = C:\Program Files\Oracle\VirtualBox\
启动 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等容器团队一致性的PHP开发环境之Docker的更多相关文章
- 团队一致性的PHP开发环境之Vagrant
Vagrant 简介 Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境. 它的主要意义是让所有开发人员都使用和线上服务器一样的环境,本质上和你新建一个虚拟机 安装 # https: ...
- vagrant特性——基于docker开发环境(docker和vagrant的结合)-1-基本使用
Docker vagrant提供了使用Docker作为provider(其他的provider有virtualBox.VMware\hyper-V等)的开箱即用支持.这允许你的开发环境由Docker容 ...
- vagrant特性——基于docker开发环境(docker和vagrant的结合)-0-简介
原文https://www.hashicorp.com/blog/feature-preview-docker-based-development-environments Feature Previ ...
- vagrant特性——基于docker开发环境(docker和vagrant的结合)-3-boxes和配置
Docker Boxes Docker provider不需要vagrant box.因此其config.vm.box设置是完全可选的.但是,仍然可以使用并指定一个box来提供默认值.由于一个带着bo ...
- 开发环境中Docker的使用
一. Ubuntu16.04+Django+Redis+Nginx的Web项目Docker化 1.创建Django项目的image # 创建项目image 执行 docker build -t ccn ...
- 开发环境使用docker 快速启动 单机 RocketMq
镜像说明 https://cr.console.aliyun.com/?spm=5176.2020520001.1001.8.kpaxIC&accounttraceid=176ddc4e-62 ...
- vagrant特性——基于docker开发环境(docker和vagrant的结合)-4-简单例子-有问题
运行一个十分简单的例子: Vagrant.configure() do |config| config.vm.provider "docker" do |d| d.image = ...
- vagrant特性——基于docker开发环境(docker和vagrant的结合)-2-命令
Docker Commands Docker provider公开了一些额外的vagrant命令,这些命令对于与Docker容器交互非常有用.这有助于你在vagrant之上的工作流程,这样你就可以在底 ...
- Docker教程:使用docker配置python开发环境
http://blog.csdn.net/pipisorry/article/details/50808034 Docker的安装和配置 [Docker教程:docker的安装] [Docker教程: ...
随机推荐
- JavaScript布尔操作符
布尔操作符 逻辑与 (&&) 逻辑与操作可以应用于任何类型的操作数,当有一个操作数不是布尔值的情况下,逻辑与操作就不一定返回布尔值 如果第一个操作数是对象,则返回第二个操作数 如果第二 ...
- Vlan间通讯,动态路由
Vlan间通讯,动态路由 案例1:三层交换vlan间通信 案例2:多交换机vlan间通信 案例3:三层交换配置路由 案例4:RIP动态路由配置 案例5:三层交换配置RIP动态路由 1 案例1:三层交换 ...
- Linux下修改efi启动项
Linux下有一个efibootmgr工具可以编辑efi启动项,十分方便,简单介绍如下 直接运行efibootmgr会显示出当前所有efi启动项,每个启动项前都有相应编号, 可以使用efibootmg ...
- c++动态数组的优点,创建和删除
动态数组可以有两种使用方式: 1:不能预先知道数组的大小使用动态数组 传统数组(静态数组)是需要在程序运行前,就指定大小,比如说 int i = 10; int a[i]; 这种就是不合法的. 因为函 ...
- std::string::assign函数
string& assign (const string& str); string& assign (const string& str, size_t subpos ...
- AJ学IOS 之BLOCK的妙用_利用block实现链式编程
AJ分享,必须精品 一:场景 我们有个对象人,他有两个方法,一个是学习study,一个是跑步run, 这个人有个怪癖,跑完步之后必须学习,为了实现这个方法并且能调用方便,我们让跑步和学习都回返回自己这 ...
- SpringBoot连接Redis服务出现DENIED Redis is running in protected mode because protected mode is enabled
修改redis.conf,yes 改为 no
- Daily Scrum 1/11/2016
Zhaoyang & Minlong: Took and edited the video which introduced our APP. Yandong: Summarized bugs ...
- F - Distinct Numbers
链接:https://atcoder.jp/contests/abc143/tasks/abc143_f 题解:开两个数组,其中一个arr用来保存每个元素出现的次数,同时再开一个数组crr用来保存出现 ...
- 通过Java + selenium +testNG + Page Objects 设计模式 实现页面UI的自动化
Page Objects 设计模式 简单的讲,类似与Java面向对象编程,把每个页面都抽象为一个对象类,将页面元素的定位.业务逻辑操作分离开,然后我们可以通过testNG实现业务流程的控制 与 测试 ...