docker安装-创建-打包-使⽤1、docker安装(环境:阿⾥云服务器)
apt update
apt install docker
2、docker镜像制作
第⼀步:环境依赖
⽅法⼀:
pip freeze >
⽅法⼆:
pip install pipreqs
pipreqs . --encoding=utf8
第⼆步:创建⼀个Dockerfile⽂件(当前路径下有:demo、Dockerfile、)
第三步:填写⽂件配置(demo为项⽬名)
FROM python:3.6
ENV PATH /usr/local/bin:$PATH
ADD ./demo /code
WORKDIR /code
RUN pip install -i pypi.doubanio/simple/ -
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进入容器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 mirrors.aliyun/ubuntu/ xenial main restricted universe multiverse
deb mirrors.aliyun/ubuntu/ xenial-security main restricted universe multiverse
deb mirrors.aliyun/ubuntu/ xenial-updates main restricted universe multiverse
deb mirrors.aliyun/ubuntu/ xenial-proposed main restricted universe multiverse
deb mirrors.aliyun/ubuntu/ xenial-backports main restricted universe multiverse
deb-src mirrors.aliyun/ubuntu/ xenial main restricted universe multiverse
deb-src mirrors.aliyun/ubuntu/ xenial-security main restricted universe multiverse
deb-src mirrors.aliyun/ubuntu/ xenial-updates main restricted universe multiverse
deb-src mirrors.aliyun/ubuntu/ xenial-proposed main restricted universe multiverse
deb-src mirrors.aliyun/ubuntu/ xenial-backports main restricted universe multiverse
deb mirrors.tuna.tsinghua.edu/ubuntu/ xenial main restricted universe multiverse
deb mirrors.tuna.tsinghua.edu/ubuntu/ xenial-updates main restricted universe multiverse
deb mirrors.tuna.tsinghua.edu/ubuntu/ xenial-backports main restricted universe multiverse deb mirrors.tuna.tsinghua.edu/ubuntu/ xenial-security main restricted universe multiverse
deb mirrors.tuna.tsinghua.edu/ubuntu/ bionic main restricted universe multiverse
deb-src mirrors.tuna.tsinghua.edu/ubuntu/ bionic main restricted universe multiverse
deb mirrors.tuna.tsinghua.edu/ubuntu/ bionic-updates main restricted universe multiverse
deb-src mirrors.tuna.tsinghua.edu/ubuntu/ bionic-updates main restricted universe multiverse deb mirrors.tuna.tsinghua.edu/ubuntu/ bionic-backports main restricted universe multiverse deb-src mirrors.tuna.tsinghua.edu/ubuntu/ bionic-backports main restricted universe multiverse deb mirrors.tuna.tsinghua.edu/ubuntu/ bionic-security main restricted universe multiverse
deb-src mirrors.tuna.tsinghua.edu/ubuntu/ bionic-security main restricted universe multiverse deb mirrors.tuna.tsinghua.edu/ubuntu/ bionic-proposed main restricted universe multiverse deb-src mirrors.tuna.tsinghua.edu/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 pypi.doubanio/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后的镜像就具备运⾏环境了。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论