1
1
FROM alpine:3.7
2
2
3
- ENV ALPINE_VERSION=3.7
3
+ # VERSIONS
4
+ ENV ALPINE_VERSION=3.7 \
5
+ PYTHON_VERSION=2.7.14
4
6
5
- # Install needed packages. Notes:
7
+ # PATHS
8
+ ENV PYTHON_PATH=/usr/lib/python$PYTHON_VERSION \
9
+ PATH="/usr/lib/python$PYTHON_VERSION/bin/:${PATH}"
10
+
11
+ # PACKAGES
6
12
# * dumb-init: a proper init system for containers, to reap zombie children
7
13
# * musl: standard C library
8
14
# * lib6-compat: compatibility libraries for glibc
@@ -11,49 +17,72 @@ ENV ALPINE_VERSION=3.7
11
17
# * bash: so we can access /bin/bash
12
18
# * git: to ease up clones of repos
13
19
# * ca-certificates: for SSL verification during Pip and easy_install
14
- # * python: the binaries themselves
15
- # * python-dev: are used for gevent e.g.
16
- # * py-setuptools: required only in major version 2, installs easy_install so we can install Pip.
17
20
ENV PACKAGES="\
18
- dumb-init \
19
- musl \
20
- libc6-compat \
21
- linux-headers \
22
- build-base \
23
- bash \
24
- git \
25
- ca-certificates \
26
- python2 \
27
- python2-dev \
28
- py-setuptools \
21
+ dumb-init \
22
+ musl \
23
+ libc6-compat \
24
+ linux-headers \
25
+ build-base \
26
+ bash \
27
+ git \
28
+ ca-certificates \
29
+ "
30
+
31
+ # PACKAGES needed to built python
32
+ ENV PYTHON_BUILD_PACKAGES="\
33
+ readline-dev \
34
+ zlib-dev \
35
+ bzip2-dev \
36
+ sqlite-dev \
37
+ libressl-dev \
29
38
"
30
39
31
- RUN echo \
32
- # replacing default repositories with edge ones
33
- && echo "http://dl-cdn.alpinelinux.org/alpine/v$ALPINE_VERSION/testing" > /etc/apk/repositories \
34
- && echo "http://dl-cdn.alpinelinux.org/alpine/v$ALPINE_VERSION/community" >> /etc/apk/repositories \
35
- && echo "http://dl-cdn.alpinelinux.org/alpine/v$ALPINE_VERSION/main" >> /etc/apk/repositories \
36
-
37
- # Add the packages, with a CDN-breakage fallback if needed
38
- && apk add --no-cache $PACKAGES || \
39
- (sed -i -e 's/dl-cdn/dl-4/g' /etc/apk/repositories && apk add --no-cache $PACKAGES) \
40
-
41
- # turn back the clock -- so hacky!
42
- && echo "http://dl-cdn.alpinelinux.org/alpine/v$ALPINE_VERSION/main/" > /etc/apk/repositories \
43
- # && echo "@community http://dl-cdn.alpinelinux.org/alpine/v$ALPINE_VERSION/community" >> /etc/apk/repositories \
44
- # && echo "@testing http://dl-cdn.alpinelinux.org/alpine/v$ALPINE_VERSION/testing" >> /etc/apk/repositories \
45
- # && echo "@edge-main http://dl-cdn.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories \
46
-
47
- # make some useful symlinks that are expected to exist
48
- && if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python2.7 /usr/bin/python; fi \
49
- && if [[ ! -e /usr/bin/python-config ]]; then ln -sf /usr/bin/python2.7-config /usr/bin/python-config; fi \
50
- && if [[ ! -e /usr/bin/easy_install ]]; then ln -sf /usr/bin/easy_install-2.7 /usr/bin/easy_install; fi \
51
-
52
- # Install and upgrade Pip
53
- && easy_install pip \
54
- && pip install --upgrade pip \
55
- && if [[ ! -e /usr/bin/pip ]]; then ln -sf /usr/bin/pip2.7 /usr/bin/pip; fi \
56
- && echo
40
+ RUN set -ex ;\
41
+ # find MAJOR and MINOR python versions based on $PYTHON_VERSION
42
+ export PYTHON_MAJOR_VERSION=$(echo "${PYTHON_VERSION}" | rev | cut -d"." -f3- | rev) ;\
43
+ export PYTHON_MINOR_VERSION=$(echo "${PYTHON_VERSION}" | rev | cut -d"." -f2- | rev) ;\
44
+ # replacing default repositories with edge ones
45
+ echo "http://dl-cdn.alpinelinux.org/alpine/v$ALPINE_VERSION/community" >> /etc/apk/repositories ;\
46
+ echo "http://dl-cdn.alpinelinux.org/alpine/v$ALPINE_VERSION/main" >> /etc/apk/repositories ;\
47
+
48
+ # Add the packages, with a CDN-breakage fallback if needed
49
+ apk add --no-cache $PACKAGES || \
50
+ (sed -i -e 's/dl-cdn/dl-4/g' /etc/apk/repositories && apk add --no-cache $PACKAGES) ;\
51
+ # Add packages just for the python build process with a CDN-breakage fallback if needed
52
+ apk add --no-cache --virtual .build-deps $PYTHON_BUILD_PACKAGES || \
53
+ (sed -i -e 's/dl-cdn/dl-4/g' /etc/apk/repositories && apk add --no-cache --virtual .build-deps $PYTHON_BUILD_PACKAGES) ;\
54
+
55
+ # turn back the clock -- so hacky!
56
+ echo "http://dl-cdn.alpinelinux.org/alpine/v$ALPINE_VERSION/main/" > /etc/apk/repositories ;\
57
+ # echo "@community http://dl-cdn.alpinelinux.org/alpine/v$ALPINE_VERSION/community" >> /etc/apk/repositories ;\
58
+ # echo "@testing http://dl-cdn.alpinelinux.org/alpine/v$ALPINE_VERSION/testing" >> /etc/apk/repositories ;\
59
+ # echo "@edge-main http://dl-cdn.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories ;\
60
+
61
+ # use pyenv to download and compile specific python version
62
+ git clone --depth 1 https://github.com/pyenv/pyenv /usr/lib/pyenv ;\
63
+ PYENV_ROOT=/usr/lib/pyenv /usr/lib/pyenv/bin/pyenv install $PYTHON_VERSION ;\
64
+ # move specific version to correct path delete pyenv, no longer needed
65
+ mv /usr/lib/pyenv/versions/$PYTHON_VERSION/ $PYTHON_PATH ;\
66
+ rm -rfv /usr/lib/pyenv ;\
67
+ # change the path on the header of every file from PYENV_ROOT to PYTHON_PATH
68
+ cd $PYTHON_PATH/bin/ && sed -i "s+/usr/lib/pyenv/versions/$PYTHON_VERSION/+$PYTHON_PATH/+g" * ;\
69
+ # delete binary "duplicates" and replace them with symlinks
70
+ # this also optimizes space since they are actually the same binary
71
+ rm -f $PYTHON_PATH/bin/python$PYTHON_MAJOR_VERSION \
72
+ $PYTHON_PATH/bin/python$PYTHON_MINOR_VERSION \
73
+ $PYTHON_PATH/bin/python$PYTHON_MAJOR_VERSION-config \
74
+ $PYTHON_PATH/bin/python$PYTHON_MINOR_VERSION-config ;\
75
+ ln -sf $PYTHON_PATH/bin/python $PYTHON_PATH/bin/python$PYTHON_MAJOR_VERSION ;\
76
+ ln -sf $PYTHON_PATH/bin/python $PYTHON_PATH/bin/python$PYTHON_MINOR_VERSION ;\
77
+ ln -sf $PYTHON_PATH/bin/python-config $PYTHON_PATH/bin/python$PYTHON_MAJOR_VERSION-config ;\
78
+ ln -sf $PYTHON_PATH/bin/python-config $PYTHON_PATH/bin/python$PYTHON_MINOR_VERSION-config ;\
79
+ # delete files to to reduce container size
80
+ # tips taken from main python docker repo
81
+ find /usr/lib/python$PYTHON_VERSION -depth \( -name '*.pyo' -o -name '*.pyc' -o -name 'test' -o -name 'tests' \) -exec rm -rf '{}' + ;\
82
+
83
+ # remove build dependencies and any leftover apk cache
84
+ apk del --no-cache --purge .build-deps ;\
85
+ rm -rf /var/cache/apk/*
57
86
58
87
# since we will be "always" mounting the volume, we can set this up
59
88
ENTRYPOINT ["/usr/bin/dumb-init" ]
0 commit comments