dockerfile lnmp构建

(1)、构建nginx镜像
查看dockerfile-nginx目录结构
dockerfile lnmp构建
查看nginx的Dockerfile:
[root@node0 ~]# cat /root/dockerfile/dockerfile-nginx/Dockerfile

# base image
FROM centos:centos7.3.1611

# MAINTAINER
MAINTAINER yang@huadingyun.cn

# add epel and aliyun 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 -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo \
    && yum clean all && yum makecache

# Necessary packages
RUN yum install -y  vim gcc gcc-c++ glibc make autoconf gd gd-devel GeoIP GeoIP-devel pcre pcre-devel openssl openssl-devel libxml2 libxml2-dev libxslt-devel bzip2 bzip2-devel libjpeg-turbo libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel libcurl libcurl-devel perl perl-devel perl-ExtUtils-Embed

# change timzone to Asia/Shanghai
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

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

WORKDIR /usr/local/src/nginx-1.6.3
# 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 --with-http_perl_module --with-ld-opt="-Wl,-E" && make && make install

# configure
RUN mkdir /usr/local/nginx/conf/vhost
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
COPY vhost/city8094.conf /usr/local/nginx/conf/vhost/city8094.conf
COPY vhost/city9193.conf /usr/local/nginx/conf/vhost/city9193.conf
COPY vhost/gongying8092.conf /usr/local/nginx/conf/vhost/gongying8092.conf
COPY vhost/gongying9192.conf /usr/local/nginx/conf/vhost/gongying9192.conf
COPY vhost/shangjia8090.conf /usr/local/nginx/conf/vhost/shangjia8090.conf
COPY vhost/shangjia9190.conf /usr/local/nginx/conf/vhost/shangjia9190.conf
COPY vhost/gateway.conf /usr/local/nginx/conf/vhost/gateway.conf
COPY vhost/gateway-test.conf /usr/local/nginx/conf/vhost/gateway-test.conf
COPY vhost/web.conf /usr/local/nginx/conf/vhost/web.conf
COPY vhost/webssl.conf /usr/local/nginx/conf/vhost/webssl.conf
COPY vhost/php1.conf /usr/local/nginx/conf/vhost/php1.conf
COPY 404.html /usr/local/nginx/html

EXPOSE 80
EXPOSE 443

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

这里nginx采用了编译安装,创建了用户www和安装了nginx的一些依赖包,copy了一些配置文件到镜像中。
通过Dockerfile构建nginx:v1.6.3
[root@node0 dockerfile-nginx]# docker build -t nginx:v1.6.3 .
此时会生成一个centos:centos7.3.1611镜像, 给镜像重新打上标签, centos:v7.3.1611 作为构建php, mariadb镜像的“base image”
# docker tag centos:centos7.3.1611 centos:v7.3.1611
# docker rmi centos:centos7.3.1611

特别说明以下配置文件
[root@node0 vhost]# cat php1.conf

