docker 部署django方式


1 docker 部署django應用

1.1 基於python基礎鏡像

# 第一種方式:基於python基礎鏡像來做
cd /home
mkdir myproject
cd myproject

docker run -di --name=myproject -p 8080:8080 -v /home/myproject:/home python:3.6
      
#mac/linux    window:xshell拖進去
scp django_test.zip root@101.133.225.166:/home/myproject
# 解壓:uzip (安裝)yum install -y unzip zip

# 進入容器I 
docker exec -it myproject /bin/bash
# 切到項目路徑下:安裝依賴
pip install -r requirement.txt
# pip list
apt-get update
apt-get vim
# setting.py 改成下面
ALLOWED_HOSTS = ['*']
# 運行項目(wsgiref)
python manage.py runserver 0.0.0.0:8080
# 換uwsgi跑
pip install uwsgi
# 在項目根路徑下創建一個uwsgi.ini 文件,寫入

[uwsgi]
#配置和nginx連接的socket連接
socket=0.0.0.0:8080
#也可以使用http
#http=0.0.0.0:8080
#配置項目路徑,項目的所在目錄
chdir=/home/django_test
#配置wsgi接口模塊文件路徑
wsgi-file=django_test/wsgi.py
#配置啟動的進程數
processes=4
#配置每個進程的線程數
threads=2
#配置啟動管理主進程
master=True
#配置存放主進程的進程號文件
pidfile=uwsgi.pid
#配置dump日志記錄
daemonize=uwsgi.log


#啟動,停止,重啟,查看
uwsgi --ini uwsgi.ini #啟動
lsof -i :8001    #按照端口號查詢
ps aux | grep uwsgi   #按照程序名查詢
kill -9 13844       #殺死進程
uwsgi --stop uwsgi.pid      #通過uwsg停止uwsgi
uwsgi --reload uwsgi.pid  #重啟

# nginx轉發

mkdir -p nginx/conf nginx/html nginx/logs

在conf目錄下新建nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
          #uwsgi_pass 101.133.225.166:8080;
          proxy_pass http://101.133.225.166:8080;
        }  
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

docker run --name nginx -id -p 80:80 -v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/nginx/html:/etc/nginx/html -v /home/nginx/logs:/var/log/nginx nginx

# 在 python的docker中用uwsgi跑起項目來即可

外部訪問:http://101.133.225.166/

1.2 基於dockerfile


# 第二種方式:dockerfile

# 寫一個dockerfile即可

FROM python:3.6
MAINTAINER lqz
WORKDIR /home
RUN pip install django==1.11.9
RUN pip install uwsgi
EXPOSE 8080
CMD ["uwsgi","--ini","/home/django_test/uwsgi.ini"] 

# 這句命令,是后台執行的,不會夯住,容器里面就停了

# dockerfile路徑下要有一個django_test.tar

#構建鏡像
docker build -t='django_1.11.9' .
# 運行容器
docker run -di --name=mydjango -p 8080:8080 -v /home/myproject:/home django_1.11.9


# 以后只需要從git上拉下最新代碼,重啟,完事(最新代碼)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM