Docke--Dockerfile 构建LNMP环境

Posted yanjieli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docke--Dockerfile 构建LNMP环境相关的知识,希望对你有一定的参考价值。

Dockerfile 构建nginx并结合php

1、构建基础镜像

先构建一个基础镜像,添加repo的环境和编译的环境,而centos镜像就是初始的官方镜像,后面构建php、nginx、mysql都使用该镜像为base image:

[[email protected] myCentos]# cat Dockerfile   #查看Dockerfile文件
# base image
FROM centos

# MAINTAINER
MAINTAINER 381347268@qq.com

# add epel and 163 yum
RUN yum install wget epel-release -y     && mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.back     && wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/Centos-7.repo 
    && wget -P /etc/yum.repos.d/ http://mirrors.163.com/.help/CentOS7-Base-163.repo 
    && yum clean all && yum makecache

# Necessary packages
RUN yum install -y  wget gcc gcc-c++ glibc make autoconf openssl openssl-devel ntpdata crontabs

# change timzone to Asia/Shanghai
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[[email protected] myCentos]# docker build -t centos:v1 .  #构建镜像取名为centos:v1
[[email protected] myCentos]# docker images  #构建完成后查看该镜像
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
centos              v1                  cca50f986518        About a minute ago   665MB

查看nginx和php的目录结构:

[[email protected] ~]# tree nginx
nginx
├── Dockerfile
├── fastcgi_params
├── nginx-1.8.1.tar.gz
├── nginx.conf
└── www.conf
[[email protected] ~]# tree php
php
├── Dockerfile
├── libmcrypt-2.5.8.tar.gz
├── php-5.6.35.tar.gz
├── php-fpm.conf.default
└── php.ini-production

这里详细描述下nginx和php的构建过程,已经构建过程中用到的所有工具包和配置文件

2、构建nginx镜像

首先构建nginx镜像,查看nginx的Dockerfile:

[[email protected] nginx]# cat Dockerfile 
# base image
FROM centos:v1

MAINTAINER 381347268@qq.com

RUN useradd -M -s /sbin/nologin www
ADD nginx-1.8.1.tar.gz /usr/local/src

# install Dependency package
RUN yum install libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel libxml2 libxml2-dev libxslt-devel

WORKDIR /usr/local/src/nginx-1.8.1

# make && make install
RUN ./configure --user=www --group=www --prefix=/usr/local/nginx --with-file-aio --with-ipv6 --with-http_ssl_module  --with-http_spdy_module --with-http_realip_module    --with-http_addition_module    --with-http_xslt_module   --with-http_image_filter_module    --with-http_geoip_module  --with-http_sub_module  --with-http_dav_module --with-http_flv_module    --with-http_mp4_module --with-http_gunzip_module  --with-http_gzip_static_module  --with-http_auth_request_module  --with-http_random_index_module   --with-http_secure_link_module   --with-http_degradation_module   --with-http_stub_status_module && make && make install

# configure
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
COPY fastcgi_params /usr/local/nginx/conf/fastcgi_params
RUN mkdir /usr/local/nginx/conf/conf.d
COPY www.conf /usr/local/nginx/conf/conf.d/www.conf

EXPOSE 80

CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]

这里nginx采用了编译安装,创建了用户www和安装了nginx的一些依赖包,copy了一些配置文件到镜像中。详细配置文件参考:

通过Dockerfile构建nginx镜像:

[[email protected] nginx]# docker build -t nginx:v1 .  #构建nginx镜像,tag为v1
[[email protected] nginx]# docker images   #查看构建完成的镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               v1                  c1c90aaefa1e        33 seconds ago      752MB
centos              v1                  cca50f986518        2 hours ago         665MB

特别说明下配置文件:

