最近忙著寫自己的項(xiàng)目,也把一個(gè)站點(diǎn)的bbs論壇打算遷移到Docker中,測(cè)試沒發(fā)現(xiàn)啥大問題。在單臺(tái)上面的架構(gòu)如下;(往后我們也是要講到compose和swarm調(diào)度的慢慢來)
docker1.png (37.72 KB, 下載次數(shù): 141)
下載附件
2017-06-22 15:11 上傳
1、首先我們先安裝一下docker,好多人都發(fā)現(xiàn)國(guó)內(nèi)用yum安裝有各種問題;這里我們用國(guó)內(nèi)的https://www.daocloud.io.登錄后注冊(cè),然后點(diǎn)擊下載。里面有提示,我們點(diǎn)擊Linxu安裝然后復(fù)制代碼執(zhí)行到shell上即可。
2、安裝好之后,安裝dockhub加速器,點(diǎn)擊加速器,復(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
復(fù)制代碼##執(zhí)行腳本,主要是把倉(cāng)庫(kù)地址寫到daemon.json文件下。 - [root@test nginx]# cat /etc/docker/daemon.json
- {"registry-mirrors": ["http://681a96df.m.daocloud.io"],
- "live-restore": true
- }
復(fù)制代碼3、準(zhǔn)備工作都已經(jīng)完成了,接下來我們來構(gòu)建一下dockerfile在三個(gè)目錄下,看下目錄結(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.gz
復(fù)制代碼4、看一下nginx 的 Dockerfile: - [root@test nginx]# cat Dockerfile
- #lnmp centos 6.0
- from centos:centos6
- MAINTAINER xiaoluo <xiaoluo@test.com>
- ENV APP_DIR /web
- add nginx-1.11.10 /nginx-1.11.10
- RUN yum -y groupinstall "Development Tools" "Server Platform Deveopment"
- RUN yum -y install openssl-devel pcre-devel
- RUN useradd nginx -s /sbin/nologin
- RUN 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 install
- RUN mkdir /usr/local/nginx/conf/vhosts
- RUN mkdir /var/log/nginx
- ADD nginx.conf /usr/local/nginx/conf/nginx.conf
- ADD nginx_default.conf /usr/local/nginx/conf/vhosts/default.conf
- EXPOSE 80
- CMD ["/usr/local/nginx/sbin/nginx"]
復(fù)制代碼##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;
- }
- }
復(fù)制代碼###php:9000是通過后面的--link 容器之間互聯(lián)指定 5、開始構(gòu)建nginx鏡像: - [root@test nginx]# docker build -t lnmp/nginx:1.0 .
復(fù)制代碼##查看是否生成鏡像: - [root@test nginx]# docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- lnmp/nginx 1.0 5f5d4169189d 4 minutes ago 669 MB
復(fù)制代碼6、開始構(gòu)建php鏡像: - [root@test php-fpm]# cat Dockerfile
- from centos:centos6
- ADD Centos-6.repo /etc/yum.repos.d/CentOS-Base.repo
- ADD epel-6.repo /etc/yum.repos.d/epel.repo
- add php-5.5.38 /php-5.5.38
- RUN yum -y groupinstall "Desktop Platform Development"
- RUN yum -y install libmcrypt-devel bzip2-devel gcc openssl-devel php-mcrypt libmcrypt
- RUN 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 install
- RUN cp /php-5.5.38/php.ini-production /usr/local/php/etc/php.ini
- RUN mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
- RUN useradd -M -s /sbin/nologin php
- RUN 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.conf
- RUN sed -i 's\;daemonize = yes\daemonize = no\g' /usr/local/php/etc/php-fpm.conf
- EXPOSE 9000
- CMD ["/usr/local/php/sbin/php-fpm"]
復(fù)制代碼7、開始構(gòu)建php鏡像: - [root@test php-fpm]# docker build -t lnmp/php:1.0 .
復(fù)制代碼8、構(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.sh
- RUN chmod +x /opt/startup.sh
- EXPOSE 3306
- CMD ["/bin/bash","/opt/startup.sh"]
復(fù)制代碼##啟動(dòng)腳本: - [root@test mysql]# cat startup.sh
- #!/bin/bash
- if [ ! -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 10s
- fi
- /usr/bin/mysqld_safe
復(fù)制代碼**正常啟動(dòng)的時(shí)候,是沒有問題的;當(dāng)時(shí)當(dāng)我們用-v做持久化的時(shí)候,好像說用戶就失去對(duì)/var/lib/mysql的控制權(quán),所以啟動(dòng)的時(shí)候我們要判斷初始化才可以用-v來持久化相關(guān)目錄,這個(gè)地方之前搞了好久就是掛不起來,后面原來是這個(gè)地方。
9、開始構(gòu)建mysql鏡像: - [root@test mysql]# docker build -t lnmp/mysql:1.0 .
復(fù)制代碼10、下面我們開始啟動(dòng)相關(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 ps
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 3527cddb4c50 lnmp/mysql:1.0 "/bin/bash /opt/st..." 4 seconds ago Up 3 seconds 0.0.0.0:3306->3306/tcp mysql
- fab93953c438 lnmp/nginx:1.0 "/usr/local/nginx/..." About a minute ago Up About a minute 0.0.0.0:80->80/tcp web
- d5854337c10b lnmp/php:1.0 "/usr/local/php/sb..." 3 minutes ago Up 2 minutes 9000/tcp
復(fù)制代碼 php
##可以看到我們已經(jīng)都啟動(dòng)了所有的容器了。
11、接下來我們登錄一下mysql.創(chuàng)建一下wordpress使用的數(shù)據(jù)庫(kù): - [root@test mysql]# mysql -uroot -p123456 -h 192.168.63.200
- MySQL [(none)]> CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8;
- Query OK, 1 row affected (0.00 sec)
復(fù)制代碼12、然后我們把wordpress代碼放到我們掛載的本地/web目錄下面: - [root@test web]# wget https://cn.wordpress.org/wordpress-4.7.2-zh_CN.tar.gz
復(fù)制代碼
#然后解壓出來。我們直接訪問一下當(dāng)前主機(jī)的IP地址:
docker2.png (100.23 KB, 下載次數(shù): 151)
下載附件
2017-06-22 15:19 上傳
直接往下走注冊(cè)即可:
docker3.png (74.7 KB, 下載次數(shù): 132)
下載附件
2017-06-22 15:20 上傳
docker4.png (54.91 KB, 下載次數(shù): 143)
下載附件
2017-06-22 15:21 上傳
##到此在Docker 分離下安裝wordpress已經(jīng)完成,但是我們要思考一個(gè)問題,就是有沒有更好的方法統(tǒng)一編排一下這些容器呢,給容器更好的分組管理:可以留意一下docker-compose,在1.13之后更是結(jié)合棧來實(shí)現(xiàn)跨主機(jī)編排。
##還有一個(gè)就是如何給這些容器做成集群管理,保證節(jié)點(diǎn)的高可用。和資源監(jiān)控調(diào)度呢?梢钥匆幌1.12之后的docker swarm,構(gòu)建集群非常簡(jiǎn)單。
|