Docker-Django项目实例

2018-10-17

以alex的crazyeye的django堡垒机应用来用django部署网页部分

1.基础文件和准备

基础镜像python:2.7 mysql:5.6 centos:7

crazyEye的文件包

创建mysql存储卷

docker create volume mysql_volume

创建环境网络

docker create network crazyeye

2.Django镜像制作

pip.conf 是pip加速配置文件

Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#Django and Uwsgi 

From python:2.7

COPY CrazyEye /home/CrazyEye

COPY pip.conf /etc/

WORKDIR /home/CrazyEye

RUN pip install -r requirements.txt

EXPOSE 8001

CMD ["/bin/sh","run.sh"]

建立django镜像

docker build -t django:v1 . 

3.nginx镜像制作

需要的软件包

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
26
27
28
29
FROM centos:7 	

ENV TIME_ZOME Asia/Shanghai

RUN yum -y install gcc gcc-c++ make openssl-devel pcre-devel

ADD nginx-1.12.1.tar.gz /tmp

RUN cd /tmp/nginx-1.12.1 && \

./configure --prefix=/usr/local/nginx && \

make -j 2 && \

make install

RUN rm -rf /tmp/nginx* && yum clean all && \

echo "${TIME_ZOME}" > /etc/timezone && \

ln -sf /usr/share/zoneinfo/${TIME_ZOME} /etc/localtime

COPY nginx.conf /usr/local/nginx/conf/

COPY CrazyEye /var/www/CrazyEye

WORKDIR /usr/local/nginx/

CMD ["./sbin/nginx","-g","daemon off;"]

nginx.conf

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
worker_processes  1; 		

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

upstream django:8001 {

}

server {

listen 8000;

charset utf-8;

location /static {

alias /var/www/CrazyEye/statics;

}

location / {

uwsgi_pass django;

include /var/www/CrazyEye/uwsgi_params;

}

}

server {

listen 80;

server_name localhost;

location / {

root html;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

}

建立nginx镜像

docker build -t nginx:v1 .

4.数据库mysql5.6

使用官方镜像,但是要使用永久数据存储,初始化一些变量root密码等

docker run -itd  --name mysql -vmysql-volume:/var/lib/mysql -p3306:3306 -e MYSQL_ROOT_PASSWORD=test123 mysql --character-set-server=utf8     

创建数据库

docker exec myslq -c sh "create database crazyeye" 

5.运行容器

docker run -itd --name mysql --mount src=mysql_volume,dst=/var/lib/mysql --network django -p3306:3306 mysql:5.6      

  docker run -itd --name django --network django django:v1     

  docker run -itd --name nginx --network django -p8000:8000 nginx:v1     

访问nginx的8000网站测试