Skip to content

Commit d03070e

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 d03070e

File tree

1 file changed

+102
-47
lines changed

1 file changed

+102
-47
lines changed

Dockerfile

Lines changed: 102 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,137 @@
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+
RUN apt-get -y update -qq \
23+
&& apt-get -y install -qq --no-install-recommends curl unzip \
24+
&& apt-get -y autoremove \
25+
&& apt-get -y clean \
26+
&& rm -rf /var/lib/apt/lists/*
27+
28+
29+
ENV ANDROID_NDK_HOME="${ANDROID_HOME}/android-ndk"
30+
ENV ANDROID_NDK_HOME_V="${ANDROID_NDK_HOME}-r${ANDROID_NDK_VERSION}"
1231

13-
ENV USER="user"
14-
ENV HOME_DIR="/home/${USER}"
15-
ENV WORK_DIR="${HOME_DIR}" \
16-
PATH="${HOME_DIR}/.local/bin:${PATH}"
1732
# get the latest version from https://developer.android.com/ndk/downloads/index.html
1833
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"
34+
ENV ANDROID_NDK_ARCHIVE="android-ndk-r${ANDROID_NDK_VERSION}-linux-x86_64.zip"
35+
ENV ANDROID_NDK_DL_URL="https://dl.google.com/android/repository/${ANDROID_NDK_ARCHIVE}"
2136

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

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/*
3546

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

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

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

6162
# 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
63+
RUN mkdir --parents "${ANDROID_SDK_HOME}/.android/" \
64+
&& echo '### User Sources for Android SDK Manager' \
65+
> "${ANDROID_SDK_HOME}/.android/repositories.cfg"
66+
67+
# accept Android licenses (JDK necessary!)
68+
RUN apt-get -y update -qq \
69+
&& apt-get -y install -qq --no-install-recommends openjdk-8-jdk \
70+
&& apt-get -y autoremove \
71+
&& apt-get -y clean \
72+
&& rm -rf /var/lib/apt/lists/*
73+
RUN yes | "${ANDROID_SDK_HOME}/tools/bin/sdkmanager" --licenses > /dev/null
74+
75+
# download platforms, API, build tools
6576
RUN "${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "platforms;android-19" && \
6677
"${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "platforms;android-27" && \
6778
"${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "build-tools;26.0.2" && \
6879
chmod +x "${ANDROID_SDK_HOME}/tools/bin/avdmanager"
6980

81+
82+
ENV USER="user"
83+
ENV HOME_DIR="/home/${USER}"
84+
ENV WORK_DIR="${HOME_DIR}" \
85+
PATH="${HOME_DIR}/.local/bin:${PATH}"
86+
87+
# install system dependencies
88+
RUN apt-get -y update -qq \
89+
&& apt-get -y install -qq --no-install-recommends \
90+
python virtualenv python-pip wget lbzip2 patch sudo \
91+
&& apt-get -y autoremove \
92+
&& apt-get -y clean \
93+
&& rm -rf /var/lib/apt/lists/*
94+
95+
# build dependencies
96+
# https://buildozer.readthedocs.io/en/latest/installation.html#android-on-ubuntu-16-04-64bit
97+
RUN dpkg --add-architecture i386 \
98+
&& apt-get -y update -qq \
99+
&& apt-get -y install -qq --no-install-recommends \
100+
build-essential ccache git python2.7 python2.7-dev \
101+
libncurses5:i386 libstdc++6:i386 libgtk2.0-0:i386 \
102+
libpangox-1.0-0:i386 libpangoxft-1.0-0:i386 libidn11:i386 \
103+
zip zlib1g-dev zlib1g:i386 \
104+
&& apt-get -y autoremove \
105+
&& apt-get -y clean \
106+
&& rm -rf /var/lib/apt/lists/*
107+
108+
# specific recipes dependencies (e.g. libffi requires autoreconf binary)
109+
RUN apt-get -y update -qq \
110+
&& apt-get -y install -qq --no-install-recommends \
111+
autoconf automake cmake gettext libltdl-dev libtool pkg-config \
112+
&& apt-get -y autoremove \
113+
&& apt-get -y clean \
114+
&& rm -rf /var/lib/apt/lists/*
115+
116+
70117
# prepare non root env
71118
RUN useradd --create-home --shell /bin/bash ${USER}
119+
72120
# with sudo access and no password
73121
RUN usermod -append --groups sudo ${USER}
74122
RUN echo "%sudo ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
75-
RUN pip install --quiet --upgrade cython==0.28.6
123+
124+
125+
RUN pip install --upgrade cython==0.28.6
126+
76127
WORKDIR ${WORK_DIR}
77128
COPY . ${WORK_DIR}
129+
78130
# user needs ownership/write access to these directories
79131
RUN chown --recursive ${USER} ${WORK_DIR} ${ANDROID_SDK_HOME}
80132
USER ${USER}
133+
81134
# install python-for-android from current branch
82-
RUN virtualenv --python=python venv && . venv/bin/activate && pip install --quiet -e .
135+
RUN virtualenv --python=python venv \
136+
&& . venv/bin/activate \
137+
&& pip install -e .

0 commit comments

Comments
 (0)