Please enable Javascript to view the contents

从零开始使用 Docker 打包 Django 开发环境 (6) uWSGI、Django

 ·  ☕ 3 分钟

1. 目录结构

  • django-devops-uwsgi 目录是打包 uWSGI 和 Django 的镜像编译文件。在镜像中安装 uWSGI、pip、virtualenv等必要的程序包。

    在使用镜像创建容器时,执行 start.sh 脚本, 创建 Python 虚拟运行环境,从 Django 的 requirements.txt 安装依赖包,最后启动 uWSGI 监听端口,等待请求。
  • django-devops-compose/www/app 目录,用于放置 Django 的代码。需要说明的是:为了减少工程的目录层级,通过改造,将 url、settings 等与工程全局相关的文件移动到了根目录。
  • django-devops-compose/www/conf 目录,用于存放 uWSGI 配置和环境变量。这里的环境变量有 pip 源地址等。
  • django-devops-compose/www/env 目录用于存放 virtualenv 创建的 Python 虚拟环境。这是为了避免,每次启动容器时,都需要重新安装依赖包,加快容器启动时间。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
└── django-devops-uwsgi
│   ├── Dockerfile
│   └── start.sh
├── django-devops-compose
│   ├── docker-compose.yml
│   └── www
│       ├── app
│       │   ├── manage.py
│       │   ├── requirements.txt
│       │   ├── settings.py
│       │   ├── static
│       │   ├── urls.py
│       │   ├── wsgi.py
│       ├── conf
│       │   ├── common.env
│       │   ├── uwsgi.ini
│       ├── env
│       ├── log
│       │   ├── mysql
│       │   └── uwsgi

2. Dockerfile

镜像文件:django-devops-uwsgi/Dockerfile

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
FROM centos:7

RUN yum -y install epel-release

RUN yum -y install gcc

RUN yum -y install python-devel python-pip mysql-devel

RUN pip install --upgrade pip

RUN pip install  virtualenv

RUN yum -y install pcre pcre-devel pcre-static

RUN  yum  -y  clean all

RUN mkdir /var/www

COPY ./start.sh /

RUN chmod +x /start.sh

CMD ["/start.sh"]

EXPOSE 8000

启动脚本文件:django-devops-uwsgi/start.sh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/bash

echo "python virtual env ------ start"
cd /var/www

virtualenv ./env
source ./env/bin/activate
pip install uwsgi --index=http://${PYPI_SOURCE} --trusted-host ${PYPI_SOURCE%%/*}
echo "python virtual env ------ end"

echo "install requirements.txt ------ start"
echo "pypi source, ${PYPI_SOURCE}"
/var/www/env/bin/pip install -r /var/www/app/requirements.txt  --index=http://${PYPI_SOURCE} --trusted-host ${PYPI_SOURCE%%/*}
echo "install requirements.txt ------ end"

cd /var/www/app

echo "syncdb ------ start"
python manage.py syncdb
echo "syncdb ------ end"

echo "migrate ------ start"
python manage.py migrate
echo "migrate ------ end"

echo "uwsgi ------ start"
uwsgi --master --ini /var/www/conf/uwsgi.ini
echo "uwsgi ------ end"

docker-compose.yml 配置文件:django-devops-compose/docker-compose.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
version: '2'

services:

    nginx:
      build: ../django-devops-nginx/
      volumes:
        - ./www/:/var/www/
      links:
        - uwsgi
      ports:
        - "80:80"
      expose:
        - "80"

    uwsgi:
      # image: djangodevops_uwsgi
      build: ../django-devops-uwsgi/
      privileged: true
      env_file:
        - ./www/conf/common.env
      volumes:
        - ./www/:/var/www/
      depends_on:
        - mysql
        # - redis
        # - rabbitmq
      links:
        - mysql
        # - redis
        # - rabbitmq
      ports:
        - "8000:8000"
      expose:
        - "8000"

    mysql:
      image: mariadb
      restart: always
      ports:
        - "3306:3306"
      expose:
        - "3306"
      volumes:
        - ./www/mysql/:/var/lib/mysql/
      environment:
        - MYSQL_ROOT_PASSWORD=root
        - MYSQL_DATABASE=app

环境变量文件:django-devops-compose/www/conf/common.env

1
2
3
4
5
6
7
# Set environment
# APP_NAME=app
# DATABASE_NAME=app
# MYSQL_USER=root
# MYSQL_PASSWORD =
PYPI_SOURCE=mirrors.aliyun.com/pypi/simple
# PYPI_SOURCE=pypi.douban.com/simple

uSWGI 配置文件:django-devops-compose/www/conf/uwsgi.ini

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[uwsgi]
chmod-socket=666
socket = uwsgi:8000
chdir = /var/www/app/
logto = /var/www/log/uwsgi/uwsgi.log
module = wsgi:application
py-autoreload = 1
master = 1
processes = 2
threads = 2

3. 常用的一些命令

启动容器,Console 推出时,容器停止。

1
docker   run -p 800:80 imagename

后台启动容器,不占用 Console,后台运行。

1
docker   run -d -p 800:80 imagename

启动容器,并通过 ssh 登陆

1
docker run -it --rm  imagename /bin/bash

批量停止容器

1
docker stop $(docker ps -q)

批量重启容器

1
docker restart $(docker ps -q)

批量删除容器

1
docker rm $(docker ps -a -q)

批量删除 :镜像

1
docker rmi $(docker images -f "dangling=true" -q)

批量删除镜像

1
docker rmi $(docker images -q)

微信公众号
作者
微信公众号