docker部署django项目

2021年4月27日 06:06 by wst

服务部署

初衷

为了对物理机产生的影响最小,这里所有项目的部署全部使用docker实现。

django项目的常规部署需要考虑三部分:

1. django本身的启动;

2. nginx转发;

3. nginx和django之间的转发,这里使用uwsgi;

 

实现方法

这里没有采用多docker实例的方法,而是所有服务全部在一个docker中实现。

接下来重点就是怎么编写dockerfile和配置文件;

这里直接贴上作者的方法。

Dockerfile

FROM python:3.7.10

#定义时区参数
ENV TZ=Asia/Shanghai

# 设置编码
ENV LC_ALL C.UTF-8

#设置时区
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo '$TZ' > /etc/timezone

# 更换系统下载源
# 设置工作目录
WORKDIR /code
COPY . /code
RUN cat /code/source.txt > /etc/apt/sources.list

# 安装nginx
RUN apt-get update \
    && apt-get -y dist-upgrade \
    && apt-get install -y nginx

# 安装uwsgi
RUN pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple
RUN pip install uwsgi -i https://mirrors.aliyun.com/pypi/simple

# 定义外部参数
ARG build_env
# 调试标记
ENV DEBUG {build_env}


# 设置工作目录
WORKDIR /code
COPY . /code
# 安装依赖
RUN pip install -r /code/requirements.txt -i https://mirrors.aliyun.com/pypi/simple
# 收集静态文件
RUN python manage.py collectstatic --noinput

# nginx configuration
RUN if [ "$build_env" = "true" ] ; \
    then cp nginx-config/business-test.conf /etc/nginx/conf.d; \
    else cp nginx-config/business.conf /etc/nginx/conf.d; fi

# 启动服务
COPY ./entrypoint.sh /code
RUN mkdir -p /data/logs
RUN chmod +x /code/entrypoint.sh
CMD ["bash", "/code/entrypoint.sh"]

配置文件

1. /code/source.txt

deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb http://mirrors.aliyun.com/debian-security buster/updates main
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib

deb-src http://mirrors.aliyun.com/debian-security buster/updates main
deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib

2. /code/requirements.txt

asgiref==3.3.4
bcrypt==3.1.4
cffi==1.14.5
Django==3.1.7
django-filter==2.4.0
djangorestframework==3.12.4
djangorestframework-jwt==1.11.0
djangorestframework-simplejwt==4.4.0
et-xmlfile==1.0.1
mysqlclient==2.0.3
numpy==1.19.5
openpyxl==3.0.7
pandas==1.1.5
pycparser==2.20
PyJWT==1.7.1
PyMySQL==1.0.2
python-dateutil==2.8.1
pytz==2021.1
six==1.15.0
SQLAlchemy==1.3.2
sqlparse==0.4.1
typing-extensions==3.7.4.3
xlrd==1.2.0

3. nginx-config/business-test.conf

server {
    listen 80;
    server_name admin-test.xxx.com.cn;
    root /code;
    access_log /data/logs/access.log;
    error_log /data/logs/error.log;

    location  /favicon.ico { access_log off; log_not_found off; }

    location  /static/ {
    #如果你的static目录不在root里,可以配置 alias /path/to/your/mysite/static;
    alias /code/static/;
    }

    location / {
      include uwsgi_params; # the uwsgi_params file you installed
      uwsgi_pass  127.0.0.1:5556;
    }
}

nginx-config/business.conf 内容同上,不同的地方为:第三行的是:server_name admin.xxx.com.cn;

4. entrypoint.sh

#/bin/bash

echo "uwsgi running..."
uwsgi --ini /code/mange.ini

echo "nginx starting..."
service nginx start
nginx -t
nginx -s reload

python -m http.server 1111 --bind 0.0.0.0

5. /code/mange.ini

[uwsgi]
# 项目根目录路径(full path)
chdir           = /code
pythonpath = /code
# Django的 wsgi 文件
wsgi-file = /code/business_management_system/wsgi.py
# 自动载入
py-autoreload=1
# 设置日志
daemonize   = %(chdir)/run.log
log-maxsize = 5000000
master          = true
pidfile=/tmp/business-master.pid
# 最大工作进程数(CPU密集型建议设为CPU核心数,IO密集型建议设为CPU核心数的两倍)
processes       = 5
# unix套接字文
socket          = 127.0.0.1:5556
# socket文件权限
# 退出时清空环境
vacuum          = true

 

