From ce0d62dca3083939e78b6244c87c0cefadb9a621 Mon Sep 17 00:00:00 2001 From: Lars Gierth Date: Fri, 5 Feb 2016 04:03:30 +0100 Subject: [PATCH] docker: save image space by excluding .git/ License: MIT Signed-off-by: Lars Gierth --- .dockerignore | 3 +++ Dockerfile | 8 +++++++- test/Dockerfile | 8 ++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/.dockerignore b/.dockerignore index 4b2bacc27..ee0f144b4 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,3 +1,6 @@ +.git/ +!.git/HEAD +!.git/refs/ cmd/ipfs/ipfs vendor/gx/ test/ diff --git a/Dockerfile b/Dockerfile index b307ac0cf..b641c4b46 100644 --- a/Dockerfile +++ b/Dockerfile @@ -48,9 +48,15 @@ RUN apk add --update musl go=$GO_VERSION git bash wget ca-certificates \ # Invoke gx && cd $SRC_PATH \ && gx --verbose install --global \ + # We get the current commit using this hack, + # so that we don't have to copy all of .git/ into the build context. + # This saves us quite a bit of image size. + && ref="$(cat .git/HEAD | cut -d' ' -f2)" \ + && commit="$(cat .git/$ref | head -c 7)" \ + && echo "ldflags=-X github.com/ipfs/go-ipfs/repo/config.CurrentCommit=$commit" \ # Build and install IPFS and entrypoint script && cd $SRC_PATH/cmd/ipfs \ - && go build -ldflags "-X github.com/ipfs/go-ipfs/repo/config.CurrentCommit=$(git rev-parse --short HEAD 2> /dev/null)" \ + && go build -ldflags "-X github.com/ipfs/go-ipfs/repo/config.CurrentCommit=$commit" \ && cp ipfs /usr/local/bin/ipfs \ && cp $SRC_PATH/bin/container_daemon /usr/local/bin/start_ipfs \ && chmod 755 /usr/local/bin/start_ipfs \ diff --git a/test/Dockerfile b/test/Dockerfile index b4adec1ce..61a4a2c1f 100644 --- a/test/Dockerfile +++ b/test/Dockerfile @@ -42,8 +42,12 @@ RUN apk add --update musl go=$GO_VERSION git bash wget ca-certificates \ COPY . $SRC_PATH -RUN cd $SRC_PATH/cmd/ipfs \ - && go build -ldflags "-X github.com/ipfs/go-ipfs/repo/config.CurrentCommit=$(git rev-parse --short HEAD 2> /dev/null)" \ +RUN cd $SRC_PATH \ + && ref="$(cat .git/HEAD | cut -d' ' -f2)" \ + && commit="$(cat .git/$ref | head -c 7)" \ + && echo "ldflags=-X github.com/ipfs/go-ipfs/repo/config.CurrentCommit=$commit" \ + && cd $SRC_PATH/cmd/ipfs \ + && go build -ldflags "-X github.com/ipfs/go-ipfs/repo/config.CurrentCommit=$commit" \ && cp ipfs /usr/local/bin/ipfs \ && cp $SRC_PATH/bin/container_daemon /usr/local/bin/start_ipfs \ && chmod 755 /usr/local/bin/start_ipfs \