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-stretch
仅98MB
。
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.总结
两种方式没有好坏之分,方式一适合稳定的代码不需要临时调整,此时部署简单,方式二适合需要进行调试的代码,修改方便。