說明: 我一次build 完 因此images 包 有1G多 可分層build bash 環境一層 應用程序及啓動腳本(shell.sh) 一層 python
步驟: mysql
1 基礎 Images 鏡像 sairamnusetty/centos6.8-python2.7.13 git
2 從github 上 git proect 下來並修改配置文件github
3 運行 install-project.sh 自動下載 git proect 在次運行 自動構建docker-images 而後 run proectredis
整個目錄文件sql
install-project.sh 代碼以下: 第一次運行檢查當下目錄是否存在OpsManage 項目文件 如沒有自動從github上git 下來 docker
#!/bin/bas h #author Mrchen 237356573@qq.com PROCJECT="./OpsManage" DOCKERFILE="./dockerfile" run_start() { #run build-project-image echo -e "\033[41;30m正在----Run build-Images-----\033[0m" docker build -t python-django:opsmanage-v1 . #run docker-images echo -e "\033[41;30m正在----Docker---Run-Images-----\033[0m" docker run --name project -p 80:8000 -d python-django:opsmanage-v1 } echo -e "\033[41;30m 正在檢查文件 \033[0m" if [ ! -d "$PROCJECT" ]; then echo -e "\033[41;30m正在-----git clone https://github.com/welliamcao/OpsManage.git----\033[0m" yum install wget git -y && git clone https://github.com/welliamcao/OpsManage.git echo -e "\033[41;30m請先修改modify Redis-host-IP Mysql-host-IP user password db---\033[0m" echo -e "\033[41;30m文件位置---./OpsManage/OpsManage/settings.py---\033[0m" echo -e "\033[41;30m 在運行該腳本 \033[0m" else if [[ ! -f "$DOCKERFILE" ]]; then cat > ./Dockerfile <<\EOF # MAINTAINER Mrchen <237356573@qq.com> # DOCKER-VERSION 1.0 FROM sairamnusetty/centos6.8-python2.7.13 MAINTAINER The CentOS Project <237356573.qq.com> RUN yum install wget gcc -y RUN wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo RUN yum clean all RUN yum install epel-release -y RUN yum repolist #install django-base-server RUN yum install zlib zlib-devel readline-devel sqlite-devel bzip2-devel openssl-devel gdbm-devel libdbi-devel ncurses-libs kernel-devel libxslt-devel libffi-devel python-devel mysql-devel zlib-devel mysql-server sshpass gcc wget git openssl-devel -y #install python-django-pip-base COPY ./requirements.txt /mnt RUN pip install -r /mnt/requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/ #mount proect OpsManage VOLUME ["/mnt/OpsManage"] COPY OpsManage /mnt #run .sh .py COPY ./loading-mariadb.sh /mnt COPY ./start-proect.sh /mnt #開放8000端口 EXPOSE 8000 #load data RUN chmod +x /mnt/start-proect.sh #ENTRYPOINT ["/mnt/start-proect.sh"] CMD ["/mnt/start-proect.sh"] EOF echo -e "new pip config" cat > ./requirements.txt <<\EOF django==1.8.17 Celery==3.1.20 django-celery celery-with-redis djangorestframework==3.5.3 paramiko ansible==2.2.2 redis xlrd supervisor MySQL-python DBUtils EOF cat > ./start-proect.sh <<\EOF #!/bin/bash #237356573 OPS_DIR=/mnt #--------------RUN---Opsmange--------------------------- if [[ ! -d ${OPS_DIR}/OpsManage ]];then echo "not proectj-DIR " else cd ${OPS_DIR}/OpsManage python ${OPS_DIR}/manage.py migrate STR=$? if [[ ! STR -ne 0 ]];then chmod +x /mnt/loading-mariadb.sh bash /mnt/loading-mariadb.sh sleep 1 python ${OPS_DIR}/manage.py runserver 0.0.0.0:8000 else echo "----------------mysql----loading-----ERROR-------" fi fi EOF #---------------------------RUN---------------------------------- echo -e "...new--all--files..................." read -r -p "ls it running?[Y/n]" PROECT case $PROECT in [yY][eE][sS]|[yY]) run_start ;; [nN][oO]|[nN]) echo "-----exit-----" exit ;; esac else read -r -p "all files exis ls it running?[Y/n]" PROECT case $PROECT in [yY][eE][sS]|[yY]) run_start ;; [nN][oO]|[nN]) echo "-----exit-----" exit ;; esac fi fi
運行: bash ./install-project.shshell
vim ./OpsManage/OpsManage/settings.py 修改 redis mariadb 及 port 咱們修改成 172.16.0.200 django
mariadb 咱們修改成:vim
修改完咱們在來看一下 loading-mariadb.sh 該文件爲啓動時修改python-django 登錄時賬號及密碼:
在次運行 bash ./install-project.sh
坐等 查看:
訪問 172.16.0.220 user: chen password : admin
images 打包 docker save e5c4cfc89496 > /mnt/opsmanage.ims
images loading進本地 倉庫 docker load < /mnt/opsmanage.ims
Tag docker tag e5c4cfc89496 python-django_opsmanage_v1
push 到私有倉庫 docker push 172.16.0.230:5000/python-django_opsmanage_v1
還能夠寫個 compose 在加 swarm集羣 也是我接下去要研究學習 還有 k8s 哦!
container (redis,mariadb,Opsmanages)下載地址: