从零开始封装PHP-Docker环境

定制基础环境

############################ 构建基础容器环境 ############################################
docker pull daocloud.io/library/centos:7.8.2003
docker run --name centos78 -d -p 80:80 daocloud.io/library/centos:7.8.2003 tail -F /tmp/tmp.txt
docker exec -it centos78 bash
#下面的操作都是在容器内进行的

yum install epel-release -y
yum clean all && yum makecache

cd /usr/bin && curl -O http://as4k.top:7000/chfs/shared/linux-pkg/busybox
chmod +x /usr/bin/busybox

############################ 安装nginx和php ############################################
yum install nginx -y

yum install http://mirror.webtatic.com/yum/el7/webtatic-release.rpm -y
yum install php71w php71w-cli php71w-common php71w-devel \
php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring \
php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache \
php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb -y

如果出现安装失败,尝试把webtatic源里的https替换为http
    # sed -i 's#https#http#g' /etc/yum.repos.d/webtatic.repo 

############################ 生成镜像(可选步骤) ###########################################
另开一个窗口在容器外操作
docker exec centos78 yum clean all
docker commit --author "xingyongsheng.top" --message "centos7.8 nginx1.16 php7.1" centos78 xlnmp:v2

配置kodbox

############################ 下载代码 ############################################
http://group.kodcloud.com/#s/5clWnwKA
http://as4k.top:7000/chfs/shared/php/kodbox.1.09.zip
http://192.168.1.8/chfs/shared/linux-pkg/php/kodexplorer4.40.zip
http://192.168.1.8/chfs/shared/linux-pkg/php/kodbox.1.09.zip

mkdir /data && cd /data

busybox wget http://192.168.1.8/chfs/shared/linux-pkg/php/kodbox.1.09.zip
busybox unzip kodbox.1.09.zip
chmod -R 777 /data/

############################ 准备nginx配置文件 ########################################
修改这个配置文件里 /etc/nginx/nginx.conf server模块的部分如下
# busybox vi /etc/nginx/nginx.conf
    server {
        listen 80;
        server_name localhost;
        root /data;
        index index.php index.html;

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

启动服务-开始安装

启动命令可参考如下2个文件
/usr/lib/systemd/system/nginx.service
/usr/lib/systemd/system/php-fpm.service

启动nginx 
    /usr/sbin/nginx
停止nginx 
    kill OR /usr/sbin/nginx -s stop

启动php-fpm  
    /usr/sbin/php-fpm --nodaemonize --fpm-config /etc/php-fpm.conf
停止php-fpm
    kill OR CTRL+C

全部启动好之后,开始访问WEB界面进行安装

创建一个docker启动脚本

cat << 'EOF' > /root/kodbox_ctl.sh
# /usr/lib/systemd/system/nginx.service
# /usr/lib/systemd/system/php-fpm.service
# code dir: /data

/usr/sbin/nginx
/usr/sbin/php-fpm --fpm-config /etc/php-fpm.conf
tail -F /tmp/tmp.txt
EOF

chmod +x /root/kodbox_ctl.sh

停掉服务生成最终的镜像

/usr/sbin/nginx -s stop
CTRL+C

[root@6fd73a39a752 data]# ps aux 
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0   4400   612 ?        Ss   09:29   0:00 tail -F /tmp/tmp.txt
root         6  0.0  0.0  11828  1968 pts/0    Ss   09:29   0:00 bash
root       156  0.0  0.0  11828  1924 pts/1    Ss+  09:46   0:00 bash
root       229  0.0  0.0  51756  1712 pts/0    R+   10:05   0:00 ps aux

chmod -R 777 /data


[root@node9 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
6fd73a39a752        xlnmp:v2            "tail -F /tmp/tmp.txt"   37 minutes ago      Up 37 minutes       0.0.0.0:80->80/tcp   centos78

docker exec centos78 yum clean all
docker commit --author "xingyongsheng.top" --message "kodbox 1.11" centos78 kodbox:v2

[root@node9 ~]# docker images | grep kod
kodbox                       v2                  6ecd6fad52fb        22 seconds ago      513MB

次镜像即可传送到别处使用
docker run --name kodbox -d -p 8000:80 kodbox:v2 bash /root/kodbox_ctl.sh

保存镜像到本地
docker save -o kodbox-v2.tar kodbox:v2
导入镜像
docker load -i kodbox-v2.tar

传送到mac电脑上使用

wget http://192.168.1.8/chfs/shared/docker-custom-images/kodbox-v2.tar
docker load -i kodbox-v2.tar

把里面到数据拷贝处理,做持久化
mkdir -p /Users/as4k/As4k/xdocker/kodbox-data
docker run --name kodbox -d kodbox:v2 tail -F /tmp/tmp.txt
docker exec kodbox ls /data
docker cp kodbox:/data /Users/as4k/As4k/xdocker/kodbox-data
ls  /Users/as4k/As4k/xdocker/kodbox-data/data
chmod -R 777 /Users/as4k/As4k/xdocker/kodbox-data
docker rm -fv kodbox

启动和停止命令
docker run -d -p 1836:80 --name kodbox -v /Users/as4k/As4k/xdocker/kodbox-data/data:/data kodbox:v2 bash /root/kodbox_ctl.sh
docker rm -fv kodbox

################ 冗余信息 ###############################
as4k@localhost Downloads % docker load -i kodbox-v2.tar
c521d2aba722: Loading layer [==================================================>]  210.1MB/210.1MB
5b90aec5742f: Loading layer [==================================================>]  107.2MB/107.2MB
Loaded image: kodbox:v2

as4k@localhost Downloads % docker images | grep kod
kodbox                                            v2                  6ecd6fad52fb        11 minutes ago      513MB

可能遇到的问题

1 权限不足

chmod -R 777 /data 刷新浏览器