Skip to content

Commit 5bfe66d

Browse files
committed
Add a few simple Python 3 templates based on existing Dockerfiles
1 parent 3f51e91 commit 5bfe66d

File tree

5 files changed

+325
-5
lines changed

5 files changed

+325
-5
lines changed

Dockerfile-alpine.template

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
FROM alpine:3.4
2+
3+
# ensure local python is used over alpine python
4+
ENV PATH /usr/local/bin:$PATH
5+
6+
# http://bugs.python.org/issue19846
7+
# > At the moment, setting "LANG=C" on a Linux system *fundamentally breaks Python 3*, and that's not OK.
8+
ENV LANG C.UTF-8
9+
10+
# install ca-certificates so that HTTPS works consistently
11+
# the other runtime dependencies for Python are installed later
12+
RUN apk add --no-cache ca-certificates
13+
14+
ENV GPG_KEY %%PLACEHOLDER%%
15+
ENV PYTHON_VERSION %%PLACEHOLDER%%
16+
17+
# if this is called "PIP_VERSION", pip explodes with "ValueError: invalid truth value '<VERSION>'"
18+
ENV PYTHON_PIP_VERSION %%PLACEHOLDER%%
19+
20+
RUN set -ex \
21+
&& apk add --no-cache --virtual .fetch-deps \
22+
gnupg \
23+
openssl \
24+
tar \
25+
xz \
26+
\
27+
&& wget -O python.tar.xz "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz" \
28+
&& wget -O python.tar.xz.asc "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz.asc" \
29+
&& export GNUPGHOME="$(mktemp -d)" \
30+
&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$GPG_KEY" \
31+
&& gpg --batch --verify python.tar.xz.asc python.tar.xz \
32+
&& rm -r "$GNUPGHOME" python.tar.xz.asc \
33+
&& mkdir -p /usr/src/python \
34+
&& tar -xJC /usr/src/python --strip-components=1 -f python.tar.xz \
35+
&& rm python.tar.xz \
36+
\
37+
&& apk add --no-cache --virtual .build-deps \
38+
bzip2-dev \
39+
gcc \
40+
libc-dev \
41+
linux-headers \
42+
make \
43+
ncurses-dev \
44+
openssl \
45+
openssl-dev \
46+
pax-utils \
47+
readline-dev \
48+
sqlite-dev \
49+
tcl-dev \
50+
tk \
51+
tk-dev \
52+
xz-dev \
53+
zlib-dev \
54+
# add build deps before removing fetch deps in case there's overlap
55+
&& apk del .fetch-deps \
56+
\
57+
&& cd /usr/src/python \
58+
&& ./configure \
59+
--enable-loadable-sqlite-extensions \
60+
--enable-shared \
61+
&& make -j$(getconf _NPROCESSORS_ONLN) \
62+
&& make install \
63+
\
64+
# explicit path to "pip3" to ensure Debian "pip3" cannot interfere
65+
&& if [ ! -e /usr/local/bin/pip3 ]; then : \
66+
&& wget -O /tmp/get-pip.py 'https://bootstrap.pypa.io/get-pip.py' \
67+
&& python3 /tmp/get-pip.py "pip==$PYTHON_PIP_VERSION" \
68+
&& rm /tmp/get-pip.py \
69+
; fi \
70+
&& pip3 install --no-cache-dir --upgrade "pip==$PYTHON_PIP_VERSION" \
71+
&& [ "$(pip list |tac|tac| awk -F '[ ()]+' '$1 == "pip" { print $2; exit }')" = "$PYTHON_PIP_VERSION" ] \
72+
\
73+
&& find /usr/local -depth \
74+
\( \
75+
\( -type d -a -name test -o -name tests \) \
76+
-o \
77+
\( -type f -a -name '*.pyc' -o -name '*.pyo' \) \
78+
\) -exec rm -rf '{}' + \
79+
&& runDeps="$( \
80+
scanelf --needed --nobanner --recursive /usr/local \
81+
| awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
82+
| sort -u \
83+
| xargs -r apk info --installed \
84+
| sort -u \
85+
)" \
86+
&& apk add --virtual .python-rundeps $runDeps \
87+
&& apk del .build-deps \
88+
&& rm -rf /usr/src/python ~/.cache
89+
90+
# make some useful symlinks that are expected to exist
91+
RUN cd /usr/local/bin \
92+
&& { [ -e easy_install ] || ln -s easy_install-* easy_install; } \
93+
&& ln -s idle3 idle \
94+
&& ln -s pydoc3 pydoc \
95+
&& ln -s python3 python \
96+
&& ln -s python3-config python-config
97+
98+
CMD ["python3"]

