1、python 整理环境
# -*- encoding: utf-8 -*-
# !/usr/bin/env python
'''
@File : test.py
@Time : 2022/9/19 9:14
@Author : Fan LR
@Version : 1.0
@Contact : 1243124253@qq.com
@License : (C)Copyright 2021-2022
@Desc :
'''
# here put the import lib
import os, sys
# 找到当前目录
project_root = os.path.dirname(os.path.realpath(__file__))
print(project_root)
# 找到解释器,虚拟环境目录
python_root = sys.exec_prefix
print(python_root)
# 拼接生成requirements命令
command = python_root + '\Scripts\pip freeze > ' + project_root + '\\requirements.txt'
print(command)
# 执行命令。
os.system(command)
ps:注意规范一下 requirements.txt看能不能用
2、构建环境
FROM pytorch/pytorch:1.8.0-cuda11.1-cudnn8-devel
#作者名
MAINTAINER Fan LR 1243124253@qq.com
# 设置 python 环境变量
ENV PYTHONUNBUFFERED 1
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
ENV LANGUAGE C.UTF-8
# 创建 code 文件夹并将其设置为工作目录
RUN mkdir /code
WORKDIR /code
# 更新 pip
RUN pip install -i https://pypi.mirrors.ustc.edu.cn/simple pip -U
# 设置pip源
RUN pip config set global.index-url https://pypi.mirrors.ustc.edu.cn/simple
# 将 requirements.txt 复制到容器的 code 目录
ADD requirements.txt /code/
# 安装库
RUN pip install -r requirements.txt
# 运行服务
CMD nohup python src/api.py &> run.log & /bin/sh -c "tail -f 50 run.log"
3、开始build
docker build -t pytorch1.8:v1 -f ./Dockerfile .
- -t 镜像名
- -f dockerfile 路径
– 最后面加个点
4、打包(另存为)新镜像
另存为:
docker commit id images_name
导出:
docker save -o 文件名 镜像名
docker save -o pytorch1.8.tar 1cf96f49742d
5、加载
docker load -i 文件名 镜像名
6、运行
docker run -it -p 端口映射 -v 数据挂载 --name 镜像名
docker run -it -p 7671:7671 -v E:\code2022\court_ana:/code --name court_anav1.0 1cf96f49742d