成功最有效的方法就是向有经验的人学习!

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

[root@docker-yankerp docker]# yum install docker -y  
[root@docker-yankerp docker]# systemctl start docker

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

[root@docker-yankerp docker]# tree -L 2 --charset ASCII  
.  
|-- mysql  
|   |-- Dockerfile  
|   `-- my.cnf  
|-- nginx  
|   |-- Dockerfile  
|   `-- wordpress  
`-- php  
    |-- Dockerfile  
    `-- wordpress
自动草稿

自动草稿

wordpress就是我们的php源码

2)构建Nginx-dockerfile如下:

FROM docker.io/centos:latest
MAINTAINER from <ywyankerp@163.com>
RUN yum install gcc gcc-c++ openssl-devel automake autoconf libtool zlib-devel make pcre-devel wget net-tools -y
RUN groupadd -g 900 nginx && useradd nginx -g nginx -s /sbin/nologin
RUN wget http://nginx.org/download/nginx-1.12.2.tar.gz && tar zxf nginx-1.12.2.tar.gz
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 
RUN cd /nginx-1.12.2/ && make && make install
RUN ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
RUN mkdir /www && chown nginx:nginx /www
ADD wordpress /www/wordpress
RUN sed -i '1afastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;' /usr/local/nginx/conf/fastcgi_params
RUN sed -i 's/index  index.html index.htm;/index  index.php index.html index.htm;/g' /usr/local/nginx/conf/nginx.conf
RUN sed -i 's*root   html;*root   /www;*g' /usr/local/nginx/conf/nginx.conf
RUN sed -i 's*#    root           html;*    root           /www;*g' /usr/local/nginx/conf/nginx.conf
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
RUN sed -i 's/#    fastcgi_index  index.php;/    fastcgi_index  index.php;/g' /usr/local/nginx/conf/nginx.conf
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
RUN sed -i 's/#    include        fastcgi_params;/    include        fastcgi_params;/g' /usr/local/nginx/conf/nginx.conf
RUN sed -i '71d' /usr/local/nginx/conf/nginx.conf && sed -i 'N;70a}' /usr/local/nginx/conf/nginx.conf
RUN sed -i '65d' /usr/local/nginx/conf/nginx.conf && sed -i 'N;64alocation ~ \\.php$ {' /usr/local/nginx/conf/nginx.conf
EXPOSE 80
CMD ["nginx"]

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

3)运行Nginx-dockerfile

自动草稿

自动草稿


运行结束如下:

自动草稿

自动草稿


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

自动草稿

自动草稿


二、构建PHP-Dockerfile

php的dockerfile如下

FROM docker.io/centos:latest
MAINTAINER from <ywyankerp@163.com>
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
RUN wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz && tar zxf libmcrypt-2.5.7.tar.gz
RUN cd libmcrypt-2.5.7/ && ./configure --prefix=/usr/local/libmcrypt && make && make install ;cd
RUN wget http://cn2.php.net/distributions/php-5.6.27.tar.gz && tar zxf php-5.6.27.tar.gz
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
RUN groupadd -g 1001 nginx  && useradd -u 900 nginx -g nginx -s /sbin/nologin
RUN mkdir /www && chown -R nginx:nginx /www
ADD wordpress /www/wordpress
RUN cd php-5.6.27 &&  cp php.ini-production /etc/php.ini 
RUN cd  /php-5.6.27 &&  cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
RUN chmod +x /etc/init.d/php-fpm && chkconfig --add php-fpm && chkconfig php-fpm on
RUN cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf
RUN sed -i 's*;pid = run/php-fpm.pid*pid = run/php-fpm.pid*g' /usr/local/php5.6/etc/php-fpm.conf
RUN sed -i 's/user = nobody/user = nginx/g' /usr/local/php5.6/etc/php-fpm.conf
RUN sed -i 's/group = nobody/group = nginx/g' /usr/local/php5.6/etc/php-fpm.conf
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
RUN sed -i 's/pm.max_children = 5/pm.max_children = 50/g' /usr/local/php5.6/etc/php-fpm.conf
RUN sed -i 's/pm.start_servers = 2/pm.start_servers = 5/g' /usr/local/php5.6/etc/php-fpm.conf
RUN sed -i 's/pm.min_spare_servers = 1/pm.min_spare_servers = 5/g' /usr/local/php5.6/etc/php-fpm.conf
RUN sed -i 's/pm.max_spare_servers = 3/pm.max_spare_servers = 35/g' /usr/local/php5.6/etc/php-fpm.conf
EXPOSE 9000

