[VOL-4673] Make better use of multistage builds to reduce docker images size

Change-Id: I230c256e30d1ca580b20f8a88d0258cf501d7b19
diff --git a/build/package/Dockerfile.bbf-adapter b/build/package/Dockerfile.bbf-adapter
index 72a4661..512e202 100644
--- a/build/package/Dockerfile.bbf-adapter
+++ b/build/package/Dockerfile.bbf-adapter
@@ -19,61 +19,8 @@
 
 FROM voltha/bbf-adapter-builder:local AS dev
 
-# Use Standard go build directory structure
-WORKDIR /go/src
-COPY . .
-
-ARG EXTRA_GO_BUILD_TAGS=""
-
-ARG CGO_PARAMETER=1
-
-ARG org_label_schema_version=unknown
-ARG org_label_schema_vcs_url=unknown
-ARG org_label_schema_vcs_ref=unknown
-ARG org_label_schema_build_date=unknown
-ARG org_opencord_vcs_commit_date=unknown
-ARG org_opencord_vcs_dirty=unknown
-
-# Build
-SHELL ["/bin/ash", "-o", "pipefail", "-c"]
-RUN \
-      CGO_ENABLED=$CGO_PARAMETER go build $EXTRA_GO_BUILD_TAGS -mod=vendor -o /app/bbf-adapter \
-      -ldflags \
-      "-X github.com/opencord/voltha-lib-go/v7/pkg/version.version=$org_label_schema_version \
-      -X github.com/opencord/voltha-lib-go/v7/pkg/version.vcsRef=$org_label_schema_vcs_ref \
-      -X github.com/opencord/voltha-lib-go/v7/pkg/version.vcsDirty=$org_opencord_vcs_dirty \
-      -X github.com/opencord/voltha-lib-go/v7/pkg/version.goVersion=$(go version 2>&1 | sed -E  's/.*go([0-9]+\.[0-9]+\.[0-9]+).*/\1/g') \
-      -X github.com/opencord/voltha-lib-go/v7/pkg/version.os=$(go env GOHOSTOS) \
-      -X github.com/opencord/voltha-lib-go/v7/pkg/version.arch=$(go env GOHOSTARCH) \
-      -X github.com/opencord/voltha-lib-go/v7/pkg/version.buildTime=$org_label_schema_build_date" \
-      ./cmd/bbf-adapter/
-
-WORKDIR /app
-
-# -------------
-# Image creation stage
-FROM alpine:3.13 AS prod
-
-SHELL ["/bin/ash", "-o", "pipefail", "-c"]
-
-RUN apk add --no-cache build-base=0.5-r2 pcre2-dev=10.36-r0 git=2.30.2-r0 cmake=3.18.4-r1 libssh-dev=0.9.5-r0 openssl-dev=1.1.1n-r0 openssl=1.1.1n-r0 bash=5.1.16-r0
-
-RUN rm -r /usr/local/lib && ln -s /usr/local/lib64 /usr/local/lib
-
-WORKDIR /
-
-COPY --from=dev /libyang /libyang
-COPY --from=dev /sysrepo /sysrepo
-
-#Build libyang
-WORKDIR /libyang/build
-RUN make install && \
-    rm -rf libyang
-
-#Build sysrepo
-WORKDIR /sysrepo/build
-RUN make install && \
-    rm -rf sysrepo
+#Build runtime dependencies
+RUN apk add --no-cache libssh-dev=0.9.5-r0 openssl-dev=1.1.1n-r0 openssl=1.1.1n-r0 bash=5.1.16-r0
 
 ARG LIBNETCONF2_VERSION
 ARG NETOPEER2_VERSION
@@ -100,6 +47,52 @@
     make install && \
     rm -rf netopeer2
 
