Dockerfile文件构建 LNMP 镜像
前言
不知道大家有没有在搜索 docker-compose构建lnmp环境 时只能看到docker-compose.yml文件,而看不到真正的 Dockerfile ,以下是我自己基于Dockerfile构建的LNMP编译文件!
注意: 如果你已经对 Dockerfile 的使用还不是很了解,请先阅读 Dockerfile 之后再阅读本篇文章,这样可以有助于更好的进行理解哦!
Dockerfile定制LNMP镜像示例
Nginx
# 指定基础镜像 FROM centos:centos7 # 作者 MAINTAINER Dx dxinsir@163.com # 更新yum源,安装Nginx依赖包 RUN yum -y update && \ yum install -y gcc make gcc-c++ openssl-devel libmcrypt-devel libmcrypt mcrypt libxml2 libxml2-devel bzip2 bzip2-devel curl curl-devel wget && \ yum clean all # 安装Nginx RUN cd /tmp \ && wget http://nginx.org/download/nginx-1.13.10.tar.gz \ && tar -xzf nginx-1.13.10.tar.gz \ && cd /tmp/nginx-1.13.10 \ && ./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 # 指定对外开放端口 EXPOSE 80 443 #前台挂起Nginx CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
小贴士:
如果你还不了解为什么要前台挂起Nginx,请看博主在 Dockerfile 中的CMD命令讲解,这里就不给大家一一讲解了。
PHP
#基础镜像 FROM centos:centos7 #作者 MAINTAINER Dx dxinsir@163.com #更新yum源,安装PHP依赖包 RUN yum -y update && \ yum install -y gcc \ gcc-c++ \ curl \ curl-devel \ libxml2 \ libxml2-devel \ openssl \ openssl-devel \ libcurl \ libcurl-devel \ libjpeg \ libjpeg-devel \ libpng \ libpng-devel \ freetype \ freetype-devel \ epel-release \ wget \ && yum install -y libmcrypt libmcrypt-devel \ && yum clean all #下载PHP、编译 RUN cd /tmp \ && wget http://cn2.php.net/get/php-7.1.16.tar.gz/from/this/mirror \ && mv mirror php-7.1.16.tar.gz \ && tar -xzf php-7.1.16.tar.gz \ && cd php-7.1.16 \ && ./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 \ # 编译好后默认都是用default为后缀的,需要我们copy一份用conf后缀为结尾 && 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 CMD ["/usr/local/php/sbin/php-fpm"]
小贴士:
如果你仔细观察了博主贴出来的这2个Dockerfile文件,相信你已经发现在nginx镜像中使用了 nginx -g daemon off前台挂起,而php镜像中直接就启动了呢?
由于php-fpm不可以使用命令进去前台挂起,所以我通过 挂载 php-fpm.conf 配置文件的方式改变为前台挂起的。
之前:
daemonize = yes
之后:
daemonize = no
如果你没有让php-fpm前台挂起,那么你会发现docker中的php-fpm中怎么样都启动不来哦!
Mysql
直接采用docker官方5.7版本的镜像库
构建镜像
进入 Dockerfile 所在目录下进行构建镜像
docker build -t nginx:1.13.10 . # -t 选项为新镜像设置名称:标签
Dockerfile下载地址
点击这里 下载我在 Github 上发布的 dockerfile 镜像文件和 docker-compose.yml 脚本 以及LNMP所需要的配置文件,或者通过 Git 下载
git clone https://github.com/18911377489/Docker-compose-LNMP.git
总结
那么到这里对于LNMP的Docerfile编写已经圆满结束了,如果你希望使用 docker-compose 构建LNMP,请 点击这里 哦!