1.准备python镜像
1)拉取python镜像
docker pull python:3.7.4
2)查看镜像,可以看到自己的镜像了
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest bd431ca8553c 11 days ago 667MB
mysql latest cbe8815cbea8 12 days ago 546MB
ubuntu latest 26b77e58432b 2 weeks ago 72.9MB
hello-world latest d1165f221234 6 weeks ago 13.3kB
centos latest 300e315adb2f 4 months ago 209MB
python 3.7.4 9fa56d0addae 18 months ago 918MB
2.运行容器
容器就是应用所在的系统了,这就是能够运行一个python应用程序的完整环境。在这个容器中利用pip安装依赖包,放入开发好的python程序,然后将这个容器导出为image,就能够四处迁移了。
1)运行容器
-it:-i和-t的结合,直接进入容器的命令行模式。–name:自定义容器名称,不用的话会自动分配一个名称。-v: 将本地文件夹~/PycharmProjects/text_similar与容器文件夹/root/text_similar共享。python:3.7.4:要运行的镜像名+TAGbash:进入容器命令行。
docker run -it --name pytest -v ~/PycharmProjects/text_similar:/root/text_similar python:3.7.4 bash
执行上面命令后,就像进入了一个新的系统了,这里的所有操作都是在当前docker容器中。当然如果用rm -rf删除共享文件夹里边内容,那本地电脑里边的也就真删了。
2)开始安装需要的python包
-i命令是指定pip拉取python包的位置。
pin install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
3)看一眼容器文件夹内容
4)创建一个文件夹
这个文件夹放要运行的python文件。容器打包成镜像,这个文件夹一起打包进去,就能作为执行文件直接使用了。
mkdir my_test_python
(5) 创建测试python程序
本地text_similar文件夹创建hello.py,由于共享,可以直接在docker容器复制到my_test_python文件夹下。
6)执行一下看看
里边只有一行代码,打印hello word。
7) exit 命令退出docker容器
3.容器打包成镜像
(1) 看一下容器信息
docker ps -a
2)把容器打包成镜像
用到容器ID,hello是镜像名称,v1是镜像TAG(版本号)
docker commit -a "eric" -m "my python test" 80cdd11f9b60 hello:v1
-a :提交的镜像作者;
-c :使用Dockerfile指令来创建镜像;
-m :提交时的说明文字;
-p :在commit时,将容器暂停。
3) 查看一下新的镜像
4)可以容器直接运行刚才hello.py,运行完退出
docker run 命令后边直接跟着python+容器内容要执行的程序文件。
5、docker上传image到docker hub
1)本地登录
docker login -u 用户名 -p 密码
2)docker hub网站创建镜像信息
4)本地tag关联
:本地镜像,例如hello:v1
:你的hub名字
[:]:你指定的docker hub的那个库的名字和版本号
docker tag <existing-image> <hub-user>/<repo-name>[:<tag>]
5)push上去
docker push <hub-user>/<repo-name>:<tag>