1)运行php-dockerfile

自动草稿

自动草稿


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

2)运行结束如下:

自动草稿

自动草稿


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

自动草稿

自动草稿


三、构建MySQL-Dockerfile

MySQL-Dockerfile如下:

FROM docker.io/centos:latest
MAINTAINER from <ywyankerp@163.com>
RUN yum install -y gcc gcc-c++ make wget libaio
RUN wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
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
WORKDIR /usr/local/mysql/
RUN echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile && source /etc/profile
WORKDIR /usr/local/mysql/
RUN mkdir /usr/local/mysql/data && mkdir /usr/local/mysql/log
RUN groupadd mysql && useradd -r -g mysql -s /bin/false mysql
ADD my.cnf /etc/my.cnf
WORKDIR /usr/local/mysql
RUN chmod 750 /usr/local/mysql/data/
RUN chown -R mysql /usr/local/mysql
RUN chgrp -R mysql /usr/local/mysql
WORKDIR /usr/local/mysql
RUN ln -s /usr/local/mysql/bin/mysql /usr/local/bin
RUN bin/mysqld --initialize --user=mysql
RUN cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
EXPOSE 3306

My.cnf文件内容如下:

[client]  
socket=/usr/local/mysql/mysql.sock  
[mysqld]  
basedir=/usr/local/mysql  
datadir=/usr/local/mysql/data  
pid-file=/usr/local/mysql/data/mysqld.pid  
socket=/usr/local/mysql/mysql.sock  
log_error=/usr/local/mysql/log/mysql.err

1)运行Mysql-dockerfile

[root@docker-yankerp ~]# cd /docker/mysql/  
[root@docker-yankerp mysql]# docker build -t="centos:mysql" .

2)运行结束如下:

自动草稿

自动草稿

查看所以的镜像

自动草稿

自动草稿

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

[root@docker-yankerp ~]# docker images   
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE  
centos              mysql               6a4bd7770f0f        About an hour ago   11.24 GB  
centos              php                 84b26690a2de        3 hours ago         1.067 GB  
centos              nginx               2961bd52b14e        3 hours ago         503.6 MB  
docker.io/centos    latest              328edcd84f1b        5 months ago        192.5 MB  
[root@docker-yankerp ~]# docker run -dit -p 80:80 centos:nginx /bin/bash  
73d1bbeb0d5c213e997523e36d467565ed3ad189fa5d0b1c47b32853038fbb14  
[root@docker-yankerp ~]# docker run -dit -p 9000:9000 centos:php /bin/bash         
01eaea6c5453d739a00a4605ba62d711741cce33f2f41f4c86e73140a9c18a68  
[root@docker-yankerp ~]# docker run -dit -p 3306:3306  centos:mysql /bin/bash          
2b0fa469425d164ea28a5dfde8cd4b78a043bab640c6334f6fdcf24b32d4bc41

查看容器运行状态

自动草稿

自动草稿


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

mysql> create database wordpress;  
Query OK, 1 row affected (0.00 sec)  
  
mysql> grant all on wordpress.* to yankerp@'%' identified by '123456';  
Query OK, 0 rows affected, 1 warning (0.00 sec)  
  
mysql> flush privileges;  
Query OK, 0 rows affected (0.01 sec)

进入后台 docker exec -it  images /bin/bash

访问测试:

自动草稿

自动草稿

自动草稿

自动草稿

自动草稿

自动草稿

自动草稿

自动草稿

赞(1) 打赏
未经允许不得转载:陈桂林博客 » Docker实战-分离部署LNMP环境搭建WordPress个人站点
分享到

大佬们的评论 抢沙发

全新“一站式”建站,高质量、高售后的一条龙服务

微信 抖音 支付宝 百度 头条 快手全平台打通信息流

橙子建站.极速智能建站8折购买虚拟主机

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册