Docker 安装 Python
查找Docker Hub上的python镜像
docker search python
leedeMacBook-Air:~ li$ docker search python
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
python Python is an interpreted, interactive, objec… 3498 [OK]
django Django is a free web application framework, … 751 [OK]
pypy PyPy is a fast, compliant alternative implem… 152 [OK]
kaggle/python Docker image for Python scripts run on Kaggle 104 [OK]
frolvlad/alpine-python3 The smallest Docker image with Python 3.5 (~… 83 [OK]
centos/python-35-centos7 Platform for building and running Python 3.5… 32
resin/raspberrypi3-python The Python buildpack image for Python apps f… 30
拉取官方的镜像,
选择拉取的镜像标签为2.7
docker pull python:2.7
查看镜像
等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为python,标签为2.7的镜像。
下面的例子拉取了3个标签
leedeMacBook-Air:~ li$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python latest 2cc378c061f7 10 days ago 923MB
python 2.7 3c43a5d4034a 2 weeks ago 908MB
python 3.5 1419e5e87f7c 2 weeks ago 917MB
使用Python环境
在~/myapp目录下创建一个 version.py 文件,代码如下:
#!/usr/bin/python import sys; print(sys.version);
运行容器
docker run
runoob@runoob:~/python$ docker run -v $PWD/myapp:/usr/src/myapp -w /usr/src/myapp python:3.5 python version.py
命令说明:
-v $PWD/myapp:/usr/src/myapp :将主机中当前目录下的myapp挂载到容器的/usr/src/myapp
-w /usr/src/myapp :指定容器的/usr/src/myapp目录为工作目录
python version.py :使用容器的python命令来执行工作目录中的version.py文件
输出结果:
2.7.10 (v2.7.10:15c95b7d81dc, May 23 2015, 09:33:12)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]