最近忙著寫自己的項目,也把一個站點的bbs論壇打算遷移到Docker中,測試沒發(fā)現(xiàn)啥大問題。在單臺上面的架構(gòu)如下;(往后我們也是要講到compose和swarm調(diào)度的慢慢來)
1、首先我們先安裝一下docker,好多人都發(fā)現(xiàn)國內(nèi)用yum安裝有各種問題;這里我們用國內(nèi)的https://www.daocloud.io.登錄后注冊,然后點擊下載。里面有提示,我們點擊Linxu安裝然后復(fù)制代碼執(zhí)行到shell上即可。
[root@test nginx]# curl -sSL https://get.daocloud.io/docker | sh
2、安裝好之后,安裝dockhub加速器,點擊加速器,復(fù)制代碼粘貼到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##執(zhí)行腳本,主要是把倉庫地址寫到daemon.json文件下。
[root@test nginx]# cat /etc/docker/daemon.json{"registry-mirrors": ["http://681a96df.m.daocloud.io"], "live-restore": true}3、準(zhǔn)備工作都已經(jīng)完成了,接下來我們來構(gòu)建一下dockerfile在三個目錄下,看下目錄結(jié)構(gòu):
[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.gz5、看一下nginx 的 Dockerfile:
[root@test nginx]# cat Dockerfile #lnmp centos 6.0from centos:centos6MAINTAINER xiaoluo <xiaoluo@test.com>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 相關(guān)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 容器之間互聯(lián)指定
6、開始構(gòu)建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 MB7、開始構(gòu)建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"]8、開始構(gòu)建php鏡像:
[root@test php-fpm]# docker build -t lnmp/php:1.0 .9、構(gòu)建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**正常啟動的時候,是沒有問題的;當(dāng)時當(dāng)我們用-v做持久化的時候,好像說用戶就失去對/var/lib/mysql的控制權(quán),所以啟動的時候我們要判斷初始化才可以用-v來持久化相關(guān)目錄,這個地方之前搞了好久就是掛不起來,后面原來是這個地方。
10、開始構(gòu)建mysql鏡像:
[root@test mysql]# docker build -t lnmp/mysql:1.0 .11、下面我們開始啟動相關(guān)容器:
[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
##可以看到我們已經(jīng)都啟動了所有的容器了。
12、接下來我們登錄一下mysql.創(chuàng)建一下Wordpress使用的數(shù)據(jù)庫:
[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)13、然后我們把wordpress代碼放到我們掛載的本地/web目錄下面:
[root@test web]# wget https://cn.wordpress.org/wordpress-4.7.2-zh_CN.tar.gz#然后解壓出來。我們直接訪問一下當(dāng)前主機(jī)的IP地址:
##到此在Docker 分離下安裝wordpress已經(jīng)完成,但是我們要思考一個問題,就是有沒有更好的方法統(tǒng)一編排一下這些容器呢,給容器更好的分組管理:可以留意一下docker-compose,在1.13之后更是結(jié)合棧來實現(xiàn)跨主機(jī)編排。
##還有一個就是如何給這些容器做成集群管理,保證節(jié)點的高可用。和資源監(jiān)控調(diào)度呢。可以看一下1.12之后的docker swarm,構(gòu)建集群非常簡單。
原文地址:http://www.roncoo.com/article/detail/127431
新聞熱點
疑難解答