Skip to content

Instantly share code, notes, and snippets.

@dhermes
Last active January 19, 2018 21:06
Show Gist options
  • Save dhermes/b91971ab73c2c8be770ceca38bd5a015 to your computer and use it in GitHub Desktop.
Save dhermes/b91971ab73c2c8be770ceca38bd5a015 to your computer and use it in GitHub Desktop.

To build the image:

$ docker build \
>   --file get-pip.Dockerfile \
>   --tag dhermes/repro-4352:latest \
>   .

After building and running the shell, the import succeeds:

$ docker run \
>   --rm \
>   --tty \
>   --interactive \
>   dhermes/repro-4352:latest \
>   /bin/bash
root@4fddb8425b8e:/projects# python
Python 2.7.6 (default, Nov  7 2017, 06:37:46)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import google.protobuf
>>> google.protobuf
<module 'google.protobuf' from '/usr/local/lib/python2.7/site-packages/google/protobuf/__init__.pyc'>
root@3ff8fc23f38f:/projects#
root@3ff8fc23f38f:/projects#
root@3ff8fc23f38f:/projects#
root@3ff8fc23f38f:/projects# pip freeze
cachetools==2.0.1
certifi==2017.11.5
chardet==3.0.4
google-api-python-client==1.4.2
google-apitools==0.4.11
google-auth==1.2.0
google-auth-httplib2==0.0.2
google-cloud-core==0.24.1
google-cloud-storage==1.1.1
google-resumable-media==0.0.2
googleapis-common-protos==1.5.3
httplib2==0.10.3
idna==2.6
oauth2client==4.1.2
protobuf==3.4.0
protorpc==0.12.0
pyasn1==0.3.7
pyasn1-modules==0.1.5
requests==2.18.4
rsa==3.4.2
six==1.11.0
uritemplate==3.0.0
urllib3==1.22

To build the other image, which uses requirements.txt and installs pip via apt-get:

$ docker build \
>   --file apt-get.Dockerfile \
>   --tag zafields/repro-4352:latest \
>   .

After building, running:

$ docker run \
>   --rm \
>   --tty \
>   --interactive \
>   zafields/repro-4352:latest \
>   /bin/bash
# Filename: apt-get.Dockerfile
# Base Image
FROM ubuntu:14.04
# Build Arguments
ARG PYTHON_VERSION=2.7.6
ARG PYTHON_MD5=bcf93efa8eaf383c98ed3ce40b763497
# Set execution environment
COPY ./requirements.txt /tmp/requirements.txt
ENV PYTHONPATH=/usr/local/lib/python2.7/dist-packages:$PYTHONPATH
WORKDIR /projects/
# Install Python Dependencies
RUN ["/bin/dash","-c","\
apt-get update \
&& DEBIAN_FRONTEND=noninteractive \
apt-get install --no-install-recommends -y \
ca-certificates \
curl \
g++ \
libssl-dev \
make \
python-pip \
xz-utils \
&& rm -rf /var/lib/apt/lists/* \
"]
# Install Python
# NOTE: Proper format of checksum validation `<md5sum_checksum><space><space><file_name>`
RUN ["/bin/dash","-c","\
cd /projects/ \
&& curl -sSL \"https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tar.xz\" -o ./python.tar.xz \
&& echo \"$PYTHON_MD5 ./python.tar.xz\" | md5sum -c - \
&& tar -xf python.tar.xz --xz \
&& rm python.tar.xz \
&& cd /projects/Python-$PYTHON_VERSION \
&& ./configure --enable-unicode=ucs4 --prefix=/usr/local/ \
&& make \
&& make install \
&& cd /projects/ \
&& rm -rf Python-$PYTHON_VERSION \
"]
# Install PIP dependencies
RUN ["/bin/bash","-c","\
pip install pip --upgrade \
&& pip install -r /tmp/requirements.txt \
"]
# Finalize environment
RUN ["/bin/bash", "-c","\
echo 'Defaults env_keep += \"PYTHONPATH\"' >> /etc/sudoers \
# && touch /usr/local/lib/python2.7/dist-packages/google/__init__.py \
"]
# Filename: get-pip.Dockerfile
# Base Image
FROM ubuntu:14.04
# Build Arguments
ARG PYTHON_VERSION=2.7.6
ARG PYTHON_MD5=bcf93efa8eaf383c98ed3ce40b763497
# Set execution environment
ENV PYTHONPATH=/usr/local/lib/python2.7/dist-packages:$PYTHONPATH
WORKDIR /projects/
# Install Python Dependencies
RUN ["/bin/dash","-c","\
apt-get update \
&& DEBIAN_FRONTEND=noninteractive \
apt-get install --no-install-recommends -y \
ca-certificates \
curl \
g++ \
libssl-dev \
make \
xz-utils \
wget \
&& rm -rf /var/lib/apt/lists/* \
"]
# Install Python
# NOTE: Proper format of checksum validation `<md5sum_checksum><space><space><file_name>`
RUN ["/bin/dash","-c","\
cd /projects/ \
&& curl -sSL \"https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tar.xz\" -o ./python.tar.xz \
&& echo \"$PYTHON_MD5 ./python.tar.xz\" | md5sum -c - \
&& tar -xf python.tar.xz --xz \
&& rm python.tar.xz \
&& cd /projects/Python-$PYTHON_VERSION \
&& ./configure --enable-unicode=ucs4 --prefix=/usr/local/ \
&& make \
&& make install \
&& cd /projects/ \
&& rm -rf Python-$PYTHON_VERSION \
"]
# Install pip.
# If the environment variable is called "PIP_VERSION", pip explodes with
# "ValueError: invalid truth value '<VERSION>'"
ENV PYTHON_PIP_VERSION 9.0.1
RUN wget --no-check-certificate -O /tmp/get-pip.py 'https://bootstrap.pypa.io/get-pip.py' \
&& python2.7 /tmp/get-pip.py "pip==$PYTHON_PIP_VERSION" \
&& rm /tmp/get-pip.py \
# we use "--force-reinstall" for the case where the version of pip we're trying to install is the same as the version bundled with Python
# ("Requirement already up-to-date: pip==8.1.2 in /usr/local/lib/python3.6/site-packages")
# https://github.com/docker-library/python/pull/143#issuecomment-241032683
&& python2.7 -m pip install --no-cache-dir --upgrade --force-reinstall "pip==$PYTHON_PIP_VERSION" \
# then we use "pip list" to ensure we don't have more than one pip version installed
# https://github.com/docker-library/python/pull/100
&& [ "$(python2.7 -m pip list |tac|tac| awk -F '[ ()]+' '$1 == "pip" { print $2; exit }')" = "$PYTHON_PIP_VERSION" ]
# Install PIP dependencies
RUN ["/bin/bash","-c","\
python -m pip install pip --upgrade \
&& python -m pip install \
google-apitools==0.4.11 \
google-api-python-client==1.4.2 \
google-cloud-storage==1.1.1 \
"]
# Finalize environment
RUN ["/bin/bash"]
google-apitools==0.4.11
google-api-python-client==1.4.2
google-cloud-storage==1.1.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment