Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy the Django container stack using Docker

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article to share with you is about how to use Docker deployment Django container stack, Xiaobian feel quite practical, so share to everyone to learn, I hope you can read this article after harvest, not much to say, follow Xiaobian to see it.

Deploying Django apps with Docker

This experiment environment: CentOS 6.7 x86_64 server of Tencent Cloud

Because Docker Hub images download very slowly in China, this time we use images provided by daocloud.

Docker deployment of Django applications can be accomplished in two ways: iterative building and container interconnection. Here's how to build a Django container stack using container interconnection:

Required Mirrors

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 them.

1. Creating 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 ---The corresponding django database file needs to be imported manually │ <$── character.cnf ---Set character set <$ │ └── my.cnf ---Additional configuration <$ ├── data ---Directory where database files are mounted <$ └── start.sh ---Container Startup Script

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 script above creates a container named mysql, mounts the container's configuration file directory and data directory, and initializes mysql's password.

2. Create redis container

Use redis to cache backend data.

Redis containers do not require special handling.

├── redis│ └── start.sh

Start script:

#!/ bin/bash #docker run --name redis -d daocloud.io/redis:3.0.73. Create a django container

To create a django container, you first need a django image, 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 package file ├── Dockerfile Dockerfile used to build django image <$── requirements.txt ---app dependent libraries ├── start.sh ---Start Script └── stop.sh

The following is the Dockerfile file information:

#Base Image FROM daocloud.io/python:2.7# Maintainer Info MAINTAINER tianfeiyu ADD blog. tag. gz/usr/src/ # app directory WORKDIR /usr/src/jianshu#dependencies required to install 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-link option above is used to enable secure interactive communication between containers. Use the format name:alias. You can reuse this parameter in a docker run command. When using-link, the connection determines the container by the container name. It is recommended to customize the container name when starting the container.

By using the-link option to establish connections between containers, you can not only avoid security problems caused by exposing the IP and port of the container to the external network, but also prevent access failures caused by IP address changes after the container is restarted. Its principle is similar to the domain name and address mapping of DNS servers. When the IP address of the container changes, Docker will automatically maintain the IP address in the mapping relationship.

Docker exposes connection information to containers in two ways: environment variables and updating the/etc/hosts file.

The django application is still launched using uwsgi, but it can also be launched using gunicorn.

4. Create an nginx container

The creation of nginx container is relatively simple. First copy the nginx configuration file to the image when building the image, and then interconnect the nginx container with the django container and mount the data volume in the django container.

├── nginx│ ├── Dockerfile ---Dockerfile to build nginx image <$ ├── nginx-conf │ │ └── django_project.conf ---nginx configuration file provided <$ ├── restart.sh│ ├── start.sh│ └── stop.sh

Dockerfile:

FROM daocloud.io/nginxMAINTAINER tianfeiyu RUN rm /etc/nginx/conf.d/default.confADD nginx-conf/ /etc/nginx/conf.d/

Start 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 process of creating all containers is clear, and the directory tree for all files is as follows:

5. Start container stack

For testing purposes, there will be a startup script under each container that needs to be created, as well as a startup script and a shutdown script that control all containers:

#!/ bin/bash#cd mysql echo "start mysql----------------"./ start.shcd ../ redis echo "start redis---------------------"./ start.shcd ../ web echo "start web ---------------------"./ start.shcd ../ nginxecho "start nginx-------------------"./ start.sh

Then enter the mysql container and import the django database file:

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report