Skip to content

Commit 34e4f25

Browse files
authored
Optimize Dockerfile (#2)
* optimize Dockerfile grouping RUN statements (from 9.22GB to 7.57GB) * optimize Dockerfile using multi-stage builds (https://learnk8s.io/blog/smaller-docker-images) (from 7.57GB to 7.37GB) * unified env vars * finally use single toolchains.tag.gz archive * unfortunately using a single ENV does not seems to work * apply suggestions from code review
1 parent 37d04cb commit 34e4f25

File tree

1 file changed

+51
-44
lines changed

1 file changed

+51
-44
lines changed

Dockerfile

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
FROM ubuntu:latest
1+
FROM ubuntu:latest as build
22

33
ENV TZ=Europe/Rome
4-
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
5-
RUN apt-get update && \
6-
# TODO add --no-install-recommends
4+
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \
5+
apt-get update && \
6+
# TODO add --no-install-recommends
77
apt-get install -y \
88
build-essential \
99
# Intall clang compiler used by macos
@@ -27,56 +27,63 @@ RUN apt-get update && \
2727
&& rm -rf /var/lib/apt/lists/*
2828
# Install toolchains in /opt
2929
RUN curl downloads.arduino.cc/tools/internal/toolchains.tar.gz | tar -xz "opt"
30-
RUN curl downloads.arduino.cc/tools/internal/i686-ubuntu16.04-linux-gnu.tar.gz | tar -xzC /opt
31-
# Remove useless toolchains:
32-
RUN rm -r /opt/arm-rpi-4.9.3-linux-gnueabihf/
33-
RUN rm -r /opt/gcc-linaro-7.2.1-2017.11-x86_64_aarch64-linux-gnu/
34-
RUN rm -r /opt/*ubuntu12.04*
35-
#install proper arm toolchains
36-
RUN curl -L 'https://developer.arm.com/-/media/Files/downloads/gnu-a/8.3-2019.03/binrel/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf.tar.xz' | tar -xJC /opt
37-
RUN curl -L 'https://developer.arm.com/-/media/Files/downloads/gnu-a/8.3-2019.03/binrel/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu.tar.xz' | tar -xJC /opt
30+
# install proper arm toolchains (already present in the toolchains.tar.gz archive)
31+
# curl -L 'https://developer.arm.com/-/media/Files/downloads/gnu-a/8.3-2019.03/binrel/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf.tar.xz' | tar -xJC /opt && \
32+
# curl -L 'https://developer.arm.com/-/media/Files/downloads/gnu-a/8.3-2019.03/binrel/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu.tar.xz' | tar -xJC /opt
3833

39-
# install macos10.15 sdk
40-
COPY MacOSX10.15.sdk.tar.xz /opt/osxcross/tarballs/
41-
# remove old unused macos10.09 sdk because we are using 10.15 one
42-
RUN rm -rf /opt/osxcross/tarballs/MacOSX10.9.sdk.tar.bz2
43-
RUN cd /opt/osxcross; git pull; UNATTENDED=1 SDK_VERSION=10.15 ./build.sh
34+
RUN cd /opt/osxcross \
35+
git pull \
36+
# use a specific version of osxcross (it does not have tags)
37+
git checkout da2c3d4ff604458a931b08b3af800c5a454136de \
38+
UNATTENDED=1 SDK_VERSION=10.15 ./build.sh
4439
# Set toolchains paths
4540
# arm-linux-gnueabihf-gcc -> linux_arm
46-
ENV PATH=/opt/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/:$PATH
4741
# aarch64-linux-gnu-gcc -> linux_arm64
48-
ENV PATH=/opt/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu/bin/:$PATH
4942
# x86_64-ubuntu16.04-linux-gnu-gcc -> linux_amd64
50-
ENV PATH=/opt/x86_64-ubuntu16.04-linux-gnu-gcc/bin/:$PATH
5143
# i686-ubuntu16.04-linux-gnu-gcc -> linux_386
52-
ENV PATH=/opt/i686-ubuntu16.04-linux-gnu/bin/:$PATH
5344
# o64-clang -> darwin_amd64
54-
ENV PATH=/opt/osxcross/target/bin/:$PATH
45+
ENV PATH=/opt/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/:/opt/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu/bin/:/opt/x86_64-ubuntu16.04-linux-gnu-gcc/bin/:/opt/i686-ubuntu16.04-linux-gnu/bin/:/opt/osxcross/target/bin/:$PATH
5546

56-
RUN mkdir -p /workdir
5747
WORKDIR /workdir
5848

59-
# debug/utilities
60-
RUN alias ll="ls -lah"
61-
RUN apt-get update && apt-get install -y tree cmake-curses-gui nano && rm -rf /var/lib/apt/lists/*
62-
63-
# Handle libusb and libudev
49+
# Handle libusb and libudev compilation and merging
6450
COPY deps/ /opt/lib/
6551
# compiler name is arm-linux-gnueabihf-gcc '-gcc' is added by ./configure
66-
ENV CROSS_COMPILE=x86_64-ubuntu16.04-linux-gnu
67-
RUN /opt/lib/build_libs.sh
68-
ENV CROSS_COMPILE=arm-linux-gnueabihf
69-
RUN /opt/lib/build_libs.sh
70-
ENV CROSS_COMPILE=aarch64-linux-gnu
71-
RUN /opt/lib/build_libs.sh
72-
ENV CROSS_COMPILE=i686-ubuntu16.04-linux-gnu
73-
RUN /opt/lib/build_libs.sh
74-
ENV CROSS_COMPILE=i686-w64-mingw32
75-
RUN /opt/lib/build_libs.sh
76-
# macos does not need eudev
77-
# CROSS_COMPILER is used to override the compiler
78-
ENV CROSS_COMPILER=o64-clang
79-
ENV CROSS_COMPILE=x86_64-apple-darwin13
80-
RUN /opt/lib/build_libs.sh
52+
RUN CROSS_COMPILE=x86_64-ubuntu16.04-linux-gnu /opt/lib/build_libs.sh && \
53+
CROSS_COMPILE=arm-linux-gnueabihf /opt/lib/build_libs.sh && \
54+
CROSS_COMPILE=aarch64-linux-gnu /opt/lib/build_libs.sh && \
55+
CROSS_COMPILE=i686-ubuntu16.04-linux-gnu /opt/lib/build_libs.sh && \
56+
CROSS_COMPILE=i686-w64-mingw32 /opt/lib/build_libs.sh && \
57+
# CROSS_COMPILER is used to override the compiler
58+
CROSS_COMPILER=o64-clang CROSS_COMPILE=x86_64-apple-darwin13 /opt/lib/build_libs.sh
59+
60+
FROM ubuntu:latest
61+
# Copy all the installed toolchains and compiled libs
62+
COPY --from=build /opt /opt
63+
ENV TZ=Europe/Rome
64+
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \
65+
apt-get update && \
66+
apt-get install -y \
67+
build-essential \
68+
# Intall clang compiler used by macos
69+
clang \
70+
cmake \
71+
dh-autoreconf \
72+
git \
73+
gperf \
74+
# Install Windows cross-tools
75+
mingw-w64 \
76+
pkg-config \
77+
tar \
78+
&& rm -rf /var/lib/apt/lists/*
79+
# Set toolchains paths
80+
# arm-linux-gnueabihf-gcc -> linux_arm
81+
# aarch64-linux-gnu-gcc -> linux_arm64
82+
# x86_64-ubuntu16.04-linux-gnu-gcc -> linux_amd64
83+
# i686-ubuntu16.04-linux-gnu-gcc -> linux_386
84+
# o64-clang -> darwin_amd64
85+
ENV PATH=/opt/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/:/opt/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu/bin/:/opt/x86_64-ubuntu16.04-linux-gnu-gcc/bin/:/opt/i686-ubuntu16.04-linux-gnu/bin/:/opt/osxcross/target/bin/:$PATH
86+
87+
WORKDIR /workdir
8188

82-
ENTRYPOINT ["/bin/bash"]
89+
ENTRYPOINT ["/bin/bash"]

0 commit comments

Comments
 (0)