English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Docker 安装 Python

Python-Image installieren

Methode 1、docker pull python:3.5

Suchen Docker Hub darauf befindliche Python-Images:

Man kann andere Versionen von python über Sort by anzeigen, Standard ist die neueste Version python:lastest

Außerdem können wir den Befehl docker search python verwenden, um verfügbare Versionen anzuzeigen:

w3codebox@w3codebox:~/python$ docker search python
NAME                           DESCRIPTION                      STARS     OFFICIAL   AUTOMATED
python  Python ist ein interpretiertes,...       982       [OK]       
kaggle/python  Docker-Image für Python...         33                   [OK]
azukiapp/python  Docker-Image, um Python ...     3                    [OK]
vimagick/python  Mini Python                                  2          [OK]
tsuru/python  Image für den Python ...           2                    [OK]
pandada8/alpine-python  Ein alpinbasierter Python-Image                 1          [OK]
1science/python  Python-Docker-Images basierend auf ...  1                    [OK]
lucidfrontier45/python-uwsgi  Python mit uWSGI                  1                    [OK]
orbweb/python  Python-Image                       1                    [OK]
pathwar/python  Python-Template für Pathwar-Ebenen 1                    [OK]
rounds/10m-python  Python, setuptools und pip.  0  [OK]
ruimashita/python  ubuntu 14.04 python  0  [OK]
tnanba/python  Python on CentOS-7 image.  0  [OK]

Hier ziehen wir das offizielle Image, das mit dem TAG3.5

w3codebox@w3codebox:~/python docker pull python:3.5

Nachdem der Download abgeschlossen ist, können wir den REPOSITORY mit python und das TAG in der Liste der lokalen Images finden 3.5 der Abbildung.

w3codebox@w3codebox:~/python$ docker images python:3.5 
REPOSITORY                               TAG                                                               IMAGE ID                                                                       CREATED                                                                       SIZE
python              3.5              045767ddf24a        9 days ago          684.1 MB

Methode zwei, durch Dockerfile aufbauen

Erstellen Sie Dockerfile</p>

Zunächst erstellen Sie das Verzeichnis python, um die späteren related Dinge zu speichern.

w3codebox@w3codebox:~$ mkdir -p ~/python ~/python/myapp

Das Verzeichnis myapp wird auf den Anwendungsdirektor des python-Containers gemappt.

Gehen Sie in das erstellte python-Verzeichnis und erstellen Sie einen Dockerfile.

FROM buildpack-deps:jessie
# Entfernen mehrerer Spuren von debian python
RUN apt-get purge -y python.*
# http://bugs.python.org/Problem19846
# > Zum Zeitpunkt der Einstellung "LANG=C" auf einem Linux-System *grundsätzlich Python unterbricht 3*, und das ist nicht in Ordnung.
ENV LANG C.UTF-8
# gpg: Schlüssel F73C700D: öffentlicher Schlüssel "Larry Hastings <[email protected]>" importiert
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 -- --verify python.tar.xz.asc python.tar.xz \",
        && rm -r \
        && mkdir -p /usr/src/python \\
        && tar -xJC /usr/src/python --strip-1 -
        && rm python.tar.xz \",
        \\
        && cd /usr/src/python \\
        && \./configure --enable-shared --enable-unicode=ucs4 \\
         -
        
        && ldconfig \",
        && pip3 install --no-cache-dir --upgrade ---installed pip==$PYTHON_PIP_VERSION \",
         /usr/local -
                \( \",
                    \( -type d -a - -o -
                    -o \",
                    \( -type f -a -*.pyc' -o -*.pyo \) \",
                \) -exec rm -rf \'{}' + \\
        && rm -rf /usr/src/python ~/.cache
# Erstellen Sie einige nützliche Symlinks, die erwartet werden:
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"]

Erstellen Sie ein Abbild durch Dockerfile und ersetzen Sie den Namen durch Ihren eigenen:

w3codebox@w3codebox:~/python$ docker build -t python:3.5 .

Erstellt, können wir das soeben erstellte Abbild im lokalen Abbildungsverzeichnis finden:

w3codebox@w3codebox:~/python$ docker images python:3.5 
REPOSITORY                               TAG                                                               IMAGE ID                                                                       CREATED                                                                       SIZE
python              3.5              045767ddf24a        9 days ago          684.1 MB

使用 python 镜像

在 ~/python/在 myapp 目录下创建一个 helloworld.py 文件,代码如下:

#!/usr/bin/python
print("Hello, World!");

运行容器

w3codebox@w3codebox:~/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!