server {
    listen   80;
    server_name yang.huadingyun.cn;
    root /home/yang;
    index index.htm index.html index.php;
    location ~ \.php$ {
        root /home/yang;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    error_page  404   /404.html;
     location = /404.html {
     root html;
    }
   access_log  logs/yang.access.log main;
   error_log   logs/yang_error.log error;
}

可以发现fastcgi_pass php:9000; 其中“php”为后面待创建的php容器的名称。

(2)、构建php镜像
查看dockerfile-php目录结构
dockerfile lnmp构建
查看php的Dockerfile:
[root@node0 ~]# cat /root/dockerfile/dockerfile-php/Dockerfile

# base image
FROM centos:v7.3.1611
MAINTAINER yang@huadingyun.cn

RUN yum install -y vim gcc gcc-c++ glibc make autoconf gd gd-devel GeoIP GeoIP-devel pcre pcre-devel openssl openssl-devel libxml2 libxml2-dev libxslt-devel bzip2 bzip2-devel libjpeg-turbo libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel libcurl libcurl-devel perl perl-devel perl-ExtUtils-Embed

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
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 's@;pid = run/php-fpm.pid@pid = run/php-fpm.pid@g' -e 's@nobody@php@g' -e 's@listen = 127.0.0.1:9000@listen = 0.0.0.0:9000@g' /usr/local/php/etc/php-fpm.conf
RUN sed -i 's@;daemonize = yes@daemonize = no@g' /usr/local/php/etc/php-fpm.conf

EXPOSE 9000

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

构建的服务必须运行在前台,而对于nginx来说:daemon off 表示将后台运行关闭了,于是运行在前台。而对于php:sed -i 's@;daemonize = yes@daemonize = no@g' /usr/local/php/etc/php-fpm.conf 这里也是将daemon模式关闭了,于是/usr/local/php/sbin/php-fpm 运行在前台。
通过Dockerfile构建php镜像php:v5.6.35
[root@node0 dockerfile-php]# docker build -t php:v5.6.35 .

(3)、构建mariadb镜像
查看dockerfile-mariadb目录结构
dockerfile lnmp构建
查看mariadb的Dockerfile:
[root@node0 ~]# cat /root/dockerfile/dockerfile-mariadb/Dockerfile

FROM centos:v7.3.1611
MAINTAINER yang@huadingyun.cn

COPY mariadb.repo /etc/yum.repos.d/
RUN yum remove  mysql mysql-server mysql-libs compat-mysql51
RUN yum -y install mariadb-server mariadb

WORKDIR /root
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"]

说明:mariadb的Dockerfile中的business.sql是关于执行的sql(赋予root密码,创建数据库等等)
setup.sh是执行了重新初始化mariadb,最后重启了服务(使用了 --user=mysql,避免出现了使用root用户启动mariadb)
setup.sh脚本原理:Dockerfile用来安装数据库服务,安装完成后通过setup.sh脚本重新将mysql进行初始化, 开启MySQL服务执行setup.sh中的sql,关闭mysql数据库,重新以前台的方式启动MySQL数据库服务。

通过Dockerfile构建mariadb镜像mariadb:v10.5.1
[root@node0 dockerfile-mariadb]# docker build -t mariadb:v10.5.1 .
至此,nginx,php,mariadb的镜像构建完毕。
查看构建完成的镜像:
dockerfile lnmp构建

(4)、创建基于lnmp容器
1、先启动一个mariadb容器, 并且将上面的server.cnf配置文件和数据目录挂载到了容器内部,这样就防止了数据的丢失;
# mkdir -p /docker/mysqlconf
# cp /root/dockerfile/dockerfile-mariadb/server.cnf /docker/mysqlconf/

 # docker run -d --name mariadb -v /docker/mysqlconf/server.cnf:/etc/my.cnf.d/server.cnf -v /docker/mysqldata/mysql:/var/lib/mysql -p 3306:3306 --restart=always mariadb:v10.5.1

2、接着启动一个php容器,使用了-v进行映射,如果这里不进行映射,那么php的程序会启动,但是遇到php结尾的文件将不会解析,出现file not found的错误,并且使用了 --link 与mariadb关联,这样php容器就可以连接mariadb数据库了;

# docker run -d --name php -v /home/yang:/home/yang -v /usr/src/nginx-1.6.0/html:/usr/src/nginx-1.6.0/html --link=mariadb --restart=always php:v5.6.35

3、最后启动一个nginx容器,同样使用了-v 进行映射,将网站目录映射到宿主机,通过 --link与php 关联。

# mkdir -p /docker/nginxconf
# cp /root/dockerfile/dockerfile-nginx/nginx.conf /docker/nginxconf/
# mkdir -p /docker/nginxconf/vhost
# cp -r /root/dockerfile/dockerfile-nginx/vhost/* /docker/nginxconf/vhost

# docker run -d --name nginx -p 80:80 -p 443:443 -v /docker/nginxconf/nginx.conf:/usr/local/nginx/conf/nginx.conf -v /docker/nginxconf/vhost:/usr/local/nginx/conf/vhost -v /docker/nginxlog:/usr/local/nginx/logs -v /home/yang:/home/yang -v /usr/src/nginx-1.6.0/html:/usr/src/nginx-1.6.0/html --link=php --restart=always nginx:v1.6.3

查看容器
dockerfile lnmp构建

测试
dockerfile lnmp构建

dockerfile lnmp构建

相关新闻

联系我们

全国服务热线

400-033-9553

电子邮件:admin@example.com
工作时间:09:00-17:00 周一至周五

在线客服
关注微信
关注微信
分享本页
返回顶部