1. 配置容器,生成鏡像
1.1 拉取centos鏡像
docker pull centos:centos7
1.2 創(chuàng)建centos容器
docker run --name centos -p 80:80 --privileged=true -d centos:centos7 /usr/sbin/init
// 進(jìn)入centos容器
docker exet -it centos /bin/bash
1.3 在容器內(nèi)安裝apache服務(wù)
yum install initscripts -y
yum install httpd
chkconfig httpd on // 設(shè)置開機(jī)啟動(dòng)
service httpd start // 啟動(dòng)apache服務(wù)
1.4 在容器內(nèi)安裝 php7.4
...
1.5 導(dǎo)出為鏡像
docker export 容器ID > centos-php.tar
// 刪除容器
docker stop 容器ID
docker rm 容器ID
// 刪除鏡像
docker rmi centos
2. 使用centos-php鏡像,配置php服務(wù)
2.1 導(dǎo)出為鏡像
cat centos-php.tar | sudo docker import - centos:php
2.2 創(chuàng)建容器
docker run --name php -p 80:80 -v /root/data/php:/var/www/html/ --privileged=true -d centos:php /usr/sbin/init
// 進(jìn)入容器
docker exec -it php /bin/bash
2.3 修改配置文件
// 配置文件目錄
vi /ect/httpd/conf/httpd.conf
// 修改php主目錄
DocumentRoot "/var/www/html/php"
// 添加php項(xiàng)目目錄
<Directory "/var/www/html/public/php">
Order allow,deny
Allow from all
AllowOverride All
</Directory>
2.4 遇到問題
問題1: failed to open stream: Permission denied
解決:chown -R apache /var/www/html