From d60992fdff4b9bdfc74363374eda937dd5acebe0 Mon Sep 17 00:00:00 2001 From: Georgy Savva Date: Thu, 18 Mar 2021 19:40:48 +0800 Subject: [PATCH] Make docker build faster (#1074) * Make docker build faster Docker build now mounts a volume for Go build cache during the image build. Another small optimization is: mount source code base instead of COPYing it. As a result the docker re-build time is 4.5s instead of 50s before. * Fix CI errors Use special "syntax" word to refer to an external docker frontend in Dockerfile. Remove empty lines to not cause warning. * Use COPY instead of mount Get back to coping the codebase instead of mounting it. * Enable docker buildkit in CI Set DOCKER_BUILDKIT in github actions * Enable docker BuildKit in scripts Set DOCKER_BUILDKIT=1 and COMPOSE_DOCKER_CLI_BUILD=1 envs in scripts. Also use sha to refer to docker frontend image. * Use exact version instead of sha for docker image For some reason sha referencing didn't work on GH action, I switched back to exact version number. * Improve .dockerignore Add a few folders to .dockerignore --- .dockerignore | 3 +++ .github/workflows/integration-tests.yml | 9 +++++++++ Dockerfile | 19 ++++++++++++------- check_static.sh | 10 ++++++++++ tools/docker-network/run.sh | 2 ++ tools/integration-tests/runTests.sh | 2 ++ 6 files changed, 38 insertions(+), 7 deletions(-) create mode 100755 check_static.sh diff --git a/.dockerignore b/.dockerignore index 40c5717ba7..8af885a524 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,5 +1,8 @@ .git .gitignore +.github/ +docs/ +.idea/ LICENSE README.md diff --git a/.github/workflows/integration-tests.yml b/.github/workflows/integration-tests.yml index 10d710bff4..5a21b3152c 100644 --- a/.github/workflows/integration-tests.yml +++ b/.github/workflows/integration-tests.yml @@ -11,6 +11,7 @@ jobs: name: autopeering env: TEST_NAME: autopeering + DOCKER_BUILDKIT: 1 runs-on: ubuntu-latest steps: @@ -46,6 +47,7 @@ jobs: name: common env: TEST_NAME: common + DOCKER_BUILDKIT: 1 runs-on: ubuntu-latest steps: @@ -80,6 +82,7 @@ jobs: name: consensus env: TEST_NAME: consensus + DOCKER_BUILDKIT: 1 runs-on: ubuntu-latest steps: @@ -115,6 +118,7 @@ jobs: name: drng env: TEST_NAME: drng + DOCKER_BUILDKIT: 1 runs-on: ubuntu-latest steps: @@ -151,6 +155,7 @@ jobs: name: message env: TEST_NAME: message + DOCKER_BUILDKIT: 1 runs-on: ubuntu-latest steps: @@ -187,6 +192,7 @@ jobs: name: value env: TEST_NAME: value + DOCKER_BUILDKIT: 1 runs-on: ubuntu-latest steps: @@ -221,6 +227,7 @@ jobs: name: faucet env: TEST_NAME: faucet + DOCKER_BUILDKIT: 1 runs-on: ubuntu-latest steps: @@ -255,6 +262,7 @@ jobs: name: syncbeacon env: TEST_NAME: syncbeacon + DOCKER_BUILDKIT: 1 runs-on: ubuntu-latest steps: @@ -289,6 +297,7 @@ jobs: name: mana env: TEST_NAME: mana + DOCKER_BUILDKIT: 1 runs-on: ubuntu-latest steps: diff --git a/Dockerfile b/Dockerfile index f777f615bd..9f1a0008ac 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,5 @@ +# syntax = docker/dockerfile:1.2.1 + ############################ # Build ############################ @@ -19,13 +21,16 @@ ENV GO111MODULE=on RUN go mod download RUN go mod verify -# Copy everything from the current directory to the PWD(Present Working Directory) inside the container -COPY . . - -# Build the binary -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \ - -ldflags='-w -s -extldflags "-static"' -a \ - -o /go/bin/goshimmer +# 1. Mount everything from the current directory to the PWD(Present Working Directory) inside the container +# 2. Mount the build cache volume +# 3. Build the binary +# 4. Verify that goshimmer binary is statically linked +RUN --mount=target=. \ + --mount=type=cache,target=/root/.cache/go-build \ + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \ + -ldflags='-w -s -extldflags "-static"' \ + -o /go/bin/goshimmer; \ + ./check_static.sh ############################ # Image diff --git a/check_static.sh b/check_static.sh new file mode 100755 index 0000000000..a1abfc8d86 --- /dev/null +++ b/check_static.sh @@ -0,0 +1,10 @@ +#!/bin/bash +# This script uses ldd utility to verify that result goshimmer binary is statically linked + +ldd /go/bin/goshimmer &> /dev/null +if [ $? -ne 0 ]; then + exit 0 +else + echo "/go/bin/goshimmer must be a statically linked binary" + exit 1 +fi diff --git a/tools/docker-network/run.sh b/tools/docker-network/run.sh index 57d07ce5e8..913dcc60fe 100755 --- a/tools/docker-network/run.sh +++ b/tools/docker-network/run.sh @@ -8,6 +8,8 @@ fi REPLICAS=$1 GRAFANA=${2:-0} +DOCKER_BUILDKIT=1 +COMPOSE_DOCKER_CLI_BUILD=1 echo "Build GoShimmer" docker-compose -f builder/docker-compose.builder.yml up --abort-on-container-exit --exit-code-from builder diff --git a/tools/integration-tests/runTests.sh b/tools/integration-tests/runTests.sh index 2376c5bb81..81c72b6e63 100755 --- a/tools/integration-tests/runTests.sh +++ b/tools/integration-tests/runTests.sh @@ -2,6 +2,8 @@ TEST_NAMES='autopeering common drng message value consensus faucet syncbeacon mana' +DOCKER_BUILDKIT=1 +COMPOSE_DOCKER_CLI_BUILD=1 echo "Build GoShimmer image" docker build -t iotaledger/goshimmer ../../.