Docker 部署Django项目的方法示例

2025-05-27 0 50

使用docker部署django项目也很简单,挺不错,分享下

环境

默认你已安装好docker环境

django项目大概结构

?

1

2

3

4

5

6

7

8

9

10
(p3s) [root@opsweb]# tree opsweb

opsweb

├── apps

├── logs

├── manage.py

├── media

├── opsweb

├── README.md

├── requirements.txt

└── static

编写Dockerfile

这里指定 Python 版本为docker官方提供的

?

1

2

3

4

5

6

7

8

9
"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速度快些 使用阿里云源

?

1

2

3

4
[global]

index-url = http://mirrors.aliyun.com/pypi/simple/

[install]

trusted-host=mirrors.aliyun.com

完整目录结构

?

1

2
[root@opsweb]# ls

Dockerfile opsweb pip.conf run_web.sh

build镜像

?

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

结语

这样镜像就build好了,然后你可以直接docker run -p8000:8000 ops:v0.1 -d启动就行,或者也可以部署到k8s上去也很简单这里就不细讲了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:https://www.cnblogs.com/guigujun/p/9926437.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 行业资讯 Docker 部署Django项目的方法示例 https://www.kuaiidc.com/68238.html

相关文章

猜你喜欢
发表评论
暂无评论