使用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. 下载目录树
[root@localhost /]# yum -y install tree
2. 创建dockerfile的目录
[root@localhost /]# mkdir /dockerfile/
[root@localhost /]# cd /dockerfile/
[root@localhost dockerfile]# mkdir nginx
[root@localhost dockerfile]# mkdir php
[root@localhost dockerfile]# cd php
上传php-7.2.11.tar.gz的安装包
3. 构建镜像
1)编写Dockerfile文件
[root@localhost 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镜像
[root@localhost 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文件
[root@localhost php]# cd ../nginx/
[root@localhost 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的配置文件
[root@localhost 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镜像
[root@localhost nginx]# docker build -t nginx:1.15.5 .
5. 下载mysql镜像
建议直接使用mysql官方镜像,我测试过源码编译安装,最后生成的镜像大小3G多,而官方镜像只有400多M,而且源码安装操作步骤比较复杂,所以这里我弃用了源码安装,而选择直接使用官方镜像。
1)下载镜像
[root@localhost nginx]# cd /
[root@localhost /]# docker pull mysql:8.0.12
2)查看已经构建完成的镜像情况
[root@localhost /]# docker images
6. 创建并启动容器
1)创建自定义网络lnmp
[root@localhost /]# docker network create lnmp
2)创建生成mysql、php、nginx容器的脚本
[root@localhost /]# cd dockerfile/
[root@localhost 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容器
[root@localhost dockerfile]# sh docker_lnmp.sh mysql
[root@localhost dockerfile]# sh docker_lnmp.sh php
[root@localhost dockerfile]# sh docker_lnmp.sh nginx
4)查看容器是否正常启动
[root@localhost dockerfile]# docker ps
5)修改mysql的密码加密方式为mysql_native_password
[root@localhost dockerfile]# vim /data/mysql/conf/docker_mysql.cnf
内容如下:
[mysqld]
default-authentication-plugin=mysql_native_password
如果不修改加密方式的话,低版本的mysql客户端登陆时会报错
6)重启mysql容器
[root@localhost dockerfile]# docker restart mysql
7.测试
1)创建mysql测试账号
[root@localhost dockerfile]# docker exec -it mysql /bin/bash
root@64a8cd55ee93:/# mysql -uroot -ptest123456
mysql> create database test;
mysql> create user test@php.lnmp identified by '123456';
mysql> grant all privileges on test.* to test@php.lnmp;
mysql> exit
root@64a8cd55ee93:/# exit
注:这里需要对php容器授权访问,使用php.lnmp
2)编写一个测试连接mysql的php文件
[root@localhost 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容器制作成镜像
[root@localhost /]# docker commit -a "abc" -m "lnmp-mysql" mysql mysql:lnmp.8.0.12
[root@localhost /]# docker images
2)将nginx、mysql和php镜像打个标签
[root@localhost /]# docker tag mysql:lnmp.8.0.12 192.168.2.2:5000/mysql:lnmp.v1
[root@localhost /]# docker tag php:7.2.11 192.168.2.2:5000/php:lnmp.v1
[root@localhost /]# docker tag nginx:1.15.5 192.168.2.2:5000/nginx:lnmp.v1
[root@localhost /]# docker images
3)将镜像打包成tar文件,保存到 /目录下
[root@localhost /]# docker save -o /mysql-lnmp.v1.tar 192.168.2.2:5000/mysql
[root@localhost /]# docker save -o /php-lnmp.v1.tar 192.168.2.2:5000/php
[root@localhost /]# docker save -o /nginx-lnmp.v1.tar 192.168.2.2:5000/nginx
使用Dockerfile定制LNMP环境镜像相关推荐
- 学习笔记五:dockerfile 构建生产环境镜像
dockerfile 构建生产环境镜像 dockerfile构建nginx镜像 dockerfile构建tomcat镜像 tomcat 的自动启动 把Go代码基于dockerfile做成镜像 把pyt ...
- docker安装redis提示没有日记写入权限_Docker 学习笔记(第六集:使用 Dockerfile 定制镜像)...
ethan 读完需要 11分钟 速读仅需 4 分钟 / 使用 Dockerfile 定制镜像 / 什么是 Dockerfile 呢? Dockerfile 是一个文本文档,其中包含用户可以在命令行上调 ...
- Docker Dockerfile 定制镜像
使用 Dockerfile 定制镜像 Dockerfile 指令详解 FROM 指定基础镜像 RUN 执行命令 构建镜像 镜像构建上下文(Context) 其它 docker build 的用法 直接 ...
- Docker用Dockerfile定制镜像
用Dockerfile定制镜像 镜像的定制实际上就是定制每一层所添加的配置.文件.如果我们可以把每一层修改.安装.构建.操作的命令都写入一个脚本,用这个脚本来构建.定制镜像,那之前提示的无法重复的问题 ...
- 使用 Dockerfile 定制镜像
目录 使用 Dockerfile 定制镜像 FROM 指定基础镜像 RUN 执行命令 构建镜像 镜像构建上下文(Context) 其它 docker build 的用法 直接用 Git repo 进行 ...
- 简单几步就教会你如何使用 dockerfile 制作 LNMP 镜像,还不赶紧学起来
简单几步就教会你如何使用dockerfile制作LNMP镜像 前言 一.dockerfile撰写php镜像 二.dockerfile撰写mysql镜像 三.dockerfile撰写nginx镜像 四. ...
- Docker : Dockerfile 定制镜像
使用 Dockerfile 定制镜像 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 镜像的定制实际上就是定制每一层所添加的配置.文件.如果我们 ...
- 用Dockerfile定制镜像
Dockerfile定制镜像 从刚才的 docker commit 的学习中,我们可以了解到,镜像的定制实际上就是定制每一层所添加的配置.文件.如果我们可以把每一层修改.安装.构建.操作的命令都写入一 ...
- 使用Dockerfile定制镜像来部署Tomcat项目
场景 使用Dockerfile定制镜像-定制Tomcat为例: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/99093026 实 ...
最新文章
- C# String部分方法的例子StringBuilder的简单介绍
- php屏幕抓取,PHP抓屏函数实现屏幕快照代码分享
- python and or 优先级
- datetime 取分钟_如何仅从DateTime获取小时和分钟
- Flex4与WebService通信
- Python Django 获取多条记录API
- elasticsearch和mysql排序问题
- php安装soap扩展
- plus 什么是mybais_谈谈自己用mybatis-plus中的一些经验。
- 详解nullable、firstpos、lastpos和followpos的计算规则
- ansible 容器部署_使用ansible-bender工具构建容器图像
- python实现sha1加密算法
- THREE.JS OrbitControls的参数设置
- 给员工授予svn相关权限
- suse12 sp4,sp5镜像资源分享
- 在pycharm内给自己的代码设置签名,附加时间
- 大数据技术专家王家林老师所有视频合集
- 计算机辅助化学教育,在计算机辅助下的化学教学_计算机辅助制造
- 决策理论(Decision theory)自动规划和调度(Automated planning and scheduling)(双语)
- 图片水印怎么去掉?图片水印去除方法