+# Use Standard go build directory structure
+WORKDIR /go/src
+COPY . .
+
+ARG EXTRA_GO_BUILD_TAGS=""
+
+ARG CGO_PARAMETER=1
+
+ARG org_label_schema_version=unknown
+ARG org_label_schema_vcs_url=unknown
+ARG org_label_schema_vcs_ref=unknown
+ARG org_label_schema_build_date=unknown
+ARG org_opencord_vcs_commit_date=unknown
+ARG org_opencord_vcs_dirty=unknown
+
+# Build bbf-adapter
+SHELL ["/bin/ash", "-o", "pipefail", "-c"]
+RUN \
+      CGO_ENABLED=$CGO_PARAMETER go build $EXTRA_GO_BUILD_TAGS -mod=vendor -o /app/bbf-adapter \
+      -ldflags \
+      "-X github.com/opencord/voltha-lib-go/v7/pkg/version.version=$org_label_schema_version \
+      -X github.com/opencord/voltha-lib-go/v7/pkg/version.vcsRef=$org_label_schema_vcs_ref \
+      -X github.com/opencord/voltha-lib-go/v7/pkg/version.vcsDirty=$org_opencord_vcs_dirty \
+      -X github.com/opencord/voltha-lib-go/v7/pkg/version.goVersion=$(go version 2>&1 | sed -E  's/.*go([0-9]+\.[0-9]+\.[0-9]+).*/\1/g') \
+      -X github.com/opencord/voltha-lib-go/v7/pkg/version.os=$(go env GOHOSTOS) \
+      -X github.com/opencord/voltha-lib-go/v7/pkg/version.arch=$(go env GOHOSTARCH) \
+      -X github.com/opencord/voltha-lib-go/v7/pkg/version.buildTime=$org_label_schema_build_date" \
+      ./cmd/bbf-adapter/
+
+# -------------
+# Image creation stage
+FROM alpine:3.13 AS prod
+
+SHELL ["/bin/ash", "-o", "pipefail", "-c"]
+
+RUN apk add --no-cache pcre2=10.36-r0 libssh=0.9.5-r0 openssl=1.1.1n-r0
+
+# Dependencies install their library files in lib64, add it to the path
+RUN echo "/lib:/usr/local/lib:/usr/lib:/usr/local/lib64" > /etc/ld-musl-x86_64.path
+
+# Copy dependencies files
+COPY --from=dev /usr/local/bin /usr/local/bin
+COPY --from=dev /usr/local/lib64 /usr/local/lib64
+COPY --from=dev /usr/local/share /usr/local/share
+COPY --from=dev /etc/sysrepo /etc/sysrepo
+
 # Copy yang files and install them to sysrepo
 COPY ./build/yang-files /yang
 RUN for f in /yang/*.yang; do sysrepoctl -i "$f" -s /yang -p 664 -v3; done
diff --git a/build/tools/Dockerfile.builder b/build/tools/Dockerfile.builder
index 1fe22ad..f84c03a 100644
--- a/build/tools/Dockerfile.builder
+++ b/build/tools/Dockerfile.builder
@@ -15,19 +15,35 @@
 # Dockerfile with golang and the sysrepo dependencies for voltha-northbound-bff-adapter
 # This image is used for testing, static code analysis and building
 
+# -------------
+# Build golangci-lint
+FROM --platform=linux/amd64 golang:1.16.3-alpine3.13 AS lint-builder
+
+RUN apk add --no-cache build-base=0.5-r2
+
+#Install golangci-lint
+RUN go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.44.2
+
+# -------------
+# Create the builder and tools image for the bbf adapter
+
 FROM --platform=linux/amd64 golang:1.16.3-alpine3.13 AS dev
 
 RUN mkdir -m 777 /.cache /go/pkg
 
 RUN apk add --no-cache build-base=0.5-r2 pcre2-dev=10.36-r0 git=2.30.2-r0 cmake=3.18.4-r1
 
-#Install golangci-lint
-RUN go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.44.2
+# Dependencies install their library files in lib64, add it to the path
+RUN echo "/lib:/usr/local/lib:/usr/lib:/usr/local/lib64" > /etc/ld-musl-x86_64.path
 
+# Get golangci-lint binary from its builder
+COPY --from=lint-builder /go/bin/golangci-lint /usr/bin/
 
 ARG LIBYANG_VERSION
 ARG SYSREPO_VERSION
 
+#Build compile time dependencies
+
 #Build libyang
 WORKDIR /
 RUN git clone https://github.com/CESNET/libyang.git
@@ -36,7 +52,8 @@
 WORKDIR /libyang/build
 RUN cmake -D CMAKE_BUILD_TYPE:String="Release" .. && \
     make && \
-    make install
+    make install && \
+    rm -rf libyang
 
 #Build sysrepo
 WORKDIR /
@@ -46,7 +63,8 @@
 WORKDIR /sysrepo/build
 RUN cmake -D CMAKE_BUILD_TYPE:String="Release" .. && \
     make && \
-    make install
+    make install && \
+    rm -rf sysrepo
 
 WORKDIR /app