Docker镜像中若要安装Python2.7和PIP,需要使用Dockerfile文件来进行安装,Dockerfile 是一个用来构建镜像的文本文件。本文主要介绍Docker 在Linux(Ubuntu或CentOS) 上安装 Python2.7 PIP的Dockerfile文件。

1、Dockerfile文件

参考资料:Docker 构建自定义镜像和Dockerfile文件

2、CentOS7 安装 Python 2.7.5 和 PIP的Dockerfile文件

Dockerfile文件内容如下:

ARG CENTOS_VERSION=7
FROM centos:$CENTOS_VERSION

# Python 2.7.5 is installed with centos7 image
# Add repository for PIP
RUN yum install -y epel-release

# Install pip
RUN yum install -y python-pip

RUN python --version

ENTRYPOINT [ "python" ]

3、Ubuntu 安装 Python 2.7.5 的 Dockerfile文件

Dockerfile文件内容如下:

ARG UBUNTU_VERSION=18.04
FROM ubuntu:$UBUNTU_VERSION

ARG PYTHON_VERSION=2.7.5

# Install dependencies
# PIP - openssl version > 1.1 may be an issue (try older ubuntu images)
RUN apt-get update \
  && apt-get install -y wget gcc make openssl libffi-dev libgdbm-dev libsqlite3-dev libssl-dev zlib1g-dev \
  && apt-get clean

WORKDIR /tmp/

# Build Python from source
RUN wget https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz \
  && tar --extract -f Python-$PYTHON_VERSION.tgz \
  && cd ./Python-$PYTHON_VERSION/ \
  && ./configure --enable-optimizations --prefix=/usr/local \
  && make && make install \
  && cd ../ \
  && rm -r ./Python-$PYTHON_VERSION*

RUN python --version

ENTRYPOINT [ "python" ]

或者

ARG UBUNTU_VERSION=16.04
FROM ubuntu:$UBUNTU_VERSION


# Install dependencies
RUN apt-get update \
  && apt-get install -y wget gcc make openssl libffi-dev libgdbm-dev libsqlite3-dev libssl-dev zlib1g-dev \
  && apt-get clean

WORKDIR /tmp/

# Build python from source
RUN wget https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz \
  && tar --extract -f Python-$PYTHON_VERSION.tgz \
  && cd ./Python-$PYTHON_VERSION/ \
  && ./configure --enable-optimizations --prefix=/usr/local \
  && make && make install \
  && cd ../ \
  && rm -r ./Python-$PYTHON_VERSION*

# Build pip from source
RUN wget https://bootstrap.pypa.io/pip/2.7/get-pip.py \
    && python get-pip.py

RUN python --version

ENTRYPOINT [ "python" ]

4、Ubuntu 安装 Python 2.7.6 和 PIP 的 Dockerfile文件

Dockerfile文件内容如下:

ARG UBUNTU_VERSION=14.04
FROM ubuntu:$UBUNTU_VERSION

RUN apt-get update \
  && apt-get install -y python python-pip \
  && apt-get clean

RUN python --version

ENTRYPOINT [ "python" ]

5、Ubuntu 安装 Python 2.7.9 和 PIP 的 Dockerfile文件

Dockerfile文件内容如下:

ARG UBUNTU_VERSION=16.04
FROM ubuntu:$UBUNTU_VERSION

ARG PYTHON_VERSION=2.7.9

# Install dependencies
RUN apt-get update \
  && apt-get install -y wget gcc make openssl libffi-dev libgdbm-dev libsqlite3-dev libssl-dev zlib1g-dev \
  && apt-get clean

WORKDIR /tmp/

# Build Python from source
RUN wget https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz \
  && tar --extract -f Python-$PYTHON_VERSION.tgz \
  && cd ./Python-$PYTHON_VERSION/ \
  && ./configure --with-ensurepip=install --enable-optimizations --prefix=/usr/local \
  && make && make install \
  && cd ../ \
  && rm -r ./Python-$PYTHON_VERSION*

RUN python --version \
  && pip --version

ENTRYPOINT [ "python" ]

推荐文档