创建dockerfile
首先,创建目录python,用于存放后面的相关东西。
w3cschool@w3cschool:~$ mdkir -p ~/python ~/python/myapp
myapp目录将映射为python容器配置的应用目录
进入创建的python目录,创建dockerfile
from buildpack-deps:jessie # remove several traces of debian python run apt-get purge -y python.* # http://bugs.python.org/issue19846 # > at the moment, setting "lang=c" on a linux system *fundamentally breaks python 3*, and that's not ok. env lang c.utf-8 # gpg: key f73c700d: public key "larry hastings <larry@hastings.org>" imported env gpg_key 97fc712e4c024bbea48a61ed3a5ca953f73c700d env python_version 3.5.1 # if this is called "pip_version", pip explodes with "valueerror: invalid truth value '<version>'" env python_pip_version 8.1.2 run set -ex \ && curl -fsl "https://www.python.org/ftp/python/${python_version%%[a-z]*}/python-$python_version.tar.xz" -o python.tar.xz \ && curl -fsl "https://www.python.org/ftp/python/${python_version%%[a-z]*}/python-$python_version.tar.xz.asc" -o python.tar.xz.asc \ && export gnupghome="$(mktemp -d)" \ && gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$gpg_key" \ && gpg --batch --verify python.tar.xz.asc python.tar.xz \ && rm -r "$gnupghome" python.tar.xz.asc \ && mkdir -p /usr/src/python \ && tar -xjc /usr/src/python --strip-components=1 -f python.tar.xz \ && rm python.tar.xz \ \ && cd /usr/src/python \ && ./configure --enable-shared --enable-unicode=ucs4 \ && make -j$(nproc) \ && make install \ && ldconfig \ && pip3 install --no-cache-dir --upgrade --ignore-installed pip==$python_pip_version \ && find /usr/local -depth \ \( \ \( -type d -a -name test -o -name tests \) \ -o \ \( -type f -a -name '*.pyc' -o -name '*.pyo' \) \ \) -exec rm -rf '{}' + \ && rm -rf /usr/src/python ~/.cache # make some useful symlinks that are expected to exist run cd /usr/local/bin \ && ln -s easy_install-3.5 easy_install \ && ln -s idle3 idle \ && ln -s pydoc3 pydoc \ && ln -s python3 python \ && ln -s python3-config python-config cmd ["python3"]
通过dockerfile创建一个镜像,替换成你自己的名字
w3cschool@w3cschool:~/python$ docker build -t python:3.5 .
创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像
w3cschool@w3cschool:~/python$ docker images python:3.5 repository tag image id created size python 3.5 045767ddf24a 9 days ago 684.1 mb
查找docker hub上的python镜像
w3cschool@w3cschool:~/python$ docker search python name description stars official automated python python is an interpreted,... 982 [ok] kaggle/python docker image for python... 33 [ok] azukiapp/python docker image to run python ... 3 [ok] vimagick/python mini python 2 [ok] tsuru/python image for the python ... 2 [ok] pandada8/alpine-python an alpine based python image 1 [ok] 1science/python python docker images based on ... 1 [ok] lucidfrontier45/python-uwsgi python with uwsgi 1 [ok] orbweb/python python image 1 [ok] pathwar/python python template for pathwar levels 1 [ok] rounds/10m-python python, setuptools and pip. 0 [ok] ruimashita/python ubuntu 14.04 python 0 [ok] tnanba/python python on centos-7 image. 0 [ok]
这里我们拉取官方的镜像,标签为3.5
w3cschool@w3cschool:~/python$ docker pull python:3.5
等待下载完成后,我们就可以在本地镜像列表里查到repository为python,标签为3.5的镜像。
在~/python/myapp目录下创建一个 helloworld.py 文件,代码如下:
#!/usr/bin/python print("hello, world!");
w3cschool@w3cschool:~/python$ docker run -v $pwd/myapp:/usr/src/myapp -w /usr/src/myapp python:3.5 python helloworld.py
命令说明:
-v $pwd/myapp:/usr/src/myapp :将主机中当前目录下的myapp挂载到容器的/usr/src/myapp
-w /usr/src/myapp :指定容器的/usr/src/myapp目录为工作目录
python helloworld.py :使用容器的python命令来执行工作目录中的helloworld.py文件
输出结果:
hello, world!