Skip to content

Commit

Permalink
build: reduce the image size by introducing multi stage build
Browse files Browse the repository at this point in the history
longhorn/longhorn 5911

Signed-off-by: Jack Lin <jack.lin@suse.com>
  • Loading branch information
ChanYiLin authored and David Ko committed Nov 22, 2023
1 parent 1618c5b commit 81dc171
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
46 changes: 32 additions & 14 deletions package/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,40 +1,58 @@
FROM registry.suse.com/bci/bci-base:15.5
FROM registry.suse.com/bci/bci-base:15.5 AS builder

ARG ARCH=amd64

RUN zypper -n addrepo --refresh https://download.opensuse.org/repositories/system:/snappy/SLE_15/system:snappy.repo && \
zypper -n addrepo --refresh https://download.opensuse.org/repositories/network:/utilities/SLE_15_SP5/network:utilities.repo && \
zypper --gpg-auto-import-keys ref

RUN if [ ${ARCH} == "amd64" ]; then \
zypper -n install autoconf libtool libunwind-devel gcc-c++; \
fi

RUN zypper -n install cmake kmod curl nfs-client nfs4-acl-tools fuse git gcc \
libibverbs librdmacm1 rdma-core-devel perl-Config-General libaio1 sg3_utils \
iputils telnet iperf qemu-tools wget iproute2 xsltproc docbook-xsl-stylesheets e2fsprogs cifs-utils && \
RUN zypper -n install cmake curl git gcc wget xsltproc docbook-xsl-stylesheets && \
rm -rf /var/cache/zypp/*

# Install grpc_health_probe
RUN wget https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/v0.4.21/grpc_health_probe-linux-${ARCH} -O /usr/local/bin/grpc_health_probe && \
chmod +x /usr/local/bin/grpc_health_probe

# Build liblonghorn
ENV LIBLONGHORN_COMMIT_ID 53d1c063b95efc8d949b095bd4bf04637230265f
RUN cd /usr/src && \
git clone https://github.com/rancher/liblonghorn.git && \
cd liblonghorn && \
git checkout 53d1c063b95efc8d949b095bd4bf04637230265f && \
git checkout ${LIBLONGHORN_COMMIT_ID} && \
make; \
make install

# Build TGT
ENV TGT_COMMIT_ID 3a8bc4823b5390e046f7aa8231ed262c0365c42c
RUN cd /usr/src && \
git clone https://github.com/rancher/tgt.git && \
cd tgt && \
git checkout 3a8bc4823b5390e046f7aa8231ed262c0365c42c && \
git checkout ${TGT_COMMIT_ID} && \
make; \
make install

# Install grpc_health_probe
RUN wget https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/v0.4.21/grpc_health_probe-linux-${ARCH} -O /usr/local/bin/grpc_health_probe && \
chmod +x /usr/local/bin/grpc_health_probe

FROM registry.suse.com/bci/bci-base:15.5 AS release

ARG ARCH=amd64

RUN zypper -n addrepo --refresh https://download.opensuse.org/repositories/system:/snappy/SLE_15/system:snappy.repo && \
zypper -n addrepo --refresh https://download.opensuse.org/repositories/network:/utilities/SLE_15_SP5/network:utilities.repo && \
zypper --gpg-auto-import-keys ref

RUN zypper -n install nfs-client nfs4-acl-tools cifs-utils libaio1 sg3_utils \
iputils iproute2 qemu-tools e2fsprogs && \
rm -rf /var/cache/zypp/*

# Copy pre-built binaries from builder
COPY --from=builder \
/usr/local/bin/grpc_health_probe \
/usr/sbin/tgt-admin \
/usr/sbin/tgt-setup-lun \
/usr/sbin/tgtadm \
/usr/sbin/tgtd \
/usr/sbin/tgtimg \
/usr/local/bin/

COPY bin/longhorn bin/longhorn-instance-manager /usr/local/bin/

COPY package/launch-simple-longhorn package/engine-manager package/launch-simple-file /usr/local/bin/
Expand Down
6 changes: 4 additions & 2 deletions scripts/package
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ fi
cp /usr/local/bin/longhorn-instance-manager ./bin/

# update base image to get latest changes
BASE_IMAGE=`grep FROM package/Dockerfile | awk '{print $2}'`
docker pull ${BASE_IMAGE}
grep FROM package/Dockerfile | awk '{print $2}' | while read -r BASE_IMAGE
do
docker pull "$BASE_IMAGE"
done

docker build --build-arg ARCH=${ARCH} -t ${IMAGE} -f package/Dockerfile .

Expand Down

0 comments on commit 81dc171

Please sign in to comment.