Docker实战-分离部署LNMP环境搭建Wordpress个人网站详细配置

简介:

dockerfile是一种被docker程序解释的脚本,dockerfile是由一条条的指令组成的,每一条指令对应linux下面的一条命令docker程序将这些dockerfile指令翻译成真正的linux命令。dockerfile有自己的书写格式和支持的命令。docker程序解决这些命令之间的依赖关系,就类似于makefile docker程序将读取的dockerfile,根据指令生成定制的images,它明确的表明images是怎么产生的。有了dockerfile,在我们需要定制自己额外的需求时,只需要在dockerfile上添加或者修改指令就好了,重新生成image就可了,完全省去了敲命令的麻烦。

dockerfile由一条条的语句组成,并且支持#开头的为注释行
dockerfile的指令是忽略大小写的,建议使用大写,每一行只支持一条指令,每条指令都可以携带多个参数
dockerfile的指令根据作用可以分为两种,构建指令和设置指令,构建指令用于构建image,指定的操作不会在运行image容器上执行。设置指令用于设置images的属性,指定的操作将在运行image的容器中执行
一般的dockerfile分为四个部分:基础镜像信息,维护者信息,镜像操作指令和容器起哦的那个时执行的指令

本次测试环境OS:Centos7.2x86_64  本次采用基础image为Centos7

一、安装docker

[css]  view plain  copy
  1. [root@kang docker]# yum install docker -y  
  2. [root@kang docker]# systemctl start docker  

(1)安装完docker后我们创建三个目录存放各自服务的dockerfile如下:

[css]  view plain  copy
  1. [root@kang docker]# tree -L 2 --charset ASCII  
  2. .  
  3. |-- mysql  
  4. |   |-- Dockerfile  
  5. |   `-- my.cnf  
  6. |-- nginx  
  7. |   |-- Dockerfile  
  8. |   `-- wordpress  
  9. `-- php  
  10.     |-- Dockerfile  
  11.     `-- wordpress  

wordpress就是我们的php源码

下载官网:点击打开链接https://wordpress.org/latest.zip

下载完之后解压:

unzip  wordpress-4.9.4.zip


(2)构建Nginx-dockerfile如下:

[css]  view plain  copy
  1. FROM docker.io/centos:latest  
  2. MAINTAINER from <15011147011@163.com>  
  3. RUN yum install gcc gcc-c++ openssl-devel automake autoconf libtool zlib-devel make pcre-devel wget net-tools -y  
  4. RUN groupadd -g 900 nginx && useradd nginx -g nginx -s /sbin/nologin  
  5. RUN wget http://nginx.org/download/nginx-1.12.2.tar.gz && tar zxf nginx-1.12.2.tar.gz  
  6. RUN cd  /nginx-1.12.2/ && ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_ssl_module --with-http_gzip_static_module --user=nginx --group=nginx   
  7. RUN cd /nginx-1.12.2/ && make && make install  
  8. RUN ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/  
  9. RUN mkdir /www && chown nginx:nginx /www  
  10. ADD wordpress /www/wordpress  
  11. RUN sed -i '1afastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;' /usr/local/nginx/conf/fastcgi_params  
  12. RUN sed -i 's/index  index.html index.htm;/index  index.php index.html index.htm;/g' /usr/local/nginx/conf/nginx.conf  
  13. RUN sed -i 's*root   html;*root   /www;*g' /usr/local/nginx/conf/nginx.conf  
  14. RUN sed -i 's*#    root           html;*    root           /www;*g' /usr/local/nginx/conf/nginx.conf  
  15. RUN sed -i 's/#    fastcgi_pass   127.0.0.1:9000;/    fastcgi_pass   172.17.0.3:9000;/g' /usr/local/nginx/conf/nginx.conf  
  16. RUN sed -i 's/#    fastcgi_index  index.php;/    fastcgi_index  index.php;/g' /usr/local/nginx/conf/nginx.conf  
  17. RUN sed -i 's*#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;*    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;*g' /usr/local/nginx/conf/nginx.conf  
  18. RUN sed -i 's/#    include        fastcgi_params;/    include        fastcgi_params;/g' /usr/local/nginx/conf/nginx.conf  
  19. RUN sed -i '71d' /usr/local/nginx/conf/nginx.conf && sed -i 'N;70a}' /usr/local/nginx/conf/nginx.conf  
  20. RUN sed -i '65d' /usr/local/nginx/conf/nginx.conf && sed -i 'N;64alocation ~ \\.php$ {' /usr/local/nginx/conf/nginx.conf  
  21. EXPOSE 80  
  22. CMD ["nginx"]  


