最近忙着寫本身的項目,也把一個站點的bbs論壇打算遷移到Docker中,測試沒發現啥大問題。在單臺上面的架構以下;(日後咱們也是要講到compose和swarm調度的慢慢來)php
一、首先咱們先安裝一下docker,好多人都發現國內用yum安裝有各類問題;這裏咱們用國內的https://www.daocloud.io.登陸後註冊,而後點擊下載。裏面有提示,咱們點擊Linxu安裝而後複製代碼執行到shell上便可。html
[root@test nginx]# curl -sSL https://get.daocloud.io/docker | sh
二、安裝好以後,安裝dockhub加速器,點擊加速器,複製代碼粘貼到shell.mysql
[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文件下。nginx
[root@test nginx]# cat /etc/docker/daemon.json {"registry-mirrors": ["http://681a96df.m.daocloud.io"], "live-restore": true }
三、準備工做都已經完成了,接下來咱們來構建一下dockerfile在三個目錄下,看下目錄結構:web
[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
五、看一下nginx 的 Dockerfile:sql
[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"]
##nginx 相關php配置:docker
[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 容器之間互聯指定shell
六、開始構建nginx鏡像:數據庫
[root@test nginx]# docker build -t lnmp/nginx:1.0 .json
##查看是否生成鏡像:
[root@test nginx]# docker p_w_picpaths REPOSITORY TAG IMAGE ID CREATED SIZE lnmp/nginx 1.0 5f5d4169189d 4 minutes ago 669 MB
七、開始構建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"]
八、開始構建php鏡像:
[root@test php-fpm]# docker build -t lnmp/php:1.0 .
九、構建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"]
##啓動腳本:
[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
**正常啓動的時候,是沒有問題的;當時當咱們用-v作持久化的時候,好像說用戶就失去對/var/lib/mysql的控制權,因此啓動的時候咱們要判斷初始化才能夠用-v來持久化相關目錄,這個地方以前搞了很久就是掛不起來,後面原來是這個地方。
十、開始構建mysql鏡像:
[root@test mysql]# docker build -t lnmp/mysql:1.0 .
十一、下面咱們開始啓動相關容器:
[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 php
##能夠看到咱們已經都啓動了全部的容器了。
十二、接下來咱們登陸一下mysql.建立一下wordpress使用的數據庫:
[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)
1三、而後咱們把wordpress代碼放到咱們掛載的本地/web目錄下面:
[root@test web]# wget https://cn.wordpress.org/wordpress-4.7.2-zh_CN.tar.gz
#而後解壓出來。咱們直接訪問一下當前主機的IP地址:
直接往下走註冊便可:
##到此在Docker 分離下安裝wordpress已經完成,可是咱們要思考一個問題,就是有沒有更好的方法統一編排一下這些容器呢,給容器更好的分組管理:能夠留意一下docker-compose,在1.13以後更是結合棧來實現跨主機編排。(docker-compose:文章http://xiaoluoge.blog.51cto.com/9141967/1902816)
##還有一個就是如何給這些容器作成集羣管理,保證節點的高可用。和資源監控調度呢。能夠看一下1.12以後的docker swarm,構建集羣很是簡單。
Docker詳情與集羣架構部分能夠查看:http://www.roncoo.com/course/view/3e9d9c48f76f4c698b8349d04b763467