Skip to content

Commit 202ff8a

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 202ff8a

File tree

1 file changed

+86
-46
lines changed

1 file changed

+86
-46
lines changed

Dockerfile

Lines changed: 86 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,122 @@
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"
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}"
2130

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}"
31+
RUN apt update -qq \
32+
&& apt install -qq --yes --no-install-recommends curl \
33+
&& rm -rf /var/lib/apt/lists/*
3034

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/*
35+
# download and install Android NDK
36+
RUN curl --location --progress-bar \
37+
"${ANDROID_NDK_DL_URL}" \
38+
--output "${ANDROID_NDK_ARCHIVE}" \
39+
&& mkdir --parents "${ANDROID_NDK_HOME_V}" \
40+
&& unzip -q "${ANDROID_NDK_ARCHIVE}" -d "${ANDROID_HOME}" \
41+
&& ln -sfn "${ANDROID_NDK_HOME_V}" "${ANDROID_NDK_HOME}" \
42+
&& rm -rf "${ANDROID_NDK_ARCHIVE}"
3543

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
4244

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/*
45+
ENV ANDROID_SDK_HOME="${ANDROID_HOME}/android-sdk"
4746

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}"
47+
# get the latest version from https://developer.android.com/studio/index.html
48+
ENV ANDROID_SDK_TOOLS_VERSION="3859397"
49+
ENV ANDROID_SDK_TOOLS_ARCHIVE="sdk-tools-linux-${ANDROID_SDK_TOOLS_VERSION}.zip"
50+
ENV ANDROID_SDK_TOOLS_DL_URL="https://dl.google.com/android/repository/${ANDROID_SDK_TOOLS_ARCHIVE}"
5451

5552
# 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}"
53+
RUN curl --location --progress-bar \
54+
"${ANDROID_SDK_TOOLS_DL_URL}" \
55+
--output "${ANDROID_SDK_TOOLS_ARCHIVE}" \
56+
&& mkdir --parents "${ANDROID_SDK_HOME}" \
57+
&& unzip -q "${ANDROID_SDK_TOOLS_ARCHIVE}" -d "${ANDROID_SDK_HOME}" \
58+
&& rm -rf "${ANDROID_SDK_TOOLS_ARCHIVE}"
6059

6160
# 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
61+
RUN mkdir --parents "${ANDROID_SDK_HOME}/.android/" \
62+
&& echo '### User Sources for Android SDK Manager' \
63+
> "${ANDROID_SDK_HOME}/.android/repositories.cfg"
64+
65+
# accept Android licenses
66+
RUN yes | "${ANDROID_SDK_HOME}/tools/bin/sdkmanager" --licenses >> /dev/null
67+
68+
# download platforms, API, build tools
6569
RUN "${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "platforms;android-19" && \
6670
"${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "platforms;android-27" && \
6771
"${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "build-tools;26.0.2" && \
6872
chmod +x "${ANDROID_SDK_HOME}/tools/bin/avdmanager"
6973

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

0 commit comments

Comments
 (0)