Skip to content

Commit

Permalink
Make docker build faster (iotaledger#1074)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
georgysavva authored Mar 18, 2021
1 parent 3fc4169 commit d60992f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
.git
.gitignore
.github/
docs/
.idea/

LICENSE
README.md
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:
name: autopeering
env:
TEST_NAME: autopeering
DOCKER_BUILDKIT: 1
runs-on: ubuntu-latest
steps:

Expand Down Expand Up @@ -46,6 +47,7 @@ jobs:
name: common
env:
TEST_NAME: common
DOCKER_BUILDKIT: 1
runs-on: ubuntu-latest
steps:

Expand Down Expand Up @@ -80,6 +82,7 @@ jobs:
name: consensus
env:
TEST_NAME: consensus
DOCKER_BUILDKIT: 1
runs-on: ubuntu-latest
steps:

Expand Down Expand Up @@ -115,6 +118,7 @@ jobs:
name: drng
env:
TEST_NAME: drng
DOCKER_BUILDKIT: 1
runs-on: ubuntu-latest
steps:

Expand Down Expand Up @@ -151,6 +155,7 @@ jobs:
name: message
env:
TEST_NAME: message
DOCKER_BUILDKIT: 1
runs-on: ubuntu-latest
steps:

Expand Down Expand Up @@ -187,6 +192,7 @@ jobs:
name: value
env:
TEST_NAME: value
DOCKER_BUILDKIT: 1
runs-on: ubuntu-latest
steps:

Expand Down Expand Up @@ -221,6 +227,7 @@ jobs:
name: faucet
env:
TEST_NAME: faucet
DOCKER_BUILDKIT: 1
runs-on: ubuntu-latest
steps:

Expand Down Expand Up @@ -255,6 +262,7 @@ jobs:
name: syncbeacon
env:
TEST_NAME: syncbeacon
DOCKER_BUILDKIT: 1
runs-on: ubuntu-latest
steps:

Expand Down Expand Up @@ -289,6 +297,7 @@ jobs:
name: mana
env:
TEST_NAME: mana
DOCKER_BUILDKIT: 1
runs-on: ubuntu-latest
steps:

Expand Down
19 changes: 12 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# syntax = docker/dockerfile:1.2.1

############################
# Build
############################
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions check_static.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions tools/docker-network/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions tools/integration-tests/runTests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ../../.

Expand Down

0 comments on commit d60992f

Please sign in to comment.