Python【算法中心 04】Docker镜像制作的两种方式代码内置与代码挂载(部署简单和避免修改Docker内文件的权衡)

文章介绍了在Docker中部署Django项目时,两种不同的代码处理方式:代码内置和代码挂载。代码内置简化部署但修改不便,而代码挂载允许外部修改代码便于调试。文中详细阐述了如何制作Docker镜像,保存和加载镜像,以及如何通过挂载卷来实现代码的动态更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.原因说明

代码内置的方式部署时只有一个镜像,不需要额外的文件,但是如果出现代码问题,修改就比较麻烦了,所以需要进行代码的外挂。这里还是以Django项目Docker的部署举例。

2.代码内置

2.1 镜像制作

在这里插入图片描述

Docker 的 Python Official Image 使用指南

FROM python:3
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
COPY . .
EXPOSE 8086
CMD python manage.py runserver 0.0.0.0:8086

使用更小的运行环境python:3.7-slim-stretch98MB

FROM python:3.7-slim-stretch
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
COPY . .
EXPOSE 8086
CMD python manage.py runserver 0.0.0.0:8086
# 1.构建镜像 -f ./DockerFile
docker build -t algorithm .

2.2 镜像的保存

# 1.查看当前镜像
docker images
[root@tcloud ~]# docker images
REPOSITORY   TAG                IMAGE ID       CREATED         SIZE
algorithm    latest             c54e5d681dd3   14 hours ago    230MB
python       3.7-slim-stretch   80b07211911e   19 months ago   98MB

# 2.导出指定版本的镜像
docker save algorithm:latest -o algorithm.tar
[root@tcloud ~]# docker save algorithm:latest -o algorithm.tar

# 3.查看保存的镜像文件
[root@tcloud ~]# ll
total 237196
-rw------- 1 root root 242887680 Mar  9 08:30 algorithm.tar

2.2 镜像的使用

# 1.停止所有容器并删除容器和镜像
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)

# 2.验证
[root@tcloud ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

# 3.镜像使用
docker load -i algorithm.tar
[root@tcloud ~]# docker load -i algorithm.tar
c0a294e617df: Loading layer [==================================================>]  58.51MB/58.51MB
62856a9a6856: Loading layer [==================================================>]  6.814MB/6.814MB
454d5f8832aa: Loading layer [==================================================>]  27.58MB/27.58MB
67b6ca5ec6cb: Loading layer [==================================================>]  4.608kB/4.608kB
0e3d2a7a5b02: Loading layer [==================================================>]   10.1MB/10.1MB
38961ae8e0f1: Loading layer [==================================================>]  2.048kB/2.048kB
a0d122052f56: Loading layer [==================================================>]   2.56kB/2.56kB
983c81d925e2: Loading layer [==================================================>]  139.8MB/139.8MB
Loaded image: algorithm:latest

# 4.镜像验证
[root@tcloud ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
algorithm    latest    c54e5d681dd3   14 hours ago   230MB

# 5.启动测试
docker run -di --name ac -p 8086:8086 algorithm

# 6.查看运行日志
docker logs -f --tail=100 ac

3.代码挂载

Dockerfile文件内容,这里少了COPY . .,镜像内的启动命令修改为CMD python /home/ac/manage.py runserver 0.0.0.0:8086不将文件放到镜像内。

FROM python:3.7-slim-stretch
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
EXPOSE 8086
# 执行的文件是在外边的(需要镜像挂载)
CMD python /home/ac/manage.py runserver 0.0.0.0:8086

代码文件放置:

在这里插入图片描述

镜像的制作和代码内置是一样的,这里不再赘述,只有运行镜像时是不同的,添加了-v进行文件的挂载:

docker run -di -v /home/algorithm:/home/ac --name ac -p 8086:8086 algorithmcenter

4.总结

两种方式没有好坏之分,方式一适合稳定的代码不需要临时调整,此时部署简单,方式二适合需要进行调试的代码,修改方便。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yuanzhengme.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值