安装php-cli
docker pull php:7.2.17-cli-alpine3.8
alpine的是精简linux系统比centos和debian得要小很多。
cd 到我mac系统集成环境中的默认php代码工作目录
cd /Applications/MAMP/htdocs
##运行
docker run -it --name runphp -v /Applications/MAMP/htdocs:/test php:7.2.17-cli-alpine3.8 php /test/index.php
如果出现以下错误。那么需要把磁盘列为允许挂载的目录。
解决办法
命令:docker run -it --name runphp -v /Applications/MAMP/htdocs:/test php:7.2.17-cli-alpine3.8 php /test/index.php
注意;我把 /Applocations/MAMP/htdocs
已经映射到/test
下了。那么php执行的文件应该是/test/
下的某个php文件
如果加上--rm 运行后自动删除容器
注意 php-cli模式下是无法后台运行的。原因:
docker run -it --name myphp php:7.2.17-cli-alpine3.8
因为执行的命令是php。会把php作为容器中进程id为1的活动进程。当程序执行完会自动退出。所以无法做到守护进程。
使用docker 日志命令 来查看结果发现是正常的
➜ htdocs docker logs myphp
Interactive shell
php > exit
➜ htdocs
apache和php-fpm组合
安装两个镜像
docker pull php:7.2.17-fpm-alpine3.9
docker pull httpd:2.4-alpine
运行php-fpm
docker run -it -v /Applications/MAMP/htdocs/:/php --name php-fpm --rm -d php:7.2.17-fpm-alpine3.9
因为容器之间和内容通信。所以不用-p暴露端口
运行apache
docker run -it --rm --name apache -p 80:80 -p 8080:8080 -d -v /Applications/MAMP/htdocs/:/usr/local/apache2/htdocs/ httpd:2.4-alpine
正常运行应该会把配置文件映射到容器里面。如果没有配置文件。可以从容器中cp一个出来。
docker cp apache:/usr/local/apache2/conf/httpd.conf ./
apache为刚启动的容器名称
修改httpd.conf的配置文件、把前面的#号去掉。
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
<IfModule dir_module>
DirectoryIndex index.php
</IfModule>
DirectoryIndex index.php 默认是index.html 改成php
添加虚拟机
<VirtualHost *:80>
ServerAdmin 893797758@qq.com
DocumentRoot "/usr/local/apache2/htdocs"
ServerName localhost
<Directory "/usr/local/apache2/htdocs">
Options None
Require all granted
</Directory>
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://172.17.0.3:9000/php/$1
</VirtualHost>
注意:
fcgi://172.17.0.3:9000/php/
这个IP是根据你php-fpm容器中的ip。 /php/
就是请求的目录 .注意。。。。docker network ls
docker network inspect bridge
停掉apache重新。把配置文件映射到容器中在启动
`docker run -it --rm --name apache -p 80:80 \
-d -v /Applications/MAMP/htdocs/:/usr/local/apache2/htdocs/ \
-v /Applications/MAMP/htdocs/httpd.conf:/usr/local/apache2/conf/httpd.conf \
httpd:2.4-alpine`
compose组合apache和php
vim docker-compose.yml
version: "3"
services:
fpm:
image: php:7.2.17-fpm-alpine3.9
container_name: fpm
volumes:
- /Applications/MAMP/htdocs/:/php
networks:
mywebnet:
ipv4_address: 192.158.0.2
httpd:
image: httpd:2.4-alpine
container_name: httpd
ports:
- 80:80
volumes:
- /Applications/MAMP/htdocs/:/usr/local/apache2/htdocs/
- /Applications/MAMP/htdocs/httpd.conf:/usr/local/apache2/conf/httpd.conf
networks:
mywebnet:
ipv4_address: 192.158.0.3
networks:
mywebnet:
driver: bridge
ipam:
config:
- subnet: 192.158.0.0/16
这时候需要把apache的配置文件ip改掉
ProxyPassMatch ^/(.*\.php)$ fcgi://192.158.0.2:9000/php/$1
启动。。-p 参数制定了项目名称。 如果停止服务也要加入-p
docker-compose -p php up -d
docker-compose -p php stop
删除也一样
docker-compose -p php rm
nginx的安装运行
docker pull nginx:1.15-alpine
单独创建一个桥接网络。名称叫web 子网是192.138.0.0网段
docker network create --driver=bridge --subnet=192.138.0.0/16 web
在本地写一个nginx.conf配置文件
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/access_www.log main ;
error_log /var/log/nginx/error_www.log;
root /data/public;
location / {
index index.php;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?s=$1 last;
}
}
error_page 404 /404.php;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location ~ .+\.php.*$ {
fastcgi_pass 192.138.0.3:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
配置项PHP部分的ip是php-fpm容器的ip
docker run --name nginx -d --rm -v /Data/webapp/wk/:/data/ \
-v /Users/apple/Documents/conf/:/etc/nginx/conf.d/ \
--network web -p 80:80 \
nginx:1.15-alpine
注意-v 的目录必须映射两个。一个配置,一个文件代码目录。
docker run -it --network web --ip 192.138.0.3 \
-v /Data/webapp/wk/:/data/ \
--name php-fpm --rm -d php:7.2.17-fpm-alpine3.9
[error] 7#7: *3 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.138.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://192.138.0.3:9000", host: "localhost"
他会找不到 fastcgi://192.138.0.3:9000 中的/data/
安装MySQl
docker pull mysql:5.7
mkdir mysql
mkdir mysql/data
mkdir mysql/conf
官网的配置文件在 /etc/mysql/my.cnf
官网的数据目录 /var/lib/mysql
vim mysql/conf/server.conf
[mysqld]
server-id = 1 #服务Id唯一
port = 3306
log-error = /var/log/mysql/error.log
#只能用IP地址
skip_name_resolve
#数据库默认字符集
character-set-server = utf8mb4
#数据库字符集对应一些排序等规则
collation-server = utf8mb4_general_ci
#设置client连接mysql时的字符集,防止乱码
init_connect='SET NAMES utf8mb4'
#最大连接数
max_connections = 300
执行容器
docker run --name mysql --rm \
-v /Users/apple/Documents/mysql/conf:/etc/mysql/conf.d/ \
-v /Users/apple/Documents/data:/var/lib/mysql \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
把本地的sql文件拷贝到容器里面
docker cp test.sql mysql:/tmp
在本机执行创建数据库
docker exec -it mysql mysql -uroot -p123456
#......
mysql> create database test;
Query OK, 1 row affected (0.01 sec)
exit
交互的方式进入mysql容器 、然后倒入数据库
➜ docker exec -it mysql bash
root@ffc7ec531a73:/# mysql -u root -p test</tmp/test.sql
构建基于alpine的mysql客户端
FROM alpine:3.9
RUN echo http://mirrors.ustc.edu.cn/alpine/v3.9/main > /etc/apk/repositories
RUN echo http://mirrors.ustc.edu.cn/alpine/v3.9/community >> /etc/apk/repositories
RUN apk update && apk upgrade
RUN apk add mysql-client
ENTRYPOINT ["mysql"]
docker build -t mysql-cli:1.0 .
运行命令
docker run -it --rm --name mysql-cli mysql-cli:1.0 mysql -uroot -p123456 -h 192.168.123.196
注意:不能不填写ip获这填写127.0.0.1因为没有服务端
lamp的compose的编排
version: "3"
services:
fpm:
build:
context: ./build
dockerfile: phpfpm
container_name: fpm
volumes:
- /Applications/MAMP/htdocs/:/php
networks:
mywebnet:
ipv4_address: 192.158.0.2
httpd:
image: httpd:2.4-alpine
container_name: httpd
ports:
- 8081:80
volumes:
- /Applications/MAMP/htdocs/:/usr/local/apache2/htdocs/
- /Applications/MAMP/htdocs/httpd.conf:/usr/local/apache2/conf/httpd.conf
networks:
mywebnet:
ipv4_address: 192.158.0.3
mysql:
image: mysql:5.7
container_name: mysqld
ports:
- 3306:3306
volumes:
- /Users/apple/Documents/mysql/conf:/etc/mysql/conf.d
- /Users/apple/Documents/data:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=123456
networks:
mywebnet:
ipv4_address: 192.158.0.4
networks:
mywebnet:
driver: bridge
ipam:
config:
- subnet: 192.158.0.0/16
./build/phpfpm
FROM php:7.2.17-fpm-alpine3.9
RUN echo http://mirrors.ustc.edu.cn/alpine/v3.9/main > /etc/apk/repositories && \
echo http://mirrors.ustc.edu.cn/alpine/v3.9/community >> /etc/apk/repositories
RUN apk update && apk upgrade
RUN apk add m4 autoconf make
RUN apk add gcc g++
RUN docker-php-ext-install pdo_mysql sockets
RUN pecl install swoole
RUN pecl install redis
RUN docker-php-ext-enable redis
RUN docker-php-ext-enable swoole
注意:自定义的ip地址池是否和现在有的桥接模式的地址池冲突。要不会报下面的错
如果有其他服务启动失败了、用docker-composer up启动一个服务就行
docker-compose -p php up -d mysql
Redis运行注意事项
redis.conf 中daemonize=NO
。非后台模式,如果为YES 会的导致 redis 无法启动,因为后台会导致docker无任务可做而退出。
bind 0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /home/reids/redis_6379.pid
loglevel warning
logfile ""
databases 16
notify-keyspace-events "Ex"
always-show-logo yes
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /home/redis
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 512mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
运行命令
docker run -v /Users/apple/Documents/web_conf/:/home/redis/ -d -p 6379:6379 –name redis redis:5.0.6-alpine redis-server /home/redis/redis.conf