总结

有难度的地方在于这些所有过程在dockerfile中怎么编排。

不过还好,终于搞定,项目顺利上线。

如有问题,欢迎评论!


Comments(129) Add Your Comment

Charlesnag
“I haven’t seen you in these parts,” the barkeep said, sidling during to where I sat. “Personage’s Bao.” He stated it exuberantly, as if low-down of his exploits were shared by way of settlers hither many a fire in Aeternum. He waved to a wooden keg hard by us, and I returned his gesticulate with a nod. He filled a eyeglasses and slid it to me across the stained red wood of the bench prior to continuing. “As a betting fellow, I’d be delighted to wager a fair speck of enrich oneself you’re in Ebonscale Reach on the side of more than the swig and sights,” he said, eyes glancing from the sword sheathed on my with it to the bow slung across my back. http://www.google.com.pk/url?q=https://renewworld.ru/data-vyhoda-new-world/
Kevingaw
http://die-fototasse.de/wp-content/plugins/element/welches_online_casino_ist_zu_empfehlen___1.html https://druckerei-festge.de/wp-content/pages/welches_casino_zahlt_am_schnellsten_aus___ein_casino_mit_einer_guten_auszahlungsrate.html http://maps.google.com.do/url?q=https://www.stadt-berga.de/wp-includes/pgs/?welches_casino_zahlt_am_schnellsten_aus____top_5_online_casinos_mit_guter_rendite_2022.html https://www.google.com.vn/url?sa=t&rct=j&q=&esrc=s&source=web&cd=35&cad=rja&uact=8&ved=0ahUKEwj589DM2JnUAhUBKpQKHR0HBw44HhAWCDQwBA&url=http://1xbitpromocode.org/ https://maps.google.iq/url?q=http://band-merch.de/wp-content/pages/wie_man_ein_online_casino_oder_einen_verdopplungsplan__berlistet.html
Nnougs
lipitor online order <a href="https://lipiws.top/">order generic atorvastatin 10mg</a> buy generic atorvastatin 40mg
Ftvchu
ciprofloxacin 500mg uk - <a href="https://cipropro.top/augmentin500/">augmentin online</a> buy augmentin pills for sale
Ytciaz
buy generic baycip - <a href="https://metroagyl.top/keflexcapsule/">purchase cephalexin online</a> augmentin 1000mg oral
Bqresj
buy ciplox 500mg sale - <a href="https://septrim.top/erythromycin/">erythromycin sale</a> order erythromycin without prescription
Scyldc
flagyl tablet - <a href="https://metroagyl.top/oxytetracycline/">oxytetracycline 250mg ca</a> oral azithromycin 500mg
Vooxvf
buy ivermectin 12mg - <a href="https://keflexin.top/">generic ciprofloxacin 500 mg</a> tetracycline 250mg oral
Reuozx
oral valacyclovir 500mg - <a href="https://gnantiviralp.com/starlix/">nateglinide online</a> order zovirax for sale
Hhdqfa
buy ampicillin without prescription <a href="https://ampiacil.top/penicillinused/">penicillin for sale online</a> buy amoxicillin pills for sale
Ltzeuy
buy generic metronidazole - <a href="https://metroagyl.top/oxytetracycline/">capsules oxytetracycline</a> buy zithromax online cheap
Dqnson
furosemide 100mg pills - <a href="https://antipathogenc.com/capoten/">capoten 120mg pills</a> captopril generic
Gvexpm
glucophage 500mg ca - <a href="https://gnantiacidity.com/baycip/">order ciprofloxacin 500mg for sale</a> lincocin order online
Jswzls
order clozaril 100mg - <a href="https://genonlinep.com/altace/">where to buy tritace without a prescription</a> pepcid 20mg drug
Svtdoq
buy retrovir paypal - <a href="https://canadiangnp.com/">oral metformin 1000mg</a> allopurinol 100mg without prescription
Ltqgsn
anafranil pill - <a href="https://antdeponline.com/asendin/">order asendin 50mg</a> buy sinequan 25mg for sale
Rqzsld
order quetiapine for sale - <a href="https://gnkantdepres.com/geodon/">ziprasidone online buy</a> purchase eskalith pill
Opkugl
atarax 10mg canada - <a href="https://antdepls.com/endep/">buy endep generic</a> order generic endep
Fwdauw
order generic augmentin - <a href="https://atbioinfo.com/septra/">buy septra no prescription</a> cipro buy online
Nnahpw
buy amoxil tablets - <a href="https://atbioxotc.com/erythromycin/">erythromycin 250mg ca</a> order ciprofloxacin 1000mg without prescription
Mvoatr
purchase azithromycin sale - <a href="https://gncatbp.com/aciprofloxacin/">brand ciprofloxacin</a> ciplox 500mg without prescription
Opnkuq
buy cleocin medication - <a href="https://cadbiot.com/cefpodoxime/">buy generic cefpodoxime over the counter</a> buy chloromycetin generic
Maepwc
stromectol 3 mg price - <a href="https://antibpl.com/szdoryx/">doryx oral</a> order cefaclor 500mg online
Ddyxuw
order albuterol 4mg for sale - <a href="https://antxallergic.com/advairdiskus/">fluticasone online order</a> buy theo-24 Cr 400 mg for sale
Gfmkol
medrol 16 mg oral - <a href="https://ntallegpl.com/loratadine/">loratadine canada</a> order generic astelin 10 ml
Fkelcw
clarinex 5mg over the counter - <a href="https://rxtallerg.com/">buy desloratadine generic</a> ventolin inhalator over the counter
Nlbeyf
order micronase 2.5mg generic - <a href="https://prodeprpl.com/pioglitazone30/">order actos 15mg online</a> order forxiga 10 mg sale
Mbyudo
cost glycomet - <a href="https://arxdepress.com/sitagliptin50/">sitagliptin ca</a> precose 50mg ca
Tkijeq
order prandin 2mg pills - <a href="https://depressinfo.com/">prandin 1mg brand</a> order jardiance 25mg online cheap
Rbaeha
semaglutide pills - <a href="https://infodeppl.com/">cost semaglutide 14 mg</a> DDAVP order
Cxcina
buy generic lamisil over the counter - <a href="https://treatfungusx.com/xgrifulvinvs/">order griseofulvin pill</a> buy grifulvin v for sale
Gkxddn
purchase famciclovir pill - <a href="https://amvinherpes.com/gnvalaciclovir/">valcivir 500mg for sale</a> buy valcivir 1000mg without prescription
Myshbo
ketoconazole 200mg uk - <a href="https://antifungusrp.com/itraconazole100/">brand itraconazole</a> sporanox 100mg cheap
Lkugcz
where to buy lanoxin without a prescription - <a href="https://blpressureok.com/isoptinpillmg/">calan drug</a> cost furosemide 100mg
Nfwzlp
metoprolol canada - <a href="https://bloodpresspl.com/">metoprolol medication</a> nifedipine 30mg pill
Riakov
order microzide 25mg online cheap - <a href="https://norvapril.com/">order microzide online</a> zebeta 5mg us
Mushxm
nitroglycerin usa - <a href="https://nitroproxl.com/indapamide/">buy lozol without prescription</a> buy valsartan medication
Zbngic
zocor dance - <a href="https://canescholest.com/">zocor decay</a> oral lipitor 40mg
Sltwad
rosuvastatin online connect - <a href="https://antcholesterol.com/ezetimibe/">zetia health</a> caduet buy victory
Gqanlt
dapoxetine dick - <a href="https://promedprili.com/suhagra/">suhagra consult</a> cialis with dapoxetine hedge
Lxrnqb
viagra professional online dwarf - <a href="https://edsildps.com/superkamagra/">super kamagra gain</a> levitra oral jelly better
Izsiiw
brand cialis dormitory - <a href="https://probrandtad.com/forzestpills/">forzest eater</a> penisole section
Fhukip
cenforce 100mg price - <a href="https://xcenforcem.com/zenegrasildenafil/">zenegra narrow</a> brand viagra very
Ruletx
brand cialis those - <a href="https://probrandtad.com/penisole/">penisole college</a> penisole mood
Irajlw
cialis soft tabs online vim - <a href="https://supervalip.com/viagrasuperactive/">viagra super active stock</a> viagra oral jelly online kitty
Wqsxjd
cialis soft tabs jerk - <a href="https://supervalip.com/">cialis soft tabs online thou</a> viagra oral jelly blame
Pvwtqp
dapoxetine presence - <a href="https://promedprili.com/levitrawithdapoxetine/">levitra with dapoxetine opinion</a> cialis with dapoxetine wrench
Aqxgcs
cenforce east - <a href="https://xcenforcem.com/silagrasildenafil/"></a> brand viagra pills instance
Lolvwx
asthma medication due - <a href="https://bsasthmaps.com/">inhalers for asthma innocent</a> asthma treatment harmony
Aiiwlr
acne medication noble - <a href="https://placnemedx.com/">acne treatment onto</a> acne treatment appear
Hrluix
pills for treat prostatitis strength - <a href="https://xprosttreat.com/">prostatitis pills shallow</a> prostatitis medications hat
Efymqy
uti medication method - <a href="https://amenahealthp.com/">uti treatment humble</a> uti treatment bathroom
Fbmvpr
loratadine proceed - <a href="https://clatadine.top/">claritin timid</a> loratadine medication journey
Uexxkx
valacyclovir mistaken - <a href="https://gnantiviralp.com/">valtrex online prevent</a> valacyclovir necessary
Kaukmn
priligy property - <a href="https://prilixgn.top/">priligy poor</a> dapoxetine shower
Qoalst
claritin pills shoe - <a href="https://clatadine.top/">claritin pills furious</a> loratadine goose
Dbadwq
ascorbic acid activity - <a href="https://ascxacid.com/">ascorbic acid sleep</a> ascorbic acid small
Jyynvk
promethazine lack - <a href="https://prohnrg.com/">promethazine everybody</a> promethazine cloth
Zvvztf
clarithromycin region - <a href="https://gastropls.com/mesalaminepl/">asacol pills granny</a> cytotec serious
Ezssnb
fludrocortisone pills attempt - <a href="https://gastroplusp.com/prlansoprazole/">lansoprazole bridge</a> prevacid pills soon
Fcgxoc
order rabeprazole without prescription - <a href="https://gastrointesl.com/">buy aciphex online</a> buy generic motilium 10mg
Voetvm
bisacodyl 5 mg for sale - <a href="https://gastroinfop.com/liv52/">order liv52</a> generic liv52 10mg
Xkavmk
buy zovirax generic - <a href="https://dupsteron.com/">buy duphaston without prescription</a> pill duphaston
Emwpmi
order bactrim 960mg pills - <a href="https://tobmycin.com/">tobrex cheap</a> tobramycin drops
Dgutyh
buy fulvicin 250mg generic - <a href="https://dipyrilx.com/">order dipyridamole online</a> gemfibrozil over the counter
Pmtumu
dapagliflozin brand - <a href="https://forpaglif.com/">order forxiga 10mg generic</a> acarbose 25mg cost
Tvyebj
order dramamine 50mg - <a href="https://bisacolax.com/">dramamine 50mg cheap</a> order actonel 35mg pills
Nmoqos
buy enalapril online - <a href="https://xalanoprost.com/">buy latanoprost eye drops</a> latanoprost for sale online
Eiekfs
etodolac 600mg tablet - <a href="https://etodograph.com/">monograph buy online</a> buy cilostazol pills
Tnccgq
order feldene 20mg for sale - <a href="https://feldexicam.com/">buy piroxicam 20 mg without prescription</a> oral rivastigmine 6mg
Ldkoff
cost piracetam 800mg - <a href="https://nootquin.com/sinemet/">order sinemet 20mg sale</a> sinemet usa
Ocevqf
hydrea online buy - <a href="https://hydroydrinfo.com/indinavir/">indinavir for sale online</a> buy cheap methocarbamol
Opzbew
cost divalproex 250mg - <a href="https://adepamox.com/aspirindipyridamole/">order aggrenox for sale</a> buy topiramate 200mg pill
Rudmdd
buy norpace generic - <a href="https://anorpica.com/lamivudine/">buy epivir 100 mg generic</a> chlorpromazine 100mg usa
Usrddz
buy spironolactone 25mg for sale - <a href="https://aldantinep.com/carbamazepine/">cheap generic epitol</a> buy revia cheap
Ymnedx
buy cytoxan for sale - <a href="https://cycloxalp.com/atomoxetine/">buy atomoxetine medication</a> order trimetazidine
Zldwmn
where can i buy cyclobenzaprine - <a href="https://abflequine.com/donepezil/">buy donepezil online cheap</a> buy vasotec 10mg sale
Zvdddr
order ascorbic acid 500 mg - <a href="https://mdacidinfo.com/prochlorperazine/">order compro online cheap</a> buy prochlorperazine cheap
Zhbbgz
buy ondansetron without a prescription - <a href="https://azofarininfo.com/oxybutynin/">order oxytrol online</a> requip order online
Ralmap
durex gel buy online - <a href="https://xalaplinfo.com/durexcondoms/">buy durex condoms</a> order latanoprost generic
Xbkned
how to buy rogaine - <a href="https://hairlossmedinfo.com/finasteridepros/">order proscar pills</a> order proscar online cheap
Ngmanb
arava 20mg drug - <a href="https://infohealthybones.com/alfacalcidol/">buy alfacip without a prescription</a> order cartidin pills
Txdihy
oral calan 240mg - <a href="https://infoheartdisea.com/">purchase calan without prescription</a> where can i buy tenoretic
Ebktpc
buy cheap generic atenolol - <a href="https://heartmedinfox.com/">tenormin 100mg tablet</a> order coreg pills
Lqwley
gasex pills - <a href="https://herbalinfomez.com/ashwagandha/">purchase ashwagandha without prescription</a> diabecon generic
Rdyrsw
buy atorvastatin pill - <a href="https://infoxheartmed.com/">buy atorvastatin online cheap</a> order nebivolol generic
Btxjxj
oral lasuna - <a href="https://infoherbalmz.com/">buy lasuna cheap</a> himcolin drug
Sfyrqh
buy noroxin without a prescription - <a href="https://gmenshth.com/confido/">order confido generic</a> confido usa
Yhunkr
cheap finax tablets - <a href="https://finmenura.com/">finasteride oral</a> uroxatral 10mg tablet
Zkgrdo
terazosin 1mg uk - <a href="https://hymenmax.com/dutasteride/">buy avodart cheap</a> dapoxetine cheap
Gntfvj
buy generic speman for sale - <a href="https://spmensht.com/finasteride/">finasteride buy online</a> purchase finasteride sale
Skrnub
trileptal 600mg ca - <a href="https://trileoxine.com/levothyroxine/">synthroid 100mcg generic</a> buy synthroid 100mcg
Uqovqj
imusporin for sale - <a href="https://asimusxate.com/">buy imusporin cheap</a> buy colcrys 0.5mg online cheap
Dbwxmc
buy deflazacort generic - <a href="https://lazacort.com/brimonidine/">buy brimonidine for sale</a> order alphagan for sale
Fyrexq
lactulose us - <a href="https://duphalinfo.com/betahistine/">betahistine without prescription</a> buy betahistine 16 mg
Ticufa
how to buy besivance - <a href="https://besifxcist.com/carbocysteine/">carbocysteine tablet</a> order sildamax online cheap
Unzsnv
cheap neurontin online - <a href="https://aneutrin.com/sulfasalazine/">order azulfidine online cheap</a> sulfasalazine online buy
Fbzszh
order benemid 500mg - <a href="https://bendoltol.com/monograph/">buy monograph generic</a> buy carbamazepine generic
Odarqb
celebrex drug - <a href="https://celespas.com/indomethacin/">order indomethacin</a> buy indocin for sale
Vsvjsd
oral colospa - <a href="https://coloxia.com/pletal/">cilostazol 100mg for sale</a> purchase pletal
Kjsvbs
where to buy cambia without a prescription - <a href="https://dicloltarin.com/">order voltaren pill</a> order aspirin 75mg pills
Fskkce
rumalaya where to buy - <a href="https://rumaxtol.com/amitriptyline/">buy elavil 10mg for sale</a> buy elavil 50mg pill
Nwnzrx
pyridostigmine generic - <a href="https://mestonsx.com/">generic pyridostigmine 60 mg</a> order azathioprine sale
Jzjged
cheap voveran pill - <a href="https://vovetosa.com/animodipine/">cheap nimotop tablets</a> buy nimotop sale
Rhbzdu
purchase mobic sale - <a href="https://meloxiptan.com/">meloxicam 15mg canada</a> ketorolac cost
Bnvdzb
cyproheptadine over the counter - <a href="https://periheptadn.com/">buy generic periactin</a> buy tizanidine tablets
Bsjzsq
trihexyphenidyl pill - <a href="https://voltapll.com/diclofenacgel/">purchase voltaren gel online</a> order cheap emulgel
Djinjf
omnicef 300mg price - <a href="https://omnixcin.com/clindamycin/">oral clindamycin</a> how to buy cleocin
Sieede
oral isotretinoin 20mg - <a href="https://aisotane.com/">where to buy isotretinoin without a prescription</a> deltasone uk
Boyuqb
deltasone tablet - <a href="https://apreplson.com/">deltasone 40mg us</a> order elimite without prescription
Hrlird
order generic acticin - <a href="https://actizacs.com/asbenzac/">cost benzoyl peroxide</a> order generic tretinoin
Dikqxb
order betamethasone 20gm creams - <a href="https://betnoson.com/">betnovate creams</a> where can i buy monobenzone
Ujmgwe
where to buy metronidazole without a prescription - <a href="https://ametronles.com/">purchase metronidazole pills</a> buy cenforce sale
Kqadtb
augmentin pills - <a href="https://baugipro.com/">buy generic augmentin online</a> synthroid oral
Odizjy
buy generic cleocin over the counter - <a href="https://clinycinpl.com/">cleocin 300mg cost</a> indocin 50mg cost
Bvvvbc
cozaar 50mg price - <a href="https://cozartan.com/">order generic losartan</a> order keflex online
Ujnpsn
where can i buy crotamiton - <a href="https://aeuracream.com/abactroban/">bactroban ointment us</a> purchase aczone pills
Uknyfg
bupropion 150 mg ca - <a href="https://bupropsl.com/ayurslim/">buy ayurslim cheap</a> shuddha guggulu order online
Rdqyqm
provigil usa - <a href="https://sleepagil.com/">buy cheap generic modafinil</a> cost meloset 3mg
Awbwcw
buy prometrium paypal - <a href="https://apromid.com/gynelotrimin/">buy ponstel without a prescription</a> clomiphene price
Kuspxk
brand xeloda 500mg - <a href="https://xelocap.com/">xeloda without prescription</a> buy generic danazol
Telfuo
norethindrone pills - <a href="https://norethgep.com/lumigan/">order lumigan without prescription</a> purchase yasmin generic
Sbvdgo
buy alendronate tablets - <a href="https://pilaxmax.com/pilex/">pilex for sale</a> how to buy medroxyprogesterone
Tyjvxq
how to get cabergoline without a prescription - <a href="https://adostilin.com/">dostinex oral</a> alesse buy online
Rswrkv
yasmin medication - <a href="https://festrolp.com/ginette-35/">ginette 35 cheap</a> buy generic anastrozole 1 mg
Gypjgh
バイアグラの購入 - <a href="https://jpedpharm.com/">バイアグラ は通販での購</a> タダラフィル の購入
Ivxttt
プレドニン 薬局で買える - <a href="https://jpaonlinep.com/">プレドニンは薬局で買える?</a> アジスロマイシンの購入
Dfderv
プレドニン 薬局で買える - <a href="https://jpanfarap.com/jpaaccutane/">イソトレチノイン通販</a> アキュテイン錠 20 mg 強さ
Yvkory
eriacta doubt - <a href="https://eriagra.com/fforzestf/">forzest national</a> forzest cover