在以上dockerfile中我没有把nginx.conf文件用拷贝的方式去弄,而是直接使用了sed命令去修改!

(3)运行Nginx-dockerfile


运行结束如下:


运行结束后查看images是否生成


二、构建PHP-Dockerfile

php的dockerfile如下

[php]  view plain  copy
  1. FROM docker.io/centos:latest  
  2. MAINTAINER from <[email protected]>  
  3. RUN yum install gcc gcc-c++ libxml2-devel libcurl-devel openssl-devel bzip2-devel openssl-devel automake autoconf libtool zlib-devel make pcre-devel wget net-tools -y  
  4. RUN wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz && tar zxf libmcrypt-2.5.7.tar.gz  
  5. RUN cd libmcrypt-2.5.7/ && ./configure --prefix=/usr/local/libmcrypt && make && make install ;cd  
  6. RUN wget http://cn2.php.net/distributions/php-5.6.27.tar.gz && tar zxf php-5.6.27.tar.gz  
  7. RUN cd php-5.6.27/ && ./configure --prefix=/usr/local/php5.6 --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=/usr/local/libmcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts && make && make install  
  8. RUN groupadd -g 1001 nginx  && useradd -u 900 nginx -g nginx -s /sbin/nologin  
  9. RUN mkdir /www && chown -R nginx:nginx /www  
  10. ADD wordpress /www/wordpress  
  11. RUN cd php-5.6.27 &&  cp php.ini-production /etc/php.ini   
  12. RUN cd  /php-5.6.27 &&  cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm  
  13. RUN chmod +x /etc/init.d/php-fpm && chkconfig --add php-fpm && chkconfig php-fpm on  
  14. RUN cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf  
  15. RUN sed -i 's*;pid = run/php-fpm.pid*pid = run/php-fpm.pid*g' /usr/local/php5.6/etc/php-fpm.conf  
  16. RUN sed -i 's/user = nobody/user = nginx/g' /usr/local/php5.6/etc/php-fpm.conf  
  17. RUN sed -i 's/group = nobody/group = nginx/g' /usr/local/php5.6/etc/php-fpm.conf  
  18. RUN sed -i 's/listen = 127.0.0.1:9000/listen = 172.17.0.3:9000/g' /usr/local/php5.6/etc/php-fpm.conf  
  19. RUN sed -i 's/pm.max_children = 5/pm.max_children = 50/g' /usr/local/php5.6/etc/php-fpm.conf  
  20. RUN sed -i 's/pm.start_servers = 2/pm.start_servers = 5/g' /usr/local/php5.6/etc/php-fpm.conf  
  21. RUN sed -i 's/pm.min_spare_servers = 1/pm.min_spare_servers = 5/g' /usr/local/php5.6/etc/php-fpm.conf  
  22. RUN sed -i 's/pm.max_spare_servers = 3/pm.max_spare_servers = 35/g' /usr/local/php5.6/etc/php-fpm.conf  
  23. EXPOSE 9000  


(1)运行php-dockerfile


注意以上图的末尾的. 表示dockerfile在当前路径。

(2)运行结束如下:


(3)查看php-image是否构建成功


三、构建MySQL-Dockerfile

MySQL-Dockerfile如下:

[sql]  view plain  copy
  1. FROM docker.io/centos:latest  
  2. MAINTAINER from <[email protected]>  
  3. RUN yum install -y gcc gcc-c++ make wget libaio  
  4. RUN wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz  
  5. RUN tar zxf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz && mv mysql-5.7.18-linux-glibc2.5-x86_64 /usr/local/mysql  
  6. WORKDIR /usr/local/mysql/  
  7. RUN echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile && source /etc/profile  
  8. WORKDIR /usr/local/mysql/  
  9. RUN mkdir /usr/local/mysql/data && mkdir /usr/local/mysql/log  
  10. RUN groupadd mysql && useradd -r -g mysql -s /bin/false mysql  
  11. ADD my.cnf /etc/my.cnf  
  12. WORKDIR /usr/local/mysql  
  13. RUN chmod 750 /usr/local/mysql/data/  
  14. RUN chown -R mysql /usr/local/mysql  
  15. RUN chgrp -R mysql /usr/local/mysql  
  16. WORKDIR /usr/local/mysql  
  17. RUN ln -s /usr/local/mysql/bin/mysql /usr/local/bin  
  18. RUN bin/mysqld --initialize --user=mysql  
  19. RUN cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld  
  20. EXPOSE 3306  


My.cnf文件内容如下:

[sql]  view plain  copy
  1. [client]  
  2. socket=/usr/local/mysql/mysql.sock  
  3. [mysqld]  
  4. basedir=/usr/local/mysql  
  5. datadir=/usr/local/mysql/data  
  6. pid-file=/usr/local/mysql/data/mysqld.pid  
  7. socket=/usr/local/mysql/mysql.sock  
  8. log_error=/usr/local/mysql/log/mysql.err  

(1)运行Mysql-dockerfile


(2.)运行结束如下:



(3.)查看mysql-image是否构建成功


四、启动容器映射主机端口如下: cop


[root@kang ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              mysql               0839e464795c        5 minutes ago       9.217 GB
centos              php                 4aa696e06aaf        37 minutes ago      1.097 GB
centos              nginx               31a034f805a1        About an hour ago   533.3 MB
centos              http                9c2f3c59a078        3 hours ago         322.7 MB
docker.io/centos    latest              50dae1ee8677        19 months ago       196.7 MB
docker.io/centos    centos6             cf2c3ece5e41        20 months ago       194.6 MB
[root@kang ~]# docker run -dit -p 80:80 centos:nginx /bin/bash  
3f9c8b141013f2098f024a7b4555ff171f81956b04921f3fdfe9df600559a60a
[root@kang ~]# docker run -dit -p 9000:9000 centos:php /bin/bash  
819e148e844ca163a1783b0d69083c658fd7f97c2109a341b2a34d64b1ba043c
[root@kang ~]# docker run -dit -p 3306:3306  centos:mysql /bin/bash   
a5821213d789155f606b70b54f48f32c7ed4ef4c58d76c33d1ca3429f4993666
[root@kang ~]# 

查看容器运行状态


后台进入容器开启相关的服务,MySQL添加授权用户。访问测试

[sql]  view plain  copy
  1. mysql> create database wordpress;  
  2. Query OK, 1 row affected (0.00 sec)  
  3.   
  4. mysql> grant all on wordpress.* to yankerp@'%' identified by '123456';  
  5. Query OK, 0 rows affected, 1 warning (0.00 sec)  
  6.   
  7. mysql> flush privileges;  
  8. Query OK, 0 rows affected (0.01 sec)  
进入后台 docker exec -it  images镜像名 /bin/bash

访问测试:

到这里有关于Docker实战-分离部署LNMP环境搭建Wordpress个人网站详细配置!!!!


希望对你有所帮助!!!@再见!!!!!再见再见再见

猜你喜欢

转载自blog.csdn.net/kangshuo2471781030/article/details/79365313