使用Dockerfile定制LNMP环境镜像
使用Dockerfile定制LNMP环境镜像
LNMP是继LAMP之后的又一个非常流行的web框架,即Linux+Nginx+Mysql+PHP的网站架构方案。nginx相较于apache更轻量级,尤其是对静态页面的处理更有优势。做运维的朋友应该都知道一个流行的词汇——动静态分离,其中一个比较普遍的是使用nginx处理静态页面,而动态页面交由后端的apache或者tomcat处理。本文要讲的是通过Dockerfile构建LNMP环境镜像,分别把nginx、mysql和PHP部署在docker容器里,实现LNMP网站架构。
一、项目环境介绍
1)系统环境
操作系统版本:Centos 7.5 64位
Docker版本:18.06.1-ce(社区版)
Nginx版本:1.15.5
PHP版本:7.2.11
Mysql版本:8.0.12
ip地址:192.168.2.2
lnmp网络ip地址:172.18.0.1
2)源码包下载地址
nginx下载地址:http://nginx.org/download/nginx-1.15.5.tar.gz
php下载地址:http://cn2.php.net/get/php-7.2.11.tar.gz/from/this/mirror
mysql使用官方8.0.12镜像
3)目录结构
1. 下载目录树
[[email protected] /]# yum -y install tree
2. 创建dockerfile的目录
[[email protected] /]# mkdir /dockerfile/
[[email protected] /]# cd /dockerfile/
[[email protected] dockerfile]# mkdir nginx
[[email protected] dockerfile]# mkdir php
[[email protected] dockerfile]# cd php
上传php-7.2.11.tar.gz的安装包
3. 构建镜像
1)编写Dockerfile文件
[[email protected] php]# vim Dockerfile
内容如下:
FROM centos:7
ENV TIME_ZOME Asia/Shanghai
ARG PV="php-7.2.11"
ADD $PV.tar.gz /tmp
RUN yum -y install gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel bison libm* \
&& mkdir /data \
&& cd /tmp/$PV \
&& ./configure --prefix=/data/php --with-config-file-path=/data/php/etc --with-gd --with-mysqli --with-openssl --with-zlib --with-curl --with-jpeg-dir --with-png-dir --with-iconv --enable-fpm --enable-zip --enable-mbstring --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt=/data/php/libmcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d \
&& make -j 4 \
&& make install \
&& cp /data/php/etc/php-fpm.conf.default /data/php/etc/php-fpm.conf \
&& cp /data/php/etc/php-fpm.d/www.conf.default /data/php/etc/php-fpm.d/www.conf \
&& sed -i '/;daemonize/a\daemonize = no' /data/php/etc/php-fpm.conf \
&& sed -i 's/127.0.0.1/0.0.0.0/g' /data/php/etc/php-fpm.d/www.conf \
&& echo "${TIME_ZOME}" > /etc/timezone \
&& ln -sf /usr/share/zoneinfo/${TIME_ZOME} /etc/localtime \
&& rm -rf /tmp/php* \
&& yum clean all \
&& yum -y remove gcc gcc-c++ make
WORKDIR /data/php/
EXPOSE 9000
CMD ["sbin/php-fpm","-c","etc/php-fpm.conf"]
注:daemonize = no表示fpm在前台运行
2)构建PHP镜像
[[email protected] php]# docker build -t php:7.2.11 .
此时会先下载centos镜像,然后再构建新镜像,时间会比较久,可喝杯茶耐心等待
3)报错问题解决方法
如果yum语句无法执行的话,并且报以下警告:
[Warning] IPv4 forwarding is disabled. Networking will not work.
这个问题是由于IPv4转发被禁用了,导致无法上网,需要将IPv4转发开启
解决方法:
vim /usr/lib/sysctl.d/00-system.conf
添加如下内容
net.ipv4.ip_forward=1
重启network服务
systemctl restart network
4. 构建nginx镜像
1)编写Dockerfile文件
[[email protected] php]# cd ../nginx/
[[email protected] nginx]# vim Dockerfile
内容如下:
FROM centos:7
ENV TIME_ZOME Asia/Shanghai
ARG NV="nginx-1.15.5"
COPY nginx.conf /data/nginx/conf/
ADD $NV.tar.gz /tmp
RUN yum -y install gcc gcc-c++ make openssl-devel pcre-devel zlib-devel \
&& mkdir -p /var/tmp/nginx/client \
&& groupadd nginx \
&& useradd nginx -g nginx -s /sbin/nologin \
&& chown -R nginx /var/tmp/nginx/client \
&& mkdir -p /data \
&& cd /tmp/$NV \
&& ./configure --prefix=/data/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --with-pcre \
&& make -j 2 \
&& make install \
&& echo "${TIME_ZOME}" > /etc/timezone \
&& ln -sf /usr/share/zoneinfo/${TIME_ZOME} /etc/localtime \
&& rm -rf /tmp/nginx* \
&& yum clean all \
&& yum -y remove gcc gcc-c++ make
WORKDIR /data/nginx/
EXPOSE 80
CMD ["./sbin/nginx","-g","daemon off;"]
2)准备好nginx.conf的配置文件
[[email protected] nginx]# vim nginx.conf
内容如下:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root html;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
3)上传nginx:1.15.5安装包
4)构建nginx镜像
[[email protected] nginx]# docker build -t nginx:1.15.5 .
5. 下载mysql镜像
建议直接使用mysql官方镜像,我测试过源码编译安装,最后生成的镜像大小3G多,而官方镜像只有400多M,而且源码安装操作步骤比较复杂,所以这里我弃用了源码安装,而选择直接使用官方镜像。
1)下载镜像
[[email protected] nginx]# cd /
[[email protected] /]# docker pull mysql:8.0.12
2)查看已经构建完成的镜像情况
[[email protected] /]# docker images
6. 创建并启动容器
1)创建自定义网络lnmp
[[email protected] /]# docker network create lnmp
2)创建生成mysql、php、nginx容器的脚本
[[email protected] /]# cd dockerfile/
[[email protected] dockerfile]# vim docker_lnmp.sh
docker_lnmp.sh脚本内容如下
#!/bin/bash
function mysql()
{
docker run --name mysql --restart=always --net lnmp -p 3306:3306 \
-v /data/mysql/data:/var/lib/mysql \
-v /data/mysql/conf:/etc/mysql/conf.d \
-v /data/mysql/logs:/logs \
-e MYSQL_ROOT_PASSWORD=test123456 \
-d mysql:8.0.12 --character-set-server=utf8
}
function php()
{
docker run --name php --restart=always --net lnmp \
-v /data/nginx/html:/data/nginx/html \
-v /data/php/log:/data/php/var/log \
-itd php:7.2.11
}
function nginx()
{
docker run --name nginx --restart=always --net lnmp -p 80:80 \
-v /data/nginx/html:/data/nginx/html \
-v /data/nginx/logs:/data/nginx/logs \
-itd nginx:1.15.5
}
$1
注:php和nginx必须都要挂载网站目录
3)启动mysql、php、nginx容器
[[email protected] dockerfile]# sh docker_lnmp.sh mysql
[[email protected] dockerfile]# sh docker_lnmp.sh php
[[email protected] dockerfile]# sh docker_lnmp.sh nginx
4)查看容器是否正常启动
[[email protected] dockerfile]# docker ps
5)修改mysql的密码加密方式为mysql_native_password
[[email protected] dockerfile]# vim /data/mysql/conf/docker_mysql.cnf
内容如下:
[mysqld]
default-authentication-plugin=mysql_native_password
如果不修改加密方式的话,低版本的mysql客户端登陆时会报错
6)重启mysql容器
[[email protected] dockerfile]# docker restart mysql
7.测试
1)创建mysql测试账号
[[email protected] dockerfile]# docker exec -it mysql /bin/bash
[email protected]:/# mysql -uroot -ptest123456
mysql> create database test;
mysql> create user [email protected] identified by '123456';
mysql> grant all privileges on test.* to [email protected];
mysql> exit
[email protected]:/# exit
注:这里需要对php容器授权访问,使用php.lnmp
2)编写一个测试连接mysql的php文件
[[email protected] dockerfile]# vim /data/nginx/html/index.php
内容如下:
<?php
echo "Hello PHP<br/>";
$conn = mysqli_connect("mysql","test","123456");
if(!$conn){
echo "连接数据库失败";
}else{
echo "连接数据库成功";
}
phpinfo();
?>
3)使用浏览器访问:http://192.168.2.2/index.php
8. 制作镜像
1)将mysql容器制作成镜像
[[email protected] /]# docker commit -a "abc" -m "lnmp-mysql" mysql mysql:lnmp.8.0.12
[[email protected] /]# docker images
2)将nginx、mysql和php镜像打个标签
[[email protected] /]# docker tag mysql:lnmp.8.0.12 192.168.2.2:5000/mysql:lnmp.v1
[[email protected] /]# docker tag php:7.2.11 192.168.2.2:5000/php:lnmp.v1
[[email protected] /]# docker tag nginx:1.15.5 192.168.2.2:5000/nginx:lnmp.v1
[[email protected] /]# docker images
3)将镜像打包成tar文件,保存到 /目录下
[[email protected] /]# docker save -o /mysql-lnmp.v1.tar 192.168.2.2:5000/mysql
[[email protected] /]# docker save -o /php-lnmp.v1.tar 192.168.2.2:5000/php
[[email protected] /]# docker save -o /nginx-lnmp.v1.tar 192.168.2.2:5000/nginx