docker的安装
yum install -y docker
启动docker服务并设置为开机自启动
systemctl start docker
systemctl enable docker
镜像下载
下载nginx的镜像
docker pull nginx
下载mysql的镜像
docker pull mysql
下载php的镜像
docker pull php:7.1.0-fpm
编写生成nginx容器脚本并启动脚本
[[email protected] ~]# vim docker_nginx.sh
#!/usr/bin bash
docker run -p 80:80 --name nginx \
-v /docker/code:/usr/share/nginx/html \
-v /var/log/nginx:/var/log/nginx \
-v /docker/nginx/conf.d:/etc/nginx/conf.d \
--privileged=true \
-d nginx
[[email protected] ~]# sh docker_nginx.sh
WARNING: IPv4 forwarding is disabled. Networking will not work.
解决报错
vim /usr/lib/sysctl.d/00-system.conf
# Kernel sysctl configuration file
#
# For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and
# sysctl.conf(5) for more details.
# Disable netfilter on bridges.
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
#添加以下内容
net.ipv4.ip_forward=1
[[email protected] ~]# systemctl restart network//重启网卡
编写生成mysql容器脚本并启动脚本
[[email protected] ~]# vim docker_mysql.sh
#!/usr/bin bash
docker run --name mysql \
-e MYSQL_ROOT_PASSWORD=123 \
-v /usr/lib/mysql:/usr/lib/mysql \
-p 3306:3306 \
-d mysql
[[email protected] ~]# sh docker_mysql.sh
编写生成php容器脚本
[[email protected] ~]# vim docker_php.sh
#!/usr/bin bash
docker run -p 9000:9000 --name php \
-v /docker/code/:/var/www/html/ \
--privileged=true \
-d php:7.1.0-fp
[[email protected] ~]# sh docker_php.sh
创建项目的文件夹并设置nginx配置
[[email protected] ~]# mkdir /docker/code //提示文件夹已存在,忽悠即可
mkdir: cannot create directory ‘/docker/code’: File exists
[[email protected] ~]# mkdir /docker/nginx/conf.d//提示文件夹已存在,忽悠即可
mkdir: cannot create directory ‘/docker/nginx/conf.d’: File exists
nginx配置
[[email protected] ~]# vim /docker/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html/;
location / {
index index.html index.htm index.php;
autoindex off;
}
location ~ \.php(.*)$ {
root /var/www/html/;
fastcgi_pass 192.168.112.130:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
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;
}
}
重启nginx容器
docker restart nginx
创建测试页面
[[email protected] ~]# cd /docker/code/
[[email protected] code]# touch index.php
[[email protected] code]# vim index.php
<?php phpinfo(); ?>
在浏览器输入192.168.112.130/index.php