Python开发

python(15)实践Django-docker镜像

2017-08-10  本文已影响59人  灼灼2015

软件,不可避免的就是发布问题。为了减少搭建过程中遇到的困难,采用了docker镜像。

  1. python基础环境
     FROM centos:6.6
     MAINTAINER chances-lixr
     WORKDIR /tmp
     RUN yum -y install wget sqlite-devel xz gcc automake zlib-devel openssl-devel;     yum clean all
     RUN yum -y install tar; yum clean all
     # Install Python
     RUN wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz && \
     tar xvf Python-3.6.1.tar.xz  && cd Python-3.6.1 && ./configure && make && make install &&  \
     rm -rf /tmp/{Python-3.6.1.tar.xz,Python-3.6.1}
     RUN mv /usr/bin/python /usr/bin/python2
     RUN ln -s /usr/local/bin/python3 /usr/bin/python && ln -s /usr/local/bin/pip3 /usr/bin/pip
     RUN sed -i 's@/usr/bin/python@/usr/bin/python2@g' /usr/bin/yum
    
    通过docker built -t python3:v1.0 ./ 的到python03:v1.0 的基础镜像。
  2. 软件的镜像制作
    FROM jumpserver/python:v3.6.1
    MAINTAINER chances
    COPY . /opt/Testmanager
    #将项目文件全部拷贝到/opt/Testmanager目录下
    WORKDIR /opt/Testmanager
    RUN yum -y install epel-release
    RUN cd requirements && yum -y install $(cat rpm_requirements.txt)
    RUN cd requirements && pip install -r requirements.txt -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
    RUN yum clean all
    VOLUME /opt/Testmanager/data
    RUN cp docker-config.py config.py
    EXPOSE 8080
    #CMD cd utils && sh make_migrations.sh && sh init_db.sh && cd .. && python run_server.py
    CMD python run_server.py
    
  3. 依赖包
    rpm_requirements.txt 安装的是操作系统所需的依赖包
    libtiff-devel libjpeg-devel libzip-devel freetype-devel lcms2-devel libwebp-devel tcl-devel tk-devel sshpass openldap-devel
    requirements.txt中为软件所需的依赖包
Django>=1.11.3
django-bootstrap3>=8.2.2
Pillow>=4.1.0
djangorestframework>=3.6.2
ForgeryPy
celery>=4.0.2
paramiko>=2.1.2
django-simple-captcha>=0.5.5
django-formtools>=2.0
djangorestframework-bulk>=0.2.1
django-redis-cache>=1.7.1
requests>=2.13.0
itsdangerous>=0.24
eventlet
django-filter>=1.0.2
passlib>=1.7.1
gssapi
django-rest-swagger
django-auth-ldap
ldap3
urllib3
redis
python-jenkins
  1. 启动文件
#!/usr/bin/env python
# ~*~ coding: utf-8 ~*~
from threading import Thread
import os
import subprocess
try:
    from config import config as env_config, env
    CONFIG = env_config.get(env, 'default')()
except ImportError:
    CONFIG = type('_', (), {'__getattr__': None})()
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
apps_dir = os.path.join(BASE_DIR, './')

def start_django():
    http_host = CONFIG.HTTP_BIND_HOST or '127.0.0.1'
    http_port = CONFIG.HTTP_LISTEN_PORT or '8080'
    os.chdir(apps_dir)
    print('start django')
    subprocess.call('python ./manage.py runserver %s:%s' % (http_host, http_port), shell=True)

def start_celery():
    os.chdir(apps_dir)
    os.environ.setdefault('C_FORCE_ROOT', '1')
    os.environ.setdefault('PYTHONOPTIMIZE', '1')
    print('start celery')
    subprocess.call('celery -A Testmanager worker -B -s /tmp/celerybeat-schedule -l info', shell=True)
    subprocess.call('celery -A Testmanager beat -B -s /tmp/celerybeat-schedule -l info', shell=True)

def main():
    t1 = Thread(target=start_django, args=())
    t2 = Thread(target=start_celery, args=())

    t1.start()
    t2.start()

    t1.join()
    t2.join()

if __name__ == '__main__':
    main()
  1. docker-compose.yml文件
version: '2'
services:
  Testmanager:
    image: testmanager:v1.0
    container_name: testmanager
    restart: always
    ports:
      - 18080:8080
    volumes:
      - /opt/test/lixr/dockertestmanager/data:/opt/testmanager/data

docker-compose up -d 启动镜像

  1. 访问方式
    http://ip:18080/ 进行访问
上一篇下一篇

猜你喜欢

热点阅读