Skip to content

Commit

Permalink
Python runtime images share same structure as build image (#151)
Browse files Browse the repository at this point in the history
* Python runtime images share same structure as build image

* PR comments

* Update dockerfile tempalte
  • Loading branch information
gpcastro authored May 23, 2019
1 parent 513bf4f commit 676883f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
4 changes: 2 additions & 2 deletions images/build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ RUN apt-get update \
# https://github.com/docker-library/python/issues/147
ENV PYTHONIOENCODING UTF-8
COPY build/__python-versions.sh /tmp
COPY --from=mcr.microsoft.com/oryx/python-build-2.7:20190322.1 /opt /opt
COPY --from=mcr.microsoft.com/oryx/python-build-3.6:20190322.1 /opt /opt
COPY --from=mcr.microsoft.com/oryx/python-build-2.7:20190402.1 /opt /opt
COPY --from=mcr.microsoft.com/oryx/python-build-3.6:20190402.1 /opt /opt
COPY --from=mcr.microsoft.com/oryx/python-build-3.7:20190402.1 /opt /opt
RUN . /tmp/__python-versions.sh && set -ex \
&& [ -d "/opt/python/$PYTHON27_VERSION" ] && echo /opt/python/$PYTHON27_VERSION/lib >> /etc/ld.so.conf.d/python.conf \
Expand Down
20 changes: 19 additions & 1 deletion images/runtime/python/Dockerfile.template
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,25 @@ ENV BUILD_NUMBER=${BUILD_NUMBER}
# GOPATH is set to "/go" in the base image, so no need to set it again
RUN ./build.sh python /opt/startupcmdgen/startupcmdgen

FROM python:%PYTHON_BASE_IMAGE%
FROM buildpack-deps:stretch AS main

ENV PYTHON_VERSION %PYTHON_FULL_VERSION%

# Add python binaries from Oryx base image
COPY --from=mcr.microsoft.com/oryx/python-build-%PYTHON_VERSION%:%IMAGE_TAG% /opt /opt
RUN set -ex \
&& cd /opt/python/ \
&& ln -s %PYTHON_FULL_VERSION% %PYTHON_VERSION% \
&& ln -s %PYTHON_VERSION% %PYTHON_MAJOR_VERSION% \
&& echo /opt/python/%PYTHON_MAJOR_VERSION%/lib >> /etc/ld.so.conf.d/python.conf \
&& ldconfig \
&& if [ "%PYTHON_MAJOR_VERSION%" = "3" ]; then cd /opt/python/%PYTHON_MAJOR_VERSION%/bin \
&& ln -s idle3 idle \
&& ln -s pydoc3 pydoc \
&& ln -s python3-config python-config; fi

ENV PATH="/opt/python/%PYTHON_MAJOR_VERSION%/bin:${PATH}"

# Bake Application Insights key from pipeline variable into final image
ARG AI_KEY
ENV ORYX_AI_INSTRUMENTATION_KEY=${AI_KEY}
Expand Down
26 changes: 17 additions & 9 deletions images/runtime/python/generateDockerfiles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,32 @@ set -e

declare -r DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
declare -r PYTHON_VERSIONS_PATH=$DIR/../../../build/__python-versions.sh
declare -r VERSIONS_FILE="$DIR/pythonVersions.txt"
declare -r DOCKERFILE_TEMPLATE="$DIR/Dockerfile.template"
declare -r IMAGE_NAME_PLACEHOLDER="%PYTHON_BASE_IMAGE%"
declare -r IMAGE_SUFFIX="-slim-stretch"

# Python major version, e.g. '2', '3'
declare -r PYTHON_MAJOR_VERSION_PLACEHOLDER="%PYTHON_MAJOR_VERSION%"
# Python version as we usually refer to, e.g. '2.7', '3.6'
declare -r PYTHON_VERSION_PLACEHOLDER="%PYTHON_VERSION%"
# Python full version, including patch, e.g. '3.7.3'
declare -r PYTHON_FULL_VERSION_PLACEHOLDER="%PYTHON_FULL_VERSION%"
declare -r ORYX_IMAGE_TAG_PLACEHOLDER="%IMAGE_TAG%"
declare -r ORYX_BASE_IMAGE_TAG="20190402.1"

source "$PYTHON_VERSIONS_PATH"
while IFS= read -r PYTHON_VERSION_VAR_NAME || [[ -n $PYTHON_VERSION_VAR_NAME ]]
do
PYTHON_VERSION=${!PYTHON_VERSION_VAR_NAME}
PYTHON_IMAGE_NAME=$PYTHON_VERSION$IMAGE_SUFFIX
IFS='.' read -ra SPLIT_VERSION <<< "$PYTHON_VERSION"
VERSION_DIRECTORY="${SPLIT_VERSION[0]}.${SPLIT_VERSION[1]}"
echo "Generating Dockerfile for image '$PYTHON_IMAGE_NAME' in directory '$VERSION_DIRECTORY'..."
MAJOR_MINOR_VERSION="${SPLIT_VERSION[0]}.${SPLIT_VERSION[1]}"

mkdir -p "$DIR/$VERSION_DIRECTORY/"
TARGET_DOCKERFILE="$DIR/$VERSION_DIRECTORY/Dockerfile"
mkdir -p "$DIR/$MAJOR_MINOR_VERSION/"
TARGET_DOCKERFILE="$DIR/$MAJOR_MINOR_VERSION/Dockerfile"
cp "$DOCKERFILE_TEMPLATE" "$TARGET_DOCKERFILE"

# Replace placeholders
sed -i "s|$IMAGE_NAME_PLACEHOLDER|$PYTHON_IMAGE_NAME|g" "$TARGET_DOCKERFILE"
sed -i "s|$PYTHON_VERSION_PLACEHOLDER|$MAJOR_MINOR_VERSION|g" "$TARGET_DOCKERFILE"
sed -i "s|$PYTHON_FULL_VERSION_PLACEHOLDER|$PYTHON_VERSION|g" "$TARGET_DOCKERFILE"
sed -i "s|$PYTHON_MAJOR_VERSION_PLACEHOLDER|${SPLIT_VERSION[0]}|g" "$TARGET_DOCKERFILE"
sed -i "s|$ORYX_IMAGE_TAG_PLACEHOLDER|$ORYX_BASE_IMAGE_TAG|g" "$TARGET_DOCKERFILE"

done < <(compgen -A variable | grep 'PYTHON[0-9]\{2,\}_VERSION')
9 changes: 0 additions & 9 deletions images/runtime/python/install-dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,4 @@ EOL
pip install --upgrade pip
pip install gunicorn

# Make sure the paths from the build image are also present in the runtime, since pip might use the
# path hardcoded in some places.
# PYTHON_VERSION is defined in the base images.
mkdir -p /opt/python/$PYTHON_VERSION/
ln -s /usr/local/bin/ /opt/python/$PYTHON_VERSION/
ln -s /usr/local/include/ /opt/python/$PYTHON_VERSION/
ln -s /usr/local/lib/ /opt/python/$PYTHON_VERSION/
ln -s /usr/local/share/ /opt/python/$PYTHON_VERSION/

ln -s /opt/startupcmdgen/startupcmdgen /usr/local/bin/oryx

0 comments on commit 676883f

Please sign in to comment.