Docker Usage Guide (6) - Deploying Django Container Stack with Docker

Deploy Django applications with Docker

This experiment environment: Tencent Cloud Server  CentOS 6.7 x86_64

Since the download speed of Docker Hub images is very slow in China, the images provided by daocloud are used this time.

Deploying Django applications with Docker can be done in two ways: iterative builds and container interconnection. The following uses container interconnection to build a Django container stack.

required image

  • Docker version 1.7.1
  • daocloud.io/nginx:1.11
  • daocloud.io/python:2.7
  • daocloud.io/mysql:5.6
  • daocloud.io/django:1.9

Order of container creation:
mysql --> redis --> django --> nginx

Please download all required images before building images.

1. Create a mysql container

First create a directory for building the container:

# mkdir /docker
# cd /docker/

Then create the following directories to store the corresponding files:

├── mysql  
│   ├── conf.d    
│   │   ├── jianshu.sql       ---对应的 django 数据库文件,需要手动导入
│   │   ├── character.cnf  ---设定字符集
│   │   └── my.cnf         ---额外配置
│   ├── data               ---挂载数据库文件的目录
│   └── start.sh           ---容器启动脚本

Here is the startup script for the mysql container:

#!/bin/bash 
    #

echo "---------------start mysql image-------------------"
docker run --name mysql \
-v $(pwd)/conf.d:/etc/mysql/conf.d \
-v $(pwd)/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-p 3307:3306 \
-d daocloud.io/mysql:5.6.30

The above script creates a container named mysql, mounts the container's configuration file directory and data directory and initializes the mysql password.

2. Create a redis container

Use redis to cache backend data.
The redis container requires no special handling.

├── redis
│   └── start.sh

Start script:

 #!/bin/bash 
#

docker run --name redis -d daocloud.io/redis:3.0.7

3. Create a django container

To create a django container, you first need a django image, which is the environment required to install django in the daocloud.io/python:2.7 image. Then interconnect the django container with the mysql and redis containers.

└── web
    ├── jianshu.tar.gz       ---app 打包文件
    ├── Dockerfile          ---构建 django 镜像所使用的 Dockerfile
    ├── requirements.txt  ---app 依赖的库
    ├── start.sh          ---启动脚本
    └── stop.sh

The following is the Dockerfile file information:

# 基础镜像
FROM daocloud.io/python:2.7

# 维护者信息
MAINTAINER tianfeiyu <www.tianfeiyu.com>

ADD blog.tar.gz /usr/src/ 

# app 所在目录
WORKDIR /usr/src/jianshu

# 安装 app 所需依赖
RUN pip install --no-cache-dir -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

Start script:

 #!/bin/bash 
# 
docker exec -d mysql mysql -uroot -p123456 -e "create database blog;"
docker build -t feiyu/django-app .
docker run --name django \
-v /usr/src/jianshu \
-v /usr/src/jianshu/static \                                                                 --link mysql:mysql \
--link redis:redis \
-p 12000:8000 \
-d feiyu/django-app /usr/local/bin/uwsgi --http :8000 --chdir /usr/src/jianshu -w jianshu.wsgi

The options above are used  –link to enable secure interactive communication between containers. Using the format ,  this parameter can be reused  name:aliasin one command. When used  , the connection is determined by the container name. It is recommended to customize the container name when starting the container.docker run–link

通过 –link 选项来建立容器间连接,不但可以避免容器的 IP 和端口暴露到外网所导致的安全问题,还可以防止容器在重启后 IP 地址变化导致的访问失效,它的原理类似于 DNS 服务器的域名和地址映射。当容器的 IP 地址发生变化时,Docker 将自动维护映射关系中 IP 地址。

Docker 通过 2 种方式为容器公开连接信息:环境变量和更新 /etc/hosts 文件。

依旧使用 uwsgi来启动 django应用程序,也可以使用gunicorn 来启动。

4. 创建 nginx 容器

nginx 容器的创建比较简单,先在构建镜像时将 nginx 的配置文件复制到镜像,再将 nginx 容器与 django 容器进行互联并且挂载 django 容器中的数据卷。

├── nginx
│   ├── Dockerfile                 ---构建 nginx 镜像的 Dockerfile
│   ├── nginx-conf                    
│   │   └── django_project.conf       ---提供的 nginx 配置文件 
│   ├── restart.sh
│   ├── start.sh
│   └── stop.sh

Dockerfile 文件:

FROM daocloud.io/nginx

MAINTAINER tianfeiyu <www.tianfeiyu.com>                                                                            

RUN rm /etc/nginx/conf.d/default.conf
ADD nginx-conf/ /etc/nginx/conf.d/

启动脚本:

#!/bin/bash 
#                                                                                                                   
docker build -t nginx .        
docker run --name nginx-server \
--link django:web \            
-v /www/static \               
--volumes-from django \        
-p 8888:80 \
-d nginx

到此,所有容器的创建过程都已经了解清楚,所有文件的目录树如下所示:

5. 启动容器栈

为了方便测试,每个需要创建的容器下面都会有一个启动脚本,并且有一个控制所有容器的启动脚本与停止脚本:

 #!/bin/bash
#
cd mysql                                                                                                            
echo "start mysql----------------"
./start.sh

cd ../redis  
echo "start redis---------------------"
./start.sh

cd ../web 
echo "start web ---------------------"
./start.sh

cd ../nginx
echo "start nginx-------------------"
./start.sh

然后进入到 mysql 容器中将 django 数据库文件导入:

# docker inspect --format "{{.State.Pid}}" mysql
12674
# nsenter --target 12674 --mount --uts --ipc --net --pid
root@91308514f209:/# cd /etc/mysql/conf.d/
root@91308514f209:/etc/mysql/conf.d# mysql -uroot -p jianshu < jianshu.sql

github 项目地址
完整的代码请查看 github 项目地址 !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325779258&siteId=291194637