溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用docker部署Django項(xiàng)目的方法

發(fā)布時(shí)間:2020-09-02 11:23:16 來源:億速云 閱讀:389 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)使用docker部署Django項(xiàng)目的方法的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過來看看吧。

使用docker部署django項(xiàng)目也很簡單,挺不錯(cuò),分享下

環(huán)境

默認(rèn)你已安裝好docker環(huán)境

django項(xiàng)目大概結(jié)構(gòu)

(p3s) [root@opsweb]# tree opsweb
opsweb
├── apps
├── logs
├── manage.py
├── media
├── opsweb
├── README.md
├── requirements.txt
└── static

編寫Dockerfile

這里指定 Python 版本為docker官方提供的

"0.0.0.0:8000" 這里筆者開啟容器中 8000 端口

FROM python:3.6
RUN mkdir -p /usr/src/app
COPY pip.conf /root/.pip/pip.conf
COPY opsweb /usr/src/app/
COPY run_web.sh /usr/src/app/
RUN pip install -r /usr/src/app/requirements.txt
WORKDIR /usr/src/app
CMD [ "sh", "./run_web.sh"]

編寫pip文件

這里是為了使用鏡像 pip install速度快些 使用阿里云源

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com

完整目錄結(jié)構(gòu)

[root@opsweb]# ls
Dockerfile  opsweb   pip.conf run_web.sh

build鏡像

docker build -t ops:v0.1 .
Sending build context to Docker daemon 4.849 MB
Step 1 : FROM python:3.6
 ---> 3e4c2972dc8d
Step 2 : RUN mkdir -p /usr/src/app
 ---> Running in 8ddd929f5c18
 ---> 89dc0a1ffdf0
Removing intermediate container 8ddd929f5c18
Step 3 : COPY pip.conf /root/.pip/pip.conf
 ---> 5a4a165fed90
Removing intermediate container 37f3fdc7e5d2
Step 4 : COPY opsweb /usr/src/app/opsweb
 ---> f602e72ffd4c
Removing intermediate container 8d4bb616916d
Step 5 : COPY opsweb/requirements.txt /usr/src/app/
 ---> 6fe11a6fcbe0
Removing intermediate container faeadee32fed
Step 6 : RUN pip install -r /usr/src/app/requirements.txt
 ---> cc09c17d53da
Removing intermediate container d7b45bec6993
Step 7 : WORKDIR /usr/src/app
 ---> Running in c22dfdddbe81
 ---> c5c944b6df45
Removing intermediate container c22dfdddbe81
Step 8 : CMD python ./manage.py runserver 0.0.0.0:8000
 ---> Running in 29d5f0f53f6e
 ---> 10d37173fd13
Removing intermediate container 29d5f0f53f6e
Successfully built 10d37173fd13

結(jié)語

這樣鏡像就build好了,然后你可以直接docker run -p8000:8000 ops:v0.1 -d啟動(dòng)就行,或者也可以部署到k8s上去 也很簡單 。

感謝各位的閱讀!關(guān)于使用docker部署Django項(xiàng)目的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI