Skip to content

Commit 58f37e2

Browse files
committed
Optimize Dockerfile
Prioritize SDK and NDK downloads before everything because once downloaded we don't care about redownloading unless the version is changed and layer cache works this way more efficiently. Redirect Licenses to /dev/null, move more important layers up. Move '&&' operators to the front - Docker sometimes, (personally I've encountered it only with apt and old coreutils), is very critical of the amount of spaces *after* the operator but does not care what's in front of it that much.
1 parent 5d4c974 commit 58f37e2

File tree

1 file changed

+83
-47
lines changed

1 file changed

+83
-47
lines changed

Dockerfile

Lines changed: 83 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,118 @@
11
# Dockerfile with:
22
# - Android build environment
33
# - python-for-android dependencies
4+
#
45
# Build with:
5-
# docker build --tag=p4a .
6+
# docker build --tag=p4a .
7+
#
68
# Run with:
7-
# docker run p4a /bin/sh -c '. venv/bin/activate && p4a apk --help'
9+
# docker run -it --rm p4a /bin/sh -c '. venv/bin/activate && p4a apk --help'
10+
#
811
# Or for interactive shell:
9-
# docker run -it --rm p4a
12+
# docker run -it --rm p4a
13+
#
14+
# Note:
15+
# Use 'docker run' without '--rm' flag for keeping the container and use
16+
# 'docker commit <container hash> <new image>' to extend the original image
17+
1018
FROM ubuntu:18.04
1119

20+
ENV ANDROID_HOME="/opt/android"
21+
22+
23+
ENV ANDROID_NDK_HOME="${ANDROID_HOME}/android-ndk"
24+
ENV ANDROID_NDK_HOME_V="${ANDROID_NDK_HOME}-r${ANDROID_NDK_VERSION}"
1225

13-
ENV USER="user"
14-
ENV HOME_DIR="/home/${USER}"
15-
ENV WORK_DIR="${HOME_DIR}" \
16-
PATH="${HOME_DIR}/.local/bin:${PATH}"
1726
# get the latest version from https://developer.android.com/ndk/downloads/index.html
1827
ENV ANDROID_NDK_VERSION="16b"
19-
# get the latest version from https://developer.android.com/studio/index.html
20-
ENV ANDROID_SDK_TOOLS_VERSION="3859397"
21-
22-
ENV ANDROID_HOME="/opt/android"
23-
ENV ANDROID_NDK_HOME="${ANDROID_HOME}/android-ndk" \
24-
ANDROID_SDK_HOME="${ANDROID_HOME}/android-sdk"
25-
ENV ANDROID_NDK_HOME_V="${ANDROID_NDK_HOME}-r${ANDROID_NDK_VERSION}" \
26-
ENV ANDROID_NDK_ARCHIVE="android-ndk-r${ANDROID_NDK_VERSION}-linux-x86_64.zip" \
27-
ANDROID_SDK_TOOLS_ARCHIVE="sdk-tools-linux-${ANDROID_SDK_TOOLS_VERSION}.zip"
28-
ENV ANDROID_NDK_DL_URL="https://dl.google.com/android/repository/${ANDROID_NDK_ARCHIVE}" \
29-
ANDROID_SDK_TOOLS_DL_URL="https://dl.google.com/android/repository/${ANDROID_SDK_TOOLS_ARCHIVE}"
28+
ENV ANDROID_NDK_ARCHIVE="android-ndk-r${ANDROID_NDK_VERSION}-linux-x86_64.zip"
29+
ENV ANDROID_NDK_DL_URL="https://dl.google.com/android/repository/${ANDROID_NDK_ARCHIVE}"
3030

31-
# install system dependencies
32-
RUN apt update -qq && apt install -qq --yes --no-install-recommends \
33-
python virtualenv python-pip wget curl lbzip2 patch bsdtar sudo && \
34-
rm -rf /var/lib/apt/lists/*
31+
# download and install Android NDK
32+
RUN curl --location --progress-bar \
33+
"${ANDROID_NDK_DL_URL}" \
34+
--output "${ANDROID_NDK_ARCHIVE}" \
35+
&& mkdir --parents "${ANDROID_NDK_HOME_V}" \
36+
&& unzip -q "${ANDROID_NDK_ARCHIVE}" -d "${ANDROID_HOME}" \
37+
&& ln -sfn "${ANDROID_NDK_HOME_V}" "${ANDROID_NDK_HOME}" \
38+
&& rm -rf "${ANDROID_NDK_ARCHIVE}"
3539

36-
# build dependencies
37-
# https://buildozer.readthedocs.io/en/latest/installation.html#android-on-ubuntu-16-04-64bit
38-
RUN dpkg --add-architecture i386 && apt update -qq && apt install -qq --yes --no-install-recommends \
39-
build-essential ccache git libncurses5:i386 libstdc++6:i386 libgtk2.0-0:i386 \
40-
libpangox-1.0-0:i386 libpangoxft-1.0-0:i386 libidn11:i386 python2.7 python2.7-dev \
41-
openjdk-8-jdk zip unzip zlib1g-dev zlib1g:i386
4240

43-
# specific recipes dependencies (e.g. libffi requires autoreconf binary)
44-
RUN apt install -qq --yes --no-install-recommends \
45-
autoconf automake cmake gettext libltdl-dev libtool pkg-config && \
46-
rm -rf /var/lib/apt/lists/*
41+
ENV ANDROID_SDK_HOME="${ANDROID_HOME}/android-sdk"
4742

48-
# download and install Android NDK
49-
RUN curl --location --progress-bar "${ANDROID_NDK_DL_URL}" --output "${ANDROID_NDK_ARCHIVE}" && \
50-
mkdir --parents "${ANDROID_NDK_HOME_V}" && \
51-
unzip -q "${ANDROID_NDK_ARCHIVE}" -d "${ANDROID_HOME}" && \
52-
ln -sfn "${ANDROID_NDK_HOME_V}" "${ANDROID_NDK_HOME}" && \
53-
rm -rf "${ANDROID_NDK_ARCHIVE}"
43+
# get the latest version from https://developer.android.com/studio/index.html
44+
ENV ANDROID_SDK_TOOLS_VERSION="3859397"
45+
ENV ANDROID_SDK_TOOLS_ARCHIVE="sdk-tools-linux-${ANDROID_SDK_TOOLS_VERSION}.zip"
46+
ENV ANDROID_SDK_TOOLS_DL_URL="https://dl.google.com/android/repository/${ANDROID_SDK_TOOLS_ARCHIVE}"
5447

5548
# download and install Android SDK
56-
RUN curl --location --progress-bar "${ANDROID_SDK_TOOLS_DL_URL}" --output "${ANDROID_SDK_TOOLS_ARCHIVE}" && \
57-
mkdir --parents "${ANDROID_SDK_HOME}" && \
58-
unzip -q "${ANDROID_SDK_TOOLS_ARCHIVE}" -d "${ANDROID_SDK_HOME}" && \
59-
rm -rf "${ANDROID_SDK_TOOLS_ARCHIVE}"
49+
RUN curl --location --progress-bar \
50+
"${ANDROID_SDK_TOOLS_DL_URL}" \
51+
--output "${ANDROID_SDK_TOOLS_ARCHIVE}" \
52+
&& mkdir --parents "${ANDROID_SDK_HOME}" \
53+
&& unzip -q "${ANDROID_SDK_TOOLS_ARCHIVE}" -d "${ANDROID_SDK_HOME}" \
54+
&& rm -rf "${ANDROID_SDK_TOOLS_ARCHIVE}"
6055

6156
# update Android SDK, install Android API, Build Tools...
62-
RUN mkdir --parents "${ANDROID_SDK_HOME}/.android/" && \
63-
echo '### User Sources for Android SDK Manager' > "${ANDROID_SDK_HOME}/.android/repositories.cfg"
64-
RUN yes | "${ANDROID_SDK_HOME}/tools/bin/sdkmanager" --licenses
57+
RUN mkdir --parents "${ANDROID_SDK_HOME}/.android/" \
58+
&& echo '### User Sources for Android SDK Manager' \
59+
> "${ANDROID_SDK_HOME}/.android/repositories.cfg"
60+
61+
# accept Android licenses
62+
RUN yes | "${ANDROID_SDK_HOME}/tools/bin/sdkmanager" --licenses >> /dev/null
63+
64+
# download platforms, API, build tools
6565
RUN "${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "platforms;android-19" && \
6666
"${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "platforms;android-27" && \
6767
"${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "build-tools;26.0.2" && \
6868
chmod +x "${ANDROID_SDK_HOME}/tools/bin/avdmanager"
6969

70+
71+
ENV USER="user"
72+
ENV HOME_DIR="/home/${USER}"
73+
ENV WORK_DIR="${HOME_DIR}" \
74+
PATH="${HOME_DIR}/.local/bin:${PATH}"
75+
76+
# install system dependencies
77+
RUN apt update -qq \
78+
&& apt install -qq --yes --no-install-recommends \
79+
python virtualenv python-pip wget curl lbzip2 patch bsdtar sudo \
80+
&& rm -rf /var/lib/apt/lists/*
81+
82+
# build dependencies
83+
# https://buildozer.readthedocs.io/en/latest/installation.html#android-on-ubuntu-16-04-64bit
84+
RUN dpkg --add-architecture i386 \
85+
&& apt update -qq \
86+
&& apt install -qq --yes --no-install-recommends \
87+
build-essential ccache git python2.7 python2.7-dev \
88+
libncurses5:i386 libstdc++6:i386 libgtk2.0-0:i386 \
89+
libpangox-1.0-0:i386 libpangoxft-1.0-0:i386 libidn11:i386 \
90+
openjdk-8-jdk zip unzip zlib1g-dev zlib1g:i386
91+
92+
# specific recipes dependencies (e.g. libffi requires autoreconf binary)
93+
RUN apt install -qq --yes --no-install-recommends \
94+
autoconf automake cmake gettext libltdl-dev libtool pkg-config \
95+
&& rm -rf /var/lib/apt/lists/*
96+
97+
7098
# prepare non root env
7199
RUN useradd --create-home --shell /bin/bash ${USER}
100+
72101
# with sudo access and no password
73102
RUN usermod -append --groups sudo ${USER}
74103
RUN echo "%sudo ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
75-
RUN pip install --quiet --upgrade cython==0.28.6
104+
105+
106+
RUN pip install --upgrade cython==0.28.6
107+
76108
WORKDIR ${WORK_DIR}
77109
COPY . ${WORK_DIR}
110+
78111
# user needs ownership/write access to these directories
79112
RUN chown --recursive ${USER} ${WORK_DIR} ${ANDROID_SDK_HOME}
80113
USER ${USER}
114+
81115
# install python-for-android from current branch
82-
RUN virtualenv --python=python venv && . venv/bin/activate && pip install --quiet -e .
116+
RUN virtualenv --python=python venv \
117+
&& . venv/bin/activate \
118+
&& pip install -e .

0 commit comments

Comments
 (0)