Dockerfile-debian.template

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
FROM buildpack-deps:jessie
2+
3+
# ensure local python is used over debian python
4+
ENV PATH /usr/local/bin:$PATH
5+
6+
# http://bugs.python.org/issue19846
7+
# > At the moment, setting "LANG=C" on a Linux system *fundamentally breaks Python 3*, and that's not OK.
8+
ENV LANG C.UTF-8
9+
10+
# runtime dependencies
11+
RUN apt-get update && apt-get install -y --no-install-recommends \
12+
tcl \
13+
tk \
14+
&& rm -rf /var/lib/apt/lists/*
15+
16+
ENV GPG_KEY %%PLACEHOLDER%%
17+
ENV PYTHON_VERSION %%PLACEHOLDER%%
18+
19+
# if this is called "PIP_VERSION", pip explodes with "ValueError: invalid truth value '<VERSION>'"
20+
ENV PYTHON_PIP_VERSION %%PLACEHOLDER%%
21+
22+
RUN set -ex \
23+
&& buildDeps=' \
24+
tcl-dev \
25+
tk-dev \
26+
' \
27+
&& apt-get update && apt-get install -y $buildDeps --no-install-recommends && rm -rf /var/lib/apt/lists/* \
28+
\
29+
&& wget -O python.tar.xz "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz" \
30+
&& wget -O python.tar.xz.asc "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz.asc" \
31+
&& export GNUPGHOME="$(mktemp -d)" \
32+
&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$GPG_KEY" \
33+
&& gpg --batch --verify python.tar.xz.asc python.tar.xz \
34+
&& rm -r "$GNUPGHOME" python.tar.xz.asc \
35+
&& mkdir -p /usr/src/python \
36+
&& tar -xJC /usr/src/python --strip-components=1 -f python.tar.xz \
37+
&& rm python.tar.xz \
38+
\
39+
&& cd /usr/src/python \
40+
&& ./configure \
41+
--enable-loadable-sqlite-extensions \
42+
--enable-shared \
43+
&& make -j$(nproc) \
44+
&& make install \
45+
&& ldconfig \
46+
\
47+
# explicit path to "pip3" to ensure Debian "pip3" cannot interfere
48+
&& if [ ! -e /usr/local/bin/pip3 ]; then : \
49+
&& wget -O /tmp/get-pip.py 'https://bootstrap.pypa.io/get-pip.py' \
50+
&& python3 /tmp/get-pip.py "pip==$PYTHON_PIP_VERSION" \
51+
&& rm /tmp/get-pip.py \
52+
; fi \
53+
&& pip3 install --no-cache-dir --upgrade "pip==$PYTHON_PIP_VERSION" \
54+
&& [ "$(pip list |tac|tac| awk -F '[ ()]+' '$1 == "pip" { print $2; exit }')" = "$PYTHON_PIP_VERSION" ] \
55+
\
56+
&& find /usr/local -depth \
57+
\( \
58+
\( -type d -a -name test -o -name tests \) \
59+
-o \
60+
\( -type f -a -name '*.pyc' -o -name '*.pyo' \) \
61+
\) -exec rm -rf '{}' + \
62+
&& apt-get purge -y --auto-remove $buildDeps \
63+
&& rm -rf /usr/src/python ~/.cache
64+
65+
# make some useful symlinks that are expected to exist
66+
RUN cd /usr/local/bin \
67+
&& { [ -e easy_install ] || ln -s easy_install-* easy_install; } \
68+
&& ln -s idle3 idle \
69+
&& ln -s pydoc3 pydoc \
70+
&& ln -s python3 python \
71+
&& ln -s python3-config python-config
72+
73+
CMD ["python3"]

Dockerfile-onbuild.template

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM python:%%PLACEHOLDER%%
2+
3+
RUN mkdir -p /usr/src/app
4+
WORKDIR /usr/src/app
5+
6+
ONBUILD COPY requirements.txt /usr/src/app/
7+
ONBUILD RUN pip install --no-cache-dir -r requirements.txt
8+
9+
ONBUILD COPY . /usr/src/app

Dockerfile-slim.template

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
FROM debian:jessie
2+
3+
# ensure local python is used over debian python
4+
ENV PATH /usr/local/bin:$PATH
5+
6+
# http://bugs.python.org/issue19846
7+
# > At the moment, setting "LANG=C" on a Linux system *fundamentally breaks Python 3*, and that's not OK.
8+
ENV LANG C.UTF-8
9+
10+
# runtime dependencies
11+
RUN apt-get update && apt-get install -y --no-install-recommends \
12+
ca-certificates \
13+
libsqlite3-0 \
14+
libssl1.0.0 \
15+
&& rm -rf /var/lib/apt/lists/*
16+
17+
ENV GPG_KEY %%PLACEHOLDER%%
18+
ENV PYTHON_VERSION %%PLACEHOLDER%%
19+
20+
# if this is called "PIP_VERSION", pip explodes with "ValueError: invalid truth value '<VERSION>'"
21+
ENV PYTHON_PIP_VERSION %%PLACEHOLDER%%
22+
23+
RUN set -ex \
24+
&& buildDeps=' \
25+
gcc \
26+
libbz2-dev \
27+
libc6-dev \
28+
liblzma-dev \
29+
libncurses-dev \
30+
libreadline-dev \
31+
libsqlite3-dev \
32+
libssl-dev \
33+
make \
34+
tcl-dev \
35+
tk-dev \
36+
wget \
37+
xz-utils \
38+
zlib1g-dev \
39+
' \
40+
&& apt-get update && apt-get install -y $buildDeps --no-install-recommends && rm -rf /var/lib/apt/lists/* \
41+
\
42+
&& wget -O python.tar.xz "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz" \
43+
&& wget -O python.tar.xz.asc "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz.asc" \
44+
&& export GNUPGHOME="$(mktemp -d)" \
45+
&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$GPG_KEY" \
46+
&& gpg --batch --verify python.tar.xz.asc python.tar.xz \
47+
&& rm -r "$GNUPGHOME" python.tar.xz.asc \
48+
&& mkdir -p /usr/src/python \
49+
&& tar -xJC /usr/src/python --strip-components=1 -f python.tar.xz \
50+
&& rm python.tar.xz \
51+
\
52+
&& cd /usr/src/python \
53+
&& ./configure \
54+
--enable-loadable-sqlite-extensions \
55+
--enable-shared \
56+
&& make -j$(nproc) \
57+
&& make install \
58+
&& ldconfig \
59+
\
60+
# explicit path to "pip3" to ensure Debian "pip3" cannot interfere
61+
&& if [ ! -e /usr/local/bin/pip3 ]; then : \
62+
&& wget -O /tmp/get-pip.py 'https://bootstrap.pypa.io/get-pip.py' \
63+
&& python3 /tmp/get-pip.py "pip==$PYTHON_PIP_VERSION" \
64+
&& rm /tmp/get-pip.py \
65+
; fi \
66+
&& pip3 install --no-cache-dir --upgrade "pip==$PYTHON_PIP_VERSION" \
67+
&& [ "$(pip list |tac|tac| awk -F '[ ()]+' '$1 == "pip" { print $2; exit }')" = "$PYTHON_PIP_VERSION" ] \
68+
\
69+
&& find /usr/local -depth \
70+
\( \
71+
\( -type d -a -name test -o -name tests \) \
72+
-o \
73+
\( -type f -a -name '*.pyc' -o -name '*.pyo' \) \
74+
\) -exec rm -rf '{}' + \
75+
&& apt-get purge -y --auto-remove $buildDeps \
76+
&& rm -rf /usr/src/python ~/.cache
77+
78+
# make some useful symlinks that are expected to exist
79+
RUN cd /usr/local/bin \
80+
&& { [ -e easy_install ] || ln -s easy_install-* easy_install; } \
81+
&& ln -s idle3 idle \
82+
&& ln -s pydoc3 pydoc \
83+
&& ln -s python3 python \
84+
&& ln -s python3-config python-config
85+
86+
CMD ["python3"]

update.sh

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
#!/bin/bash
22
set -e
33

4+
declare -A gpgKeys=(
5+
# gpg: key 18ADD4FF: public key "Benjamin Peterson <benjamin@python.org>" imported
6+
[2.7]='C01E1CAD5EA2C4F0B8E3571504C367C218ADD4FF'
7+
# https://www.python.org/dev/peps/pep-0373/#release-manager-and-crew
8+
9+
# gpg: key 36580288: public key "Georg Brandl (Python release signing key) <georg@python.org>" imported
10+
[3.3]='26DEA9D4613391EF3E25C9FF0A5B101836580288'
11+
# https://www.python.org/dev/peps/pep-0398/#release-manager-and-crew
12+
13+
# gpg: key F73C700D: public key "Larry Hastings <larry@hastings.org>" imported
14+
[3.4]='97FC712E4C024BBEA48A61ED3A5CA953F73C700D'
15+
# https://www.python.org/dev/peps/pep-0429/#release-manager-and-crew
16+
17+
# gpg: key F73C700D: public key "Larry Hastings <larry@hastings.org>" imported
18+
[3.5]='97FC712E4C024BBEA48A61ED3A5CA953F73C700D'
19+
# https://www.python.org/dev/peps/pep-0478/#release-manager-and-crew
20+
21+
# gpg: key AA65421D: public key "Ned Deily (Python release signing key) <nad@acm.org>" imported
22+
[3.6]='0D96DF4D4110E5C43FBFB17F2D347EA6AA65421D'
23+
# https://www.python.org/dev/peps/pep-0494/#release-manager-and-crew
24+
)
25+
426
cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
527

628
versions=( "$@" )
@@ -11,6 +33,17 @@ versions=( "${versions[@]%/}" )
1133

1234
pipVersion="$(curl -fsSL 'https://pypi.python.org/pypi/pip/json' | awk -F '"' '$2 == "version" { print $4 }')"
1335

36+
generated_warning() {
37+
cat <<-EOH
38+
#
39+
# NOTE: THIS DOCKERFILE IS GENERATED VIA "update.sh"
40+
#
41+
# PLEASE DO NOT EDIT IT DIRECTLY.
42+
#
43+
44+
EOH
45+
}
46+
1447
travisEnv=
1548
for version in "${versions[@]}"; do
1649
# <span class="release-number"><a href="/downloads/release/python-278/">Python 2.7.8</a></span>
@@ -26,13 +59,34 @@ for version in "${versions[@]}"; do
2659
echo
2760
} >&2
2861
else
62+
if [[ "$version" != 2.* ]]; then
63+
for variant in \
64+
debian \
65+
alpine \
66+
slim \
67+
onbuild \
68+
; do
69+
if [ "$variant" = 'debian' ]; then
70+
dir="$version"
71+
else
72+
dir="$version/$variant"
73+
fi
74+
template="Dockerfile-$variant.template"
75+
{ generated_warning; cat "$template"; } > "$dir/Dockerfile"
76+
done
77+
if [ -d "$version/wheezy" ]; then
78+
cp "$version/Dockerfile" "$version/wheezy/Dockerfile"
79+
sed -ri 's/:jessie/:wheezy/g' "$version/wheezy/Dockerfile"
80+
fi
81+
fi
2982
(
3083
set -x
31-
sed -ri '
32-
s/^(ENV PYTHON_VERSION) .*/\1 '"$fullVersion"'/;
33-
s/^(ENV PYTHON_PIP_VERSION) .*/\1 '"$pipVersion"'/;
34-
' "$version"/{,*/}Dockerfile
35-
sed -ri 's/^(FROM python):.*/\1:'"$version"'/' "$version/onbuild/Dockerfile"
84+
sed -ri \
85+
-e 's/^(ENV GPG_KEY) .*/\1 '"${gpgKeys[$version]}"'/' \
86+
-e 's/^(ENV PYTHON_VERSION) .*/\1 '"$fullVersion"'/' \
87+
-e 's/^(ENV PYTHON_PIP_VERSION) .*/\1 '"$pipVersion"'/' \
88+
-e 's/^(FROM python):.*/\1:'"$version"'/' \
89+
"$version"/{,*/}Dockerfile
3690
)
3791
fi
3892
for variant in wheezy alpine slim; do

0 commit comments

Comments
 (0)