This page looks best with JavaScript enabled

Building a Django Development Environment with Docker from Scratch (6) uWSGI, Django

 ·  ☕ 4 min read

1. Directory Structure

  • The django-devops-uwsgi directory holds the image build files that package uWSGI and Django. It installs the necessary packages such as uWSGI, pip, and virtualenv into the image.

    When a container is created from the image, the start.sh script runs: it creates the Python virtual runtime environment, installs the dependency packages from Django’s requirements.txt, and finally starts uWSGI to listen on the port and wait for requests.
  • The django-devops-compose/www/app directory is where the Django code goes. Note that to reduce the directory nesting of the project, files that relate to the project as a whole, such as url and settings, were moved to the root directory.
  • The django-devops-compose/www/conf directory stores the uWSGI configuration and environment variables. The environment variables here include the pip source address and so on.
  • The django-devops-compose/www/env directory stores the Python virtual environment created by virtualenv. This avoids having to reinstall the dependency packages every time the container starts, which speeds up container startup.
 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

Image file: 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

Startup script file: 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 configuration file: 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

Environment variable file: 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 configuration file: 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. Some Common Commands

Start a container; when the Console exits, the container stops.

1
docker   run -p 800:80 imagename

Start a container in the background, without occupying the Console, running in the background.

1
docker   run -d -p 800:80 imagename

Start a container and log in over ssh

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

Stop containers in bulk

1
docker stop $(docker ps -q)

Restart containers in bulk

1
docker restart $(docker ps -q)

Delete containers in bulk

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

Delete : images in bulk

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

Delete images in bulk

1
docker rmi $(docker images -q)

微信公众号
WRITTEN BY
微信公众号