[[email protected] nginx]# cat www.conf 
server {
    listen   80;
    root /usr/local/nginx/html;
    index index.htm index.html index.php;
    location ~ .php$ {
        root /usr/local/nginx/html;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

可以发现fastcgi_pass php:9000;

这是因为php也是一个容器,和nginx是隔离的,后面启动容器的时候nginx将会通过--link的方式与镜像进行互联访问

3、构建php镜像

查看php的Dockerfile:

[[email protected] php]# cat Dockerfile 
# base image
FROM centos:v1

MAINTAINER 381347268@qq.com

ADD libmcrypt-2.5.8.tar.gz /usr/local/src

WORKDIR /usr/local/src/libmcrypt-2.5.8
RUN ./configure && make && make install

ADD php-5.6.35.tar.gz /usr/local/src

RUN yum -y install libxml2 libxml2-devel bzip2 bzip2-devel libjpeg-turbo libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel libcurl libcurl-devel

WORKDIR /usr/local/src/php-5.6.35
RUN ./configure --prefix=/usr/local/php --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-mysql=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-mcrypt --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets --enable-fpm --with-config-file-path=/usr/local/php/etc --with-bz2 --with-gd && make && make install


COPY php.ini-production /usr/local/php/etc/php.ini
COPY php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

RUN useradd -M -s /sbin/nologin php
RUN sed -i -e [email protected];pid = run/[email protected] = run/[email protected] -e [email protected]@[email protected] -e [email protected] = 127.0.0.1:[email protected] = 0.0.0.0:[email protected] /usr/local/php/etc/php-fpm.conf
RUN sed -i [email protected];daemonize = [email protected] = [email protected] /usr/local/php/etc/php-fpm.conf


EXPOSE 9000

CMD ["/usr/local/php/sbin/php-fpm"]

构建的服务必须运行在前台,而对于nginx来说:daemon off 表示将后台运行关闭了,于是运行在前台

而对于php:sed -i ‘[email protected];daemonize = [email protected] = [email protected]‘ /usr/local/php/etc/php-fpm.conf

这里也是将daemon模式关闭了,于是/usr/local/php/sbin/php-fpm 运行在前台

通过Dockerfile构建php镜像:

[[email protected] php]# docker build -t php:v1 .  #构建php镜像,tag为v1
[[email protected] php]# docker images  #查看构建完成的镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
php                 v1                  2019612cc0a4        35 seconds ago      1.19GB
nginx               v1                  c1c90aaefa1e        About an hour ago   752MB
centos              v1                  cca50f986518        2 hours ago         665M

4、启动nginx容器和php容器

利用构建的镜像启动php、nginx服务:

[[email protected] php]# docker run -d --name php -v /www:/usr/local/nginx/html php:v1  #启动一个php容器并通过-v进行映射
cf77c85e7c2f63a7ac2db2edaa37d22418503e8cfa35d87b9cfad541d03e004a
[[email protected] php]# docker run -d --name nginx -p 80:80 -v /www:/usr/local/nginx/html --link=php:php nginx:v1  #启动一个nginx容器并通过-v进行映射及--link进行关联php容器
3bbc12db8e9cc118a7824c5a314d13c4e37f3a6397f32bbfe939f9e424b306e0

上面启动php的容器时,使用了-v进行映射,如果这里不进行映射,那么php的程序会启动,但是遇到php结尾的文件将不会解析,出现file not found的错误

查看容器状态:

[[email protected] php]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                NAMES
3bbc12db8e9c        nginx:v1            "/usr/local/nginx/sb…"   26 seconds ago      Up 25 seconds               0.0.0.0:80->80/tcp   nginx
cf77c85e7c2f        php:v1              "/usr/local/php/sbin…"   2 minutes ago       Up 2 minutes                9000/tcp             php

在宿主机上到网站目录创建测试文件:

[[email protected] www]# tree .  #查看网站目录的结构
.
├── index.php
└── test.html
[[email protected] www]# cat index.php   #查看index.php文件
<?php phpinfo() ?>
[[email protected] www]# cat test.html   #查看test.html文件
<h1>Hello Docker nginx-php</h1>

访问测试:

技术分享图片

技术分享图片

进入到php容器查看hosts文件:

[[email protected] ~]# docker exec -it php /bin/bash  #使用exec进入php容器
[[email protected] php-5.6.35]# cat /etc/hosts
127.0.0.1    localhost
::1    localhost ip6-localhost ip6-loopback
fe00::0    ip6-localnet
ff00::0    ip6-mcastprefix
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters
172.17.0.2    cf77c85e7c2f

进入到nginx容器查看hosts文件:

[[email protected] ~]# docker exec -it nginx /bin/bash  #使用exec进入nginx容器
[[email protected] nginx-1.8.1]# cat /etc/hosts
127.0.0.1    localhost
::1    localhost ip6-localhost ip6-loopback
fe00::0    ip6-localnet
ff00::0    ip6-mcastprefix
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters
172.17.0.2    php cf77c85e7c2f
172.17.0.3    3bbc12db8e9c

可以看到nginx的hosts文件中有一条php的解析,这就是为什么nginx能够和php进行通信的缘由(通过--link进行指定)

 

基于上面的步骤,nginx和php的连接就ok了,下面添加一个mysql 然后测试部署wordpress

5、构建mysql镜像

查看mysql的目录结构:

[[email protected] mysql]# tree .
.
├── business.sql
├── Dockerfile
├── MariaDB-10.0.33-centos7-x86_64-client.rpm
├── MariaDB-10.0.33-centos7-x86_64-common.rpm
├── MariaDB-10.0.33-centos7-x86_64-compat.rpm
├── MariaDB-10.0.33-centos7-x86_64-server.rpm
├── server.cnf
└── setup.sh

查看mysql的Dockerfile:

[[email protected] mysql]# cat Dockerfile 
FROM centos

MAINTAINER 381347268@qq.com

COPY MariaDB-10.0.33-centos7-x86_64-client.rpm /root/MariaDB-10.0.33-centos7-x86_64-client.rpm
COPY MariaDB-10.0.33-centos7-x86_64-common.rpm /root/MariaDB-10.0.33-centos7-x86_64-common.rpm 
COPY MariaDB-10.0.33-centos7-x86_64-compat.rpm  /root/MariaDB-10.0.33-centos7-x86_64-compat.rpm
COPY MariaDB-10.0.33-centos7-x86_64-server.rpm  /root/MariaDB-10.0.33-centos7-x86_64-server.rpm
WORKDIR /root
RUN yum remove mysql-libs -y
RUN yum -y install *.rpm
ADD business.sql /root/business.sql
ADD server.cnf /etc/my.cnf.d/server.cnf
ADD setup.sh /root/setup.sh

RUN yum clean all
RUN chmod +x /root/setup.sh
EXPOSE 3306

CMD ["/root/setup.sh"]

(1)这里所使用的基础镜像是centos(官方镜像,latest);(2)配置文件通过后面启动时挂载进去,(如果后面有更改,只需要在某个目录创建好配置文件,然后进行挂载即可);(3)business.sql是关于执行的sql(赋予root密码,创建数据库等等);(4)setup.sh是执行了重新初始化mysql(mariadb),最后重启了服务(使用了 --user=mysql,避免出现了使用root用户启动mariadb)

setup.sh脚本原理:Dockerfile用来安装数据库服务,安装完成后,通过setup.sh脚本重新将mysql进行初始化  初始化后,开启MySQL服务执行setup.sh中的sql,关闭mysql数据库 重新以前台的方式启动MySQL数据库服务

技术分享图片
[[email protected] mysql]# cat setup.sh 
#!/bin/sh
chown -R mysql:mysql /var/lib/mysql

mysql_install_db --user=mysql > /dev/null

mysqld_safe --user=mysql &

sleep 5

mysql < /root/business.sql

sleep 5

ps -wef | grep mysql | grep -v grep | awk {print $2} | xargs kill -9

mysqld_safe --user=mysql
setup.sh
技术分享图片
[[email protected] mysql]# cat business.sql 
create database wordpress DEFAULT CHARACTER SET utf8;

USE mysql;
GRANT ALL PRIVILEGES ON *.* TO root@% WITH GRANT OPTION;
FLUSH PRIVILEGES;

UPDATE user SET password=PASSWORD("root") WHERE user=root;
FLUSH PRIVILEGES;
business.sql
技术分享图片
[[email protected] mysql]# cat server.cnf 
[mysqld]
bind-address=0.0.0.0
console=1
general_log=1
general_log_file=/dev/stdout
collation-server=utf8_unicode_ci
character-set-server=utf8
server.cnf

通过Dockerfile构建mysql镜像:

[[email protected] mysql]# docker build -t mysql:v1 .  #构建mysql镜像

查看构建完成的镜像:

[[email protected] mysql]# docker images  #查看三个镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               v1                  4a3e428817c1        2 hours ago         813MB
php                 v1                  2019612cc0a4        3 hours ago         1.19GB
nginx               v1                  c1c90aaefa1e        4 hours ago         752MB

 

6、部署lnmp环境及wordpress

基于上面的三个镜像 nginx、mysql、php构建lnmp环境,我们先将上面启动的容器给删除掉,因为没有什么用了

先启动三个容器:

[[email protected] mysql]# docker run -d --name mysql -v /root/mysql/server.cnf:/etc/my.cnf.d/server.cnf -v /data/mysql:/var/lib/mysql -p 3306:3306 mysql:v1  #启动一个mysql容器
df28bcb97289676654ea7b573b0e3e52f910a599ff03b2dbf2dfe6345726127d
[[email protected] mysql]# docker run -d --name php -v /www:/usr/local/nginx/html --link=mysql:mysql php:v1  #启动一个php容器
2f7fd098f0069deeaef4eb5b067e981c6be01a16d0769c3bc4203eab03a45c40
[[email protected] mysql]# docker run -d --name nginx -p 80:80 -v /www:/usr/local/nginx/html --link=php:php nginx:v1  #启动一个nginx容器
b68688cd6f2d324e58c469cfb49fa7a14257ef2bb0c301a40a36ef244201c159
[[email protected] mysql]# docker ps  #查看启动的三个容器的状态
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                    NAMES
b68688cd6f2d        nginx:v1            "/usr/local/nginx/sb…"   11 seconds ago       Up 10 seconds       0.0.0.0:80->80/tcp       nginx
2f7fd098f006        php:v1              "/usr/local/php/sbin…"   About a minute ago   Up About a minute   9000/tcp                 php
df28bcb97289        mysql:v1            "/root/setup.sh"         About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp   mysql

 

这里阐述下这里启动的这三个容器:(1)先启动一个mysql容器, 并且将上面的server.cnf配置文件和数据目录挂载到了容器内部,这样就防止了数据的丢失;(2)接着启动一个php容器,使用了-v进行映射,如果这里不进行映射,那么php的程序会启动,但是遇到php结尾的文件将不会解析,出现file not found的错误,并且使用了 --link 与mysql 关联,这样php容器就可以连接mysql数据库了;(3)最后启动一个nginx容器,同样使用了-v 进行映射,将网站目录映射到宿主机,通过 --link与php 关联

开始安装wordpress:

[[email protected] ~]# cd /www/
[[email protected] www]# wget https://cn.wordpress.org/wordpress-4.7.4-zh_CN.tar.gz
[[email protected] www]# tar xf wordpress-4.7.4-zh_CN.tar.gz
[[email protected] www]# ll
总用量 8324
-rw-r--r-- 1 root   root           19 1月  11 12:27 index.php
-rw-r--r-- 1 root   root           32 1月  11 12:04 test.html
drwxr-xr-x 5 nobody nfsnobody    4096 4月  23 2017 wordpress
-rw-r--r-- 1 root   root      8507412 4月  23 2017 wordpress-4.7.4-zh_CN.tar.gz

通过浏览器访问宿主机进行安装:http://IP/wordpress

技术分享图片

技术分享图片

...省略...

 技术分享图片

 

以上是关于Docke--Dockerfile 构建LNMP环境的主要内容,如果未能解决你的问题,请参考以下文章

Docke--Dockerfile实践

LNMP平台对接redis服务(可以再想一想,手臂环住那个人的温度。但是眼皮被太阳照得通红,号啕的伤心突然也就这么回事。)

构建LNMP+WordPress

20200720记录:部署LNMP环境,构建LNMP平台,地址重写

LNMP平台构建实验 +bbs社区搭建

构建LNMP平台