Deploy the Django container stack using Docker

Source: Internet
Author: User
Tags redis create database docker hub docker run aliyun

Deploy a Django application using Docker
Environment: Tencent ECS CentOS 6.7 x86_64
Since Docker Hub images are very slow to download in China, they are all images provided by daocloud. Docker basic operations can refer to the http://www.tianfeiyu.com /? Cat = 159.

Docker can deploy Django applications in two ways: iterative building and container interconnection. The following describes how to build a Django container stack using container interconnection.

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
Container creation sequence:

Mysql --> redis --> django --> nginx
Architecture diagram

Download all required images before creating an image.
1. Create a mysql container
First, create a directory for building containers:

# Mkdir/docker
# Cd/docker/
Then, create the following directories to store the corresponding files:

── Mysql
│ ── Conf. d
│ ── Jianshu. SQL --- corresponding django database file, which needs to be manually imported
│ ── Character. cnf --- set character set
│ ── My. cnf --- Additional configuration
│ ── Data --- directory for mounting database files
│ ── Start. sh --- container startup script
The following is the mysql container startup script:

#! /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 configuration file directory and Data Directory of the container, and initializes the mysql password.

2. Create a redis container
Use redis to cache back-end data.
Redis containers do not need special processing.

── Redis
│ └ ── Start. sh
Startup 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, that is, the environment required for installing django in the daocloud. io/python: 2.7 image. Connect the django container with the mysql and redis containers.

── Web
── Jianshu.tar.gz --- app package file
── Dockerfile --- the Dockerfile used to build the django image
── Requirements.txt --- Library on which the app depends
── Start. sh --- start script
── Stop. sh
The following is the Dockerfile information:

# Basic image
FROM daocloud. io/python: 2.7
   
# Maintainer information
MAINTAINER tianfeiyu <www.tianfeiyu.com>
   
ADD blog.tar.gz/usr/src/
   
# App directory
WORKDIR/usr/src/jianshu
   
# Dependencies required for app installation
RUN pip install -- no-cache-dir-r requirements.txt-I http://mirrors.aliyun.com/pypi/simple/ -- trusted-host mirrors.aliyun.com
Startup script:

#! /Bin/bash
    #
Docker exec-d 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 "-link" option is used to implement secure interactive communication between containers. The parameter format is name: alias. This parameter can be reused in a docker run command. When "-link" is used, the container name is used to determine the container connection. We recommend that you customize the container name when starting the container.

You can use the-link option to establish a connection between containers, which not only avoids security problems caused by the exposure of the container's IP address and port to the Internet, it can also prevent access failure caused by IP address changes after the container is restarted. The principle is similar to DNS server domain name and address ING. When the container's IP address changes, Docker automatically maintains the IP address in the ing relationship.

Docker exposes connection information for containers in two ways: environment variables and/etc/hosts files.
You can still use uwsgi to start the django application or gunicorn.

4. Create an nginx container
Creating nginx containers is relatively simple. First, copy the nginx configuration file to the image when creating the image, then connect the nginx container with the django container and mount the data volumes in the django container.

── Nginx
│ ── Dockerfile --- build the Dockerfile of the nginx image
│ ── Nginx-conf
│ ── Django_project.conf --- nginx configuration file provided
│ ── 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/
 
Startup script:

#! /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
At this point, the creation process of all containers has been clearly understood, and the directory tree of all files is as follows:

Directory tree

5. Start the container stack
To facilitate the test, each container to be created has a startup script and a script to control the startup and stop of all containers:

#! /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
Startup complete

Go to the mysql container and import the django database file:

# 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.