免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1153 | 回复: 0

[系统管理] Dockerfile分离构建LNMP环境部署wordpress [复制链接]

论坛徽章:
1
15-16赛季CBA联赛之同曦
日期:2017-01-17 18:19:30
发表于 2017-03-24 15:52 |显示全部楼层
本帖最后由 调皮仔 于 2017-03-24 15:59 编辑

  最近忙着写自己的项目,也把一个站点的bbs论坛打算迁移到Docker中,测试没发现啥大问题。在单台上面的架构如下;(往后我们也是要讲到compose和swarm调度的慢慢来)

1.png

1、首先我们先安装一下docker,好多人都发现国内用yum安装有各种问题;这里我们用国内的https://www.daocloud.io.登录后注册,然后点击下载。里面有提示,我们点击Linxu安装然后复制代码执行到shell上即可。

[root@test nginx]# curl -sSL https://get.daocloud.io/docker | sh


2、安装好之后,安装dockhub加速器,点击加速器,复制代码粘贴到shell.

[root@test nginx]# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://681a96df.m.daocloud.io{"registry-mirrors": ["http://681a96df.m.daocloud.io"],    "live-restore": true}Success.You need to restart docker to take effect: sudo systemctl restart docker

##执行脚本,主要是把仓库地址写到daemon.json文件下。

[root@test nginx]# cat /etc/docker/daemon.json{"registry-mirrors": ["http://681a96df.m.daocloud.io"],    "live-restore": true}

3、准备工作都已经完成了,接下来我们来构建一下dockerfile在三个目录下,看下目录结构:

[root@test test]# tree -L 2 --charset ASCII|-- mysql|   |-- Dockerfile|   |-- epel-6.repo|   |-- my.cnf|   `-- startup.sh|-- nginx|   |-- Dockerfile|   |-- nginx-1.11.10|   |-- nginx-1.11.10.tar.gz|   |-- nginx.conf|   `-- nginx_default.conf`-- php-fpm    |-- Centos-6.repo    |-- Dockerfile    |-- epel-6.repo    |-- php-5.5.38    `-- php-5.5.38.tar.gz

4、看一下nginx 的 Dockerfile:

[root@test nginx]# cat Dockerfile #lnmp centos 6.0from centos:centos6MAINTAINER xiaoluo <[url=mailtoiaoluo@test.com]xiaoluo@test.com[/url]>ENV APP_DIR /webadd nginx-1.11.10 /nginx-1.11.10RUN yum -y groupinstall "Development Tools" "Server Platform Deveopment"RUN yum -y install openssl-devel pcre-develRUN useradd nginx -s /sbin/nologinRUN cd /nginx-1.11.10 && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module  --with-pcre && make && make installRUN mkdir /usr/local/nginx/conf/vhostsRUN mkdir /var/log/nginxADD nginx.conf /usr/local/nginx/conf/nginx.confADD nginx_default.conf /usr/local/nginx/conf/vhosts/default.confEXPOSE 80CMD ["/usr/local/nginx/sbin/nginx"]

##nginx 相关php配置:

[root@test nginx]# cat nginx_default.conf server {    listen       80 default_server;    server_name  localhost;    #charset koi8-r;    location / {        root   /web;        index  index.php index.html index.htm;    }    # redirect server error pages to the static page /50x.html    #    error_page   500 502 503 504  /50x.html;    location = /50x.html {        root   APP_DIR;    }    # Disable nginx log write favicon.ico    location = /favicon.ico {    log_not_found off;    access_log off;        }    # pass the PHP scripts to FastCGI server listening on port 9000    #    location ~ \.php$ {        root           /web;        fastcgi_pass   php:9000;        #fastcgi_pass  unix:/tmp/php-fpm.sock;        fastcgi_index  index.php;        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;        include        fastcgi_params;    }}

###php:9000是通过后面的--link 容器之间互联指定


5、开始构建nginx镜像:

[root@test nginx]# docker build -t lnmp/nginx:1.0 .

##查看是否生成镜像:

[root@test nginx]# docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZElnmp/nginx          1.0                 5f5d4169189d        4 minutes ago       669 MB

6、开始构建php镜像:

[root@test php-fpm]# cat Dockerfile from centos:centos6ADD Centos-6.repo /etc/yum.repos.d/CentOS-Base.repoADD epel-6.repo /etc/yum.repos.d/epel.repoadd php-5.5.38 /php-5.5.38RUN yum -y groupinstall  "Desktop Platform Development" RUN yum -y install libmcrypt-devel bzip2-devel gcc openssl-devel php-mcrypt libmcryptRUN cd /php-5.5.38 && ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-mcrypt  --with-bz2 --enable-fpm --with-gd && make && make installRUN cp /php-5.5.38/php.ini-production  /usr/local/php/etc/php.iniRUN mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.confRUN useradd -M -s /sbin/nologin phpRUN 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.confRUN sed -i 's\;daemonize = yes\daemonize = no\g' /usr/local/php/etc/php-fpm.confEXPOSE 9000CMD ["/usr/local/php/sbin/php-fpm"]

7、开始构建php镜像:

[root@test php-fpm]# docker build -t lnmp/php:1.0 .

8、构建mysql镜像的Dockerfile:

[root@test mysql]# cat Dockerfile FROM centos:centos6  MAINTAINER xiaoluo "18878774@163.com"  RUN yum install -y mysql-server mysql  ADD ./startup.sh /opt/startup.shRUN chmod +x /opt/startup.shEXPOSE 3306CMD ["/bin/bash","/opt/startup.sh"]

##启动脚本:

[root@test mysql]# cat startup.sh #!/bin/bashif [ ! -f /var/lib/mysql/ibdata1 ]; then        mysql_install_db        /usr/bin/mysqld_safe &        sleep 10s        mysql -e "grant all privileges on *.* to 'root'@'%' identified by '123456'; FLUSH PRIVILEGES;"        killall mysqld        sleep 10sfi/usr/bin/mysqld_safe

**正常启动的时候,是没有问题的;当时当我们用-v做持久化的时候,好像说用户就失去对/var/lib/mysql的控制权,所以启动的时候我们要判断初始化才可以用-v来持久化相关目录,这个地方之前搞了好久就是挂不起来,后面原来是这个地方。


9、开始构建mysql镜像:

[root@test mysql]# docker build -t lnmp/mysql:1.0 .

10、下面我们开始启动相关容器:

[root@test web]# docker run -dit --name php -v /web:/web lnmp/php:1.0[root@test web]# docker run -dit --name web -p 80:80 -v /web:/web --link php:php lnmp/nginx:1.0[root@test web]#docker run -dit --name mysql -p 3306:3306 -v /opt/data:/var/lib/mysql lnmp/mysql:1.0#####[root@test mysql]# docker psCONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                    NAMES3527cddb4c50        lnmp/mysql:1.0      "/bin/bash /opt/st..."   4 seconds ago        Up 3 seconds        0.0.0.0:3306->3306/tcp   mysqlfab93953c438        lnmp/nginx:1.0      "/usr/local/nginx/..."   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp       webd5854337c10b        lnmp/php:1.0        "/usr/local/php/sb..."   3 minutes ago        Up 2 minutes        9000/tcp

              php


##可以看到我们已经都启动了所有的容器了。


11、接下来我们登录一下mysql.创建一下wordpress使用的数据库:

[root@test mysql]# mysql -uroot -p123456 -h 192.168.63.200MySQL [(none)]> CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8; Query OK, 1 row affected (0.00 sec)

12、然后我们把wordpress代码放到我们挂载的本地/web目录下面:

[root@test web]# wget https://cn.wordpress.org/wordpress-4.7.2-zh_CN.tar.gz

#然后解压出来。我们直接访问一下当前主机的IP地址:


2.png


直接往下走注册即可:

3.png



4.png


##到此在Docker 分离下安装wordpress已经完成,但是我们要思考一个问题,就是有没有更好的方法统一编排一下这些容器呢,给容器更好的分组管理:可以留意一下docker-compose,在1.13之后更是结合栈来实现跨主机编排。


##还有一个就是如何给这些容器做成集群管理,保证节点的高可用。和资源监控调度呢。可以看一下1.12之后的docker swarm,构建集群非常简单。


查看更多参考内容:http://www.roncoo.com/article/index




您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP