docker安装-创建-打包-使用

1、docker安装(环境:阿里云服务器)

apt update
apt install docker

2、docker镜像制作

第一步:环境依赖

方法一:
pip freeze > requirements.txt
方法二:
pip install pipreqs
pipreqs . --encoding=utf8

第二步:创建一个Dockerfile文件(当前路径下有:demo、Dockerfile、requirements.txt)

第三步:填写文件配置(demo为项目名)

FROM python:3.6
ENV PATH /usr/local/bin:$PATH
ADD ./demo /code
WORKDIR /code
RUN pip install -i https://pypi.doubanio.com/simple/ -r requirements.txt
CMD [ "python", "./manage.py", "runserver", "0.0.0.0:3389"]

第四步:创建镜像

docker build -t myproject .  (myproject镜像名,全部小写)
docker build -t myproject:v1 .

第五步:创建容器,导入镜像(参数:外部端口:内部端口、容器:镜像)

//d参数:不加d启动直接进入docker容器里面了
//--rm参数:结束删除容器
docker run -itd --name Voc Image /bin/bash
//-p参数:端口映射 
docker run -itd -p 8888:3389 --name voc img /bin/bash
//如果dockerfile中的启动命令配置正确,且容器中的程序能正常运行,直接通过cmd启动了
docker run -itd -p 8888:3389 --name voc img
//参数/bin/bash:半自动,需要进入docker容器启动程序,不加就自动启动

第六步:容器启动启动不了,需要手动进入容器配置环境依赖

//换源(当前路径下的sourse.list去替换docker里面的)
docker cp ./sourse.list voc:/etc/apt/
apt -update
//会卡壳怎么办?
cd /usr/lib/apt/methods
ln -s http https
apt update
//ImportError: libgomp.so.1: cannot open shared object file: No such file or directory
apt install libgomp1
//ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory
apt install libglib2.0-dev
//ImportError: libSM.so.6: cannot open shared object file: No such file or directory
apt install libsm6
apt install libxrender1
apt install libxext-dev
//ImportError: libGL.so.1: cannot open shared object file: No such file or directory
apt install libgl1-mesa-glx                        (失败了就  apt-update)

sources.list

deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

第七步:docker容器打包和加载

//打包容器   save是打包镜像----load
docker export 2535dce630ad> hqh.tar
//加载打包好的容器,变成一个镜像
docker import hqh.tar newhqh(重新加载docker)
//删除镜像和容器
docker rmi ID  (删除镜像)
docker rm -f ID  (删除容器)
//查看容器
docker ps   (查看启动的容器)
docker ps -a   (查看所有容器)
//查看镜像
docker images
//结束和启动容器
docker stop ID  (再次启动容器)
docker start ID  (再次启动容器)

第八步:docker中常用的命令

//压缩一个目录:(压缩当前目录下的NER文件夹)
tar czvf version1.tar NER
zip -r version1.zip NER 
//空间使用情况
df -lh
du -h --max-depth=1
//打印docker日志
docker logs (dockerid)
//不支持中文运行错误
PYTHONIOENCODING=UTF-8 python manage.py runserver 0.0.0.0:8888
//进入容器(出容器会结束容器,直接关连接界面)
docker attach (dockerid)
//进入容器(出容器不会结束容器)
docker exec -it (dockerid) /bin/bash
//容器中安装一些环境,直接pip过慢
pip install -i https://pypi.doubanio.com/simple/ hanlp
//安装jdk环境,永久激活
JAVA_HOME=/code/jdk/java-se-8u41-ri
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH
//安装支持中文环境
apt-get install  language-pack-zh-han*
export LANGUAGE=zh_CN.UTF-8
export LANG=zh_CN.UTF-8
export LC_ALL=zh_CN.UTF-8
都需要在这里配置,就是永久的了
vim /etc/bash.bashrc
source /etc/bash.bashrc   (不行的话,就是su,进入管理员权限)

第九步:总结

1、docker build后能直接运行
第一步:制作镜像
第二步:全自动创建容器
第三步:save和load直接打包镜像即可
2、docker build后,不能启动,需要进容器,配置
第一步:制作镜像
第二步:半自动创建容器
第三步:进入容器,配置环境依赖
第四步:export和import打包容器   //只有容器才具备运行环境
第五步:保存容器,加载容器成镜像,再将镜像导入容器(半自动),完美运行,缺点是只能进入容器内部运行程序。
3、不想保存容器,如何使情况2转换成情况1
再次制作镜像,以保存的容器镜像为基础镜像,build后的镜像就具备运行环境了。

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值