文章索引
1、创建Dockerfile目录
[root@linux-node1 ~]# mkdir /opt/dockerfile/lnmp/{nginx,mysql,php7} -p
2、编写mysql Dockerfile
继承自官方的MySQL5.7镜像,相对独立解耦的模块,无其它额外处理
[root@linux-node1 ~]# cd /opt/dockerfile/lnmp/mysql/ [root@linux-node1 mysql]# cat Dockerfile FROM mysql:5.7 MAINTAINER zhangyang zhangyang1@afocus.om.cn ENV TZ "Asia/Shanghai"
3、编写PHP7 Dockerfile
继承自官方的centos7镜像,PHP源码按需编译安装,方便后续扩展管理或者配置。通过sed修改php.in监听所有9000端口(此处为关键!!因为提供Nignx容器访问支持PHP脚本,PHP容器对于Nginx容器不是localhost)
[root@linux-node1 php]# cd /opt/dockerfile/lnmp/php7 [root@linux-node1 php]# cat Dockerfile FROM centos:centos7 MAINTAINER zhangyang zhangyang1@afocus.com.cn ENV TZ "Asia/Shanghai" #Dir RUN mkdir -p /usr/local/nginx/html #Yum RUN yum -y update && \ yum install -y gcc automake autoconf libtool make gcc-c++ vixie-cron wget zlib file openssl-devel && \ yum install -y sharutils zip bash vim cyrus-sasl-devel libmemcached libmemcached-devel libyaml libyaml-devel && \ yum install -y unzip libvpx-devel openssl-devel ImageMagick-devel autoconf tar gcc libxml2-devel && \ yum install -y gd-devel libmcrypt-devel libmcrypt mcrypt mhash libmcrypt libmcrypt-devel libxml2 libxml2-devel bzip2 bzip2-devel curl && \ yum install -y curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype-devel bison libtool-ltdl-devel net-tools && \ yum clean all #Php #fix error: mcrypt.h not found RUN rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm && \ yum install -y libmcrypt-devel RUN cd /tmp && \ wget http://cn2.php.net/distributions/php-7.0.12.tar.gz && \ tar xzf php-7.0.12.tar.gz && \ cd /tmp/php-7.0.12 && \ ./configure \ --prefix=/usr/local/php \ --with-mysqli \ --with-pdo-mysql \ --with-iconv-dir \ --with-freetype-dir \ --with-jpeg-dir --with-png-dir \ --with-zlib \ --with-libxml-dir \ --enable-simplexml \ --enable-xml \ --disable-rpath \ --enable-bcmath \ --enable-soap \ --enable-zip \ --with-curl \ --enable-fpm \ --with-fpm-user=nobody \ --with-fpm-group=nobody \ --enable-mbstring \ --enable-sockets \ --with-mcrypt \ --with-gd \ --enable-gd-native-ttf \ --with-openssl \ --with-mhash \ --enable-opcache && \ make && \ make install #复制文件 RUN cp /tmp/php-7.0.12/php.ini-production /usr/local/php/lib/php.ini && \ cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && \ cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf EXPOSE 9000 #配置php RUN sed -i -e 's/listen = 127.0.0.1:9000/listen = 9000/' /usr/local/php/etc/php-fpm.d/www.conf #启动php ENTRYPOINT ["/usr/local/php/sbin/php-fpm", "-F", "-c", "/usr/local/php/lib/php.ini"]
4、编写nginx Dockerfile
[root@linux-node1 php]# cd /opt/dockerfile/lnmp/nginx [root@linux-node1 nginx]# cat Dockerfile FROM centos:centos7 MAINTAINER zhangyang zhangyang1@afocus.com.cn ENV TZ "Asia/Shanghai" #Yum RUN yum -y update && \ yum install -y gcc automake autoconf libtool make gcc-c++ vixie-cron wget && \ yum install -y zlib file openssl-devel sharutils zip bash vim cyrus-sasl-devel && \ yum install -y libmemcached libmemcached-devel libyaml libyaml-devel unzip libvpx-devel && \ yum install -y openssl-devel ImageMagick-devel autoconf tar gcc libxml2-devel gd-devel libmcrypt-devel && \ yum install -y libmcrypt mcrypt mhash libmcrypt libmcrypt-devel libxml2 libxml2-devel bzip2 && \ yum install -y bzip2-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype-devel bison libtool-ltdl-devel net-tools && \ yum clean all #Nginx RUN cd /tmp && \ wget http://nginx.org/download/nginx-1.11.5.tar.gz && \ tar xzf nginx-1.11.5.tar.gz && \ cd /tmp/nginx-1.11.5 && \ ./configure \ --prefix=/usr/local/nginx \ --with-http_ssl_module --with-http_sub_module --with-http_dav_module --with-http_flv_module \ --with-http_gzip_static_module --with-http_stub_status_module --with-debug && \ make && \ make install #配置nginx ENV HTTP_PHP_CONFIG \\\n\\\t#php\\\n\\\tlocation ~ \\\\.php$ {\\\n\\\t\\\troot html;\\\n\\\t\\\tfastcgi_pass php7:9000;\\\n\\\t\\\tfastcgi_index index.php;\\\n\\\t\\\tfastcgi_param SCRIPT_FILENAME /usr/local/nginx/html\$fastcgi_script_name;\\\n\\\t\\\tinclude fastcgi_params;\\\n\\\t}\\\n\\\n\\\t RUN sed -i -e "s@# deny access to .htaccess files, if Apache@${HTTP_PHP_CONFIG}# deny access to .htaccess files, if Apache@" /usr/local/nginx/conf/nginx.conf EXPOSE 80 443 #启动nginx ENTRYPOINT ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
5、构建镜像
docker build --tag youngcheng/mysql -f mysql/Dockerfile . docker build --tag youngcheng/php7 -f php7/Dockerfile . docker build --tag youngcheng/nginx -f nginx/Dockerfile .
6、启动容器
整个流程可以看到,Nginx、PHP、MySQL三者的关系:
Nginx容器---->PHP容器,PHP容器---->MySQL容器。
即容器之间是有关联的,两两容器的数据通信通过容器启动命令docker run加参数--link解决。
docker run --name mysql -p 3306:3306 -v /root/bo/data/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -it youngcheung/mysql & docker run --name php7 -p 9000:9000 -v /var/www/html:/usr/local/nginx/html --link mysql:mysql -it youngcheung/php7 & docker run --name nginx -p 80:80 -v /var/www/html:/usr/local/nginx/html --link php7:php7 -it youngcheung/nginx
7、测试PHP及数据库连通情况
phpinfo
vi /var/www/html/phpinfo.php <?php phpinfo(); ?>
测试MySQL
[root@linux-node1 ~]# cat /var/www/html/mysql.php <?php $link_id=new PDO('mysql:host=mysql;port=3306;dbname=mysql;charset=utf8', 'root', '123456'); if($link_id){ echo "mysql successful by YoungCheung !"; }else{ echo mysql_error(); } ?>
lnmp创建完成!
!评论内容需包含中文