美文网首页
dockerfile LNMP

dockerfile LNMP

作者: 蓝山_d851 | 来源:发表于2020-06-02 12:05 被阅读0次

1.目录结构

[root@VM_0_10_centos lnmp]# tree 
.
|-- nginx
|   |-- Dockerfile
|   |-- nginx-1.8.1.tar.gz
|   `-- nginx.conf
`-- php
    |-- Dockerfile
    |-- php-5.6.31.tar.gz
    `-- php.ini

2.物料地址

nginx,用的是源码包来构建,版本为nginx-1.12.1.tar.gz,下载地址http://nginx.org/en/download.html/
php,也用的源码包来构建,版本为php-5.6.31.tar.gz,下载地址http://php.net/downloads.php

其中,在dockerfile目录下创建了两个目录(nginx、php),里面分别存放Dockerfile文件、源码包。nginx目录下还放了nginx.conf配置文件,php目录下也放置了php.ini配置文件。

 有些人会问为什么要把nginx.conf、php.ini配置文件放到这里,有两个原因,其一,把这两个默认的配置文件放在这里可以提前修改好所需要的参数,当容器启动后,就不需要在进入容器去修改了。当然,我这里只是练习环境,并未对这两个文件做任何更改。其二,在实际环境中,这两个文件是经常需要修改的,单独拿出来后在启动容器时你可以把这两个文件mount到容器中,便于管理。

3.nginx 创建

  1. dockerfile内容
[root@VM_0_10_centos nginx]# vim  Dockerfile 

FROM centos:7
MAINTAINER blog.51cto.com/ganbing

RUN yum -y install gcc gcc-c++ make openssl-devel pcre-devel
ADD  nginx-1.8.1.tar.gz   /tmp

RUN cd /tmp/nginx-1.8.1 &&  \
        ./configure  --prefix=/usr/local/nginx  &&  \
        make  -j 2 && \
        make install
RUN  rm -rf /tmp/nginx*  &&  yum  clean all
COPY  nginx.conf  /usr/local/nginx/conf/
WORKDIR  /usr/local/nginx/
EXPOSE  80
CMD ["./sbin/nginx","-g","daemon off;"]

来分析一下上面的内容,当你构建时,它会根据你编排好的内容一步一步的执行下去,如果当中的某一步执行不下去,会立刻停止构建。上面的大部分指令都很好理解,在上文中的dockerifle指令中有介绍,最后一个指令我要详细说明一下:CMD ["./sbin/nginx","-g","daemon off;"]

/sbin/nginx 这个没什么说的,就是正常启动nginx服务;
-g: 设置配置文件外的全局指令,也就是启动nginx时设置了daemon off参数,默认参数是打开的on,是否以守护进程的方式运行nginx,守护进程是指脱离终端并且在后台运行的进程。这里设置为off,也就是不让它在后台运行。为什么我们启动nginx容器时不让它在后台运行呢,docker 容器默认会把容器内部第一个进程,也就是pid=1的程序作为docker容器是否正在运行的依据,如果docker 容器pid挂了,那么docker容器便会直接退出。

  1. nginx.conf配置
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.php index.html index.htm;
        }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root          /usr/local/nginx/html;
            fastcgi_pass   lnmp_php:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
         }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

配置中主要添加了 location ~ .php这一段的内容,其中fastcgi_pass的 lnmp_php,这个是后面启动php容器时的名称。了解nginx原理的朋友应该能理解,当匹配到php的请求时,它会转发给lnmp_php这个容器php-fpm服务来处理。正常情况下,如果php服务不是跑在容器中,lnmp_php这个内容一般写php服务器的Ip地址。

  1. build构建
    nginx源码包、nginx.conf、Dockerfile都准备好了之后,现在我们可以来用docker build来构建这个镜像了:
    切换到nginx目录下:
[root@VM_0_10_centos nginx]# pwd
/opt/dockerfile/lnmp/nginx
[root@VM_0_10_centos nginx]# 
[root@VM_0_10_centos nginx]# 
[root@VM_0_10_centos nginx]# docker build   -t   nginx:1.8.1   .

4、php构建

4.1dockerfiel内容

[root@VM_0_10_centos lnmp]# cd php/
[root@VM_0_10_centos php]# ls
Dockerfile  php-5.6.31.tar.gz  php.ini
[root@VM_0_10_centos php]# vim Dockerfile 

FROM  centos:7
MAINTAINER blog.51cto.com/ganbing
RUN yum install -y gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel  libxml2* curl-devel libjpeg-devel  libjpeg.x86_64 libpng.x86_64 freetype.x86_64 libjpeg-devel.x86_64 libpng-devel.x86_64 freetype-devel.x86_64
ADD php-5.6.31.tar.gz  /tmp/


RUN cd  /tmp/php-5.6.31 && \
        ./configure --prefix=/usr/local/php \
        --with-config-file-path=/usr/local/php/etc \
        --with-mysql --with-mysqli \
        --with-openssl --with-zlib --with-curl --with-gd \
        --with-jpeg-dir --with-png-dir --with-iconv \
        --disable-fileinfo \
        --enable-fpm --enable-zip --enable-mbstring && \
        make  &&  make  install
RUN cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && \
        sed -i 's/127.0.0.1/0.0.0.0/g' /usr/local/php/etc/php-fpm.conf && \
        sed -i "21a daemonize=no" /usr/local/php/etc/php-fpm.conf
COPY php.ini  /usr/local/php/etc/
RUN rm -rf /tmp/php* && yum clean all
WORKDIR /usr/local/php/
EXPOSE 9000
CMD ["./sbin/php-fpm","-c","/usr/local/php/etc/php-fpm.conf"]

上面内容和nginx的Dockerfile风格差不多,也是一步一步的来。先安装依赖包、解压、配置并编译,然后修改下配置文件,启动php-fpm服务。是不是发现写Dockerfile挺简单的,就是把你平时部署php服务的步骤思路写到这个Dockerfile里面

4.2 php.ini内容

默认内容

4.3 build构建

[root@VM_0_10_centos php]# ls
Dockerfile  php-5.6.31.tar.gz  php.ini
[root@VM_0_10_centos php]# vim Dockerfile 
[root@VM_0_10_centos php]# 
[root@VM_0_10_centos php]# 
[root@VM_0_10_centos php]# 
[root@VM_0_10_centos php]# 
[root@VM_0_10_centos php]# 
[root@VM_0_10_centos php]# 
[root@VM_0_10_centos php]# 
[root@VM_0_10_centos php]# docker  build  -t  php:5.6.31   .

5 运行容器

  • 创建自定义网络lnmp
    先创建一个自定义网络,运行ningx、php这些容器的时候加入到lnmp网络中来:
[root@VM_0_10_centos php]# docker network  create  lnmp
[root@VM_0_10_centos php]# docker network  ls
NETWORK ID          NAME                DRIVER              SCOPE
23ce2eaab704        bridge              bridge              local
3bc9a32f3c47        host                host                local
f5633c733814        lnmp                bridge              local
faeeaa4fcf53        none                null                local
[root@VM_0_10_centos php]# 
  • 创建php容器
    创建容器:
[root@ganbing php]#  docker  run  -itd --name lnmp_php --network lnmp -v /app/wwwroot:/usr/local/nginx/html  php:5.6.31
  • 创建nginx容器
[root@VM_0_10_centos lnmp]#  docker run -itd --name lnmp_nginx --network lnmp -p 888:80 -v /app/wwwroot:/usr/local/nginx/html nginx:1.8.1
  • 测试访问
    其实到了这一步,我们可以创建一个index.html静态页面来访问一下:
[root@ganbing wwwroot]# echo "Dockerfile lnmp test" > /app/wwwroot/index.html

用浏览器访问这台宿主机的ip:


image.png

我们在弄个index.php文件来测试一下:

[root@ganbing wwwroot]# echo "<? phpinfo();" > /app/wwwroot/index.php
image.png

到了这一步,说明nginx、php的环境是弄好了,下面我们来把mysql数据库容器跑起来,来完成lnmp的平台,mysql数据库我这里就不用dockerfile来构建了,我直接用官方的镜像启动。

  • 创建mysql容器
    在创建容器前,我们创建一个数据卷mysql-volume,把它挂载到mysql容器中,实现数据持久化:
[root@ganbing /]# docker volume  create  mysql-volume
mysql-volume

启动mysql容器,如果你本地有mysql镜像,它会引用本的的镜像,如果没有它会去docker hub中拉取:

 docker run -itd  --name lnmp_mysql --network lnmp -p 3306:3306 -v mysql-volume:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql --character-set-server=utf8

  • 创建数据库wordpress:
docker exec lnmp_mysql sh  -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e"create database wordpress"' 
  • 查看wordpress库是否创建 :
[root@VM_0_10_centos lnmp]#  docker exec lnmp_mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e"show databases"' 
mysql: [Warning] Using a password on the command line interface can be insecure.
Database
information_schema
mysql
performance_schema
sys
wordpress
[root@VM_0_10_centos lnmp]# 
  • 查看数据卷是否同步了wordpress库:
[root@ganbing /]# ls /var/lib/docker/volumes/mysql-volume/_data/
auto.cnf    client-cert.pem  ibdata1      ibtmp1              private_key.pem  server-key.pem
ca-key.pem  client-key.pem   ib_logfile0  mysql               public_key.pem   sys
ca.pem      ib_buffer_pool   ib_logfile1  performance_schema  server-cert.pem  wordpress

5 下载wordpress博客系统测试lnmp

下载至/app/wwwroot目录下:

[root@VM_0_10_centos wwwroot]# wget  https://cn.wordpress.org/latest-zh_CN.tar.gz
[root@VM_0_10_centos wwwroot]# 
[root@VM_0_10_centos wwwroot]# ls
index.html  index.php  latest-zh_CN.tar.gz  wordpress
[root@VM_0_10_centos wwwroot]# tar -xf latest-zh_CN.tar.g

解压完了之后用浏览器访问:
http://容器宿主机IP/wordpress

image.png

详细参考文章https://blog.51cto.com/ganbing/2074640

相关文章

网友评论

      本文标题:dockerfile LNMP

      本文链接:https://www.haomeiwen.com/subject/eqtuzhtx.html