# 1.制作基礎鏡像
leanote 使用 mongodb 存儲數據,如果把 mongodb 單獨做成一個鏡像,初始化數據時比較麻煩,所以最后還是決定把 mongodb 和 leanote 放到同一個鏡像里邊。
docker容器啟動后,必須有一個進程前台運行,這個前台進程結束時docker容器結束,docker stop 時會給前台進程發信號,所以前台進程可以正常結束,而后台進程不會收到結束信號,前台進程結束時后台進程會被強制結束。
如果只是簡單的啟動 mongodb 和 leanote,mongodb 后台運行,leanote前台運行,結束容器的時候,leanote會正常結束,而mongodb則會非法結束,引起mongodb數據不一致。
其中一個解決辦法就是使用supervisor作為前台進程,其他進程作為supervisor的子進程。
## 1.1.編寫Dockerfile文件
使用easy_install來安裝supervisor
cd /home/zhuyr/leanote [root@dev leanote]# cat Dockerfile FROM centos MAINTAINER zhuyr<304748716@qq.com> # supervisor配置文件路徑 ENV SUPERVISORD_CONF=/etc/supervisord.conf # supervisor臨時文件路徑(日志文件、sock文件、pid文件) ENV SUPERVISORD_TMP_CONF=/tmp/supervisor # supervisor程序塊文件路徑,即是[program]塊 ENV SUPERVISORD_INCLUDE_FILE=/etc/supervisordfile # web管理界面的IP ENV SUPERVISORD_WEB_IP=* # web管理界面的PORT ENV SUPERVISORD_WEB_PORT=9001 # web管理界面的賬號 ENV SUPERVISORD_WEB_ACCOUNT=admin # web管理界面的密碼 ENV SUPERVISORD_WEB_PASSWORD=adminpass RUN mkdir -p ${SUPERVISORD_TMP_CONF} RUN mkdir -p ${SUPERVISORD_INCLUDE_FILE} RUN yum -y update RUN yum install -y python-setuptools wget telinit RUN wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py -O |python RUN easy_install supervisor RUN echo -e "[unix_http_server]\nfile=${SUPERVISORD_TMP_CONF}/supervisor.sock\n[inet_http_server]\nport=${SUPERVISORD_WEB_IP}:${SUPERVISORD_WEB_PORT}\nusername=${SUPERVISORD_WEB_ACCOUNT}\npassword=${SUPERVISORD_WEB_PASSWORD}\n[supervisord]\nlogfile=${SUPERVISORD_TMP_CONF}/supervisord.log\nlogfile_maxbytes=50MB\nlogfile_backups=10\nloglevel=info\npidfile=${SUPERVISORD_TMP_CONF}/supervisord.pid\nnodaemon=false\nminfds=1024\nminprocs=200\n[supervisorctl]\nserverurl=unix://${SUPERVISORD_TMP_CONF}/supervisor.sock\n[include]\nfiles = ${SUPERVISORD_INCLUDE_FILE}/*.ini" > ${SUPERVISORD_CONF} USER root EXPOSE 22 80 9001 RUN /usr/sbin/init & RUN /usr/sbin/telinit &
## 1.2 生成supervisor基礎鏡像
-t 生成的鏡像名稱
--rm 生成成功后刪除中間鏡像
--no-cache 不使用之前生成后緩存的中間鏡像
注意命令后邊跟的.,表示當前目錄
[root@dev leanote]# docker build -t zhuyr/supervisor:1.0 --rm --no-cache . [root@dev leanote]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zhuyr/supervisor 1.0 e116f6895b37 About a minute ago 236.6 MB ubuntu 14.04 7c09e61e9035 7 days ago 187.9 MB
## 1.3 下載安裝包
[root@test2 leanote]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.1.tgz [root@test2 leanote]# wget https://static.axboy.cn/leanote/leanote-linux-amd64-v2.6.1.bin.tar.gz
# 2.制作leanote docker鏡像
## 2.1 編寫Dockerfile文件(修改原Dockerfile文件)
鏡像內包含 mongodb 和 leanote。
使用ADD指令添加*.tar.gz時會自動解壓。
[root@dev leanote]# cat Dockerfile # zhuyr/leanote:2.6.1 # with mongodb FROM zhuyr/supervisor:1.0 MAINTAINER zhuyr<304748716@qq.com> ADD init.sh /root/init.sh #ADD supervisord.conf /etc/supervisor/conf.d/supervisord.conf ADD supervisord.conf /etc/supervisord.conf COPY leanote-linux-amd64-v2.6.1.bin.tar.gz /root/ COPY mongodb-linux-x86_64-3.0.1.tgz /root/ RUN mkdir -p /root/db && \ cd /root && \ tar -xvf /root/mongodb-linux-x86_64-3.0.1.tgz && \ tar -xvf /root/leanote-linux-amd64-v2.6.1.bin.tar.gz && \ mv /root/mongodb-linux-x86_64-3.0.1 /root/mongodb && \ mkdir -p /root/conf_bak && \ cp /root/leanote/conf/* /root/conf_bak && \ chmod a+x /root/init.sh && \ chmod a+x /root/leanote/bin/run.sh EXPOSE 9000 CMD ["/usr/bin/supervisord"] [root@dev leanote]#
## 2.2 寫supervisor文件
supervisord.conf里配置啟動的進程,mongodb啟動時指定數據庫文件目錄/root/db以后存放mongodb數據,leanote啟動時先運行init.sh,再運行run.sh
[root@dev leanote]# cat supervisord.conf [supervisord] nodaemon=true [program:mongodb] command=/root/mongodb/bin/mongod --dbpath /root/db [program:leanote] command=/bin/bash -c "/root/init.sh && /root/leanote/bin/run.sh"
## 2.3 初始化數據庫或添加配置文件
init.sh初始化數據庫。通過判斷是否存在文件/root/db/already-init-db來確定是否需要初始化數據庫。數據庫初始化時創建該文件。leanote的初始數據存放於/root/leanote/mongodb_backup/leanote_install_data/目錄下
生成鏡像時對conf目錄進行了備份,如果文件/root/leanote/conf/app.conf不存在,則說明容器啟動時將conf目錄作為數據卷掛載到了其他地方,這時就從備份文件恢復配置文件。
[root@dev leanote]# cat init.sh #!/bin/bash #set -m # 如果數據庫未初始化,則初始化數據庫 if [ ! -f "/root/db/already-init-db" ] ; then touch /root/db/already-init-db /root/mongodb/bin/mongorestore -h localhost -d leanote --dir /root/leanote/mongodb_backup/leanote_install_data/ fi # 如果配置文件不存在,則復制配置文件 if [ ! -f "/root/leanote/conf/app.conf" ] ; then cp /root/conf_bak/* /root/leanote/conf/ fi
## 2.4 目錄內容顯示
[root@dev leanote]# ll 總用量 58876 -rw-r--r--. 1 root root 675 3月 7 20:33 Dockerfile -rw-r--r--. 1 root root 428 3月 7 20:32 init.sh -rw-r--r--. 1 root root 20717785 3月 7 20:35 leanote-linux-amd64-v2.6.1.bin.tar.gz -rw-r--r--. 1 root root 39554547 3月 7 20:35 mongodb-linux-x86_64-3.0.1.tgz -rw-r--r--. 1 root root 197 3月 7 20:31 supervisord.conf [root@dev leanoteDockerFile]#
## 2.5生成leanote鏡像
[root@dev leanoteDockerFile]# docker build -t zhuyr/leanote:2.6.1 --rm --no-cache . [root@dev leanoteDockerFile]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zhuyr/leanote 2.4 60896a4094b8 12 seconds ago 477.4 MB zhuyr/supervisor 1.0 e116f6895b37 15 minutes ago 236.6 MB ubuntu 14.04 7c09e61e9035 7 days ago 187.9 MB [root@dev leanoteDockerFile]#
# 3.運行docker
第一次啟動。
三個目錄,一個存放mongodb數據庫文件,一個存放leanote配置文件,一個存放leanote附件。一個端口9000,用於訪問leanote。
[root@dev leanote]# docker run --name leanote -d -v /leanote/leanotedb:/root/db -v /leanote/conf/:/root/leanote/conf/ -v /leanote/files:/root/leanote/files -p 9000:9000 zhuyr/leanote:2.6.1
后台初始帳戶密碼:admin abc123
## 3.1 修改mongodb數據庫和leanote的配置文件
[root@test2 leanote]# docker exec -it leanote /bin/bash [root@33106c3f0054 bin]# pwd /root/mongodb/bin [root@33106c3f0054 bin]# ./mongo >show dbs;
leanote 0.078GB
local 0.078GB
> use leanote; switched to db leanote > db.createUser({ ... user:'zhuyr', ... pwd:'abc123', ... roles:[{role:'dbOwner',db:'leanote'}] ... }); Successfully added user: { "user" : "zhuyr", "roles" : [ { "role" : "dbOwner", "db" : "leanote" } ] } > db.auth("zhuyr","abc123"); 1 > #查看leanote配置文件 [root@33106c3f0054 leanote]# cd conf/ [root@33106c3f0054 conf]# pwd /root/leanote/conf [root@33106c3f0054 conf]# cat app.conf
## 4.問題處理
如果mongodb里的leanote沒導入成功,則進入容器重新執行以下命令:
/root/mongodb/bin/mongorestore -h localhost -d leanote --dir /root/leanote/mongodb_backup/leanote_install_data/
登錄后,進入后台管理-配置-站點URL,修改成你的實際域名或IP地址如:http://172.16.160.210:9000
