From 87595d8f9bfb8638cc8708b19cec4e215b378178 Mon Sep 17 00:00:00 2001 From: Hyoung-yoon Kim Date: Thu, 26 Oct 2023 14:14:16 -0400 Subject: [PATCH 1/3] chore: statically build in docker --- Makefile | 21 ++++++++++---- static.Dockerfile | 73 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 static.Dockerfile diff --git a/Makefile b/Makefile index 85e5d907..936e7525 100644 --- a/Makefile +++ b/Makefile @@ -120,11 +120,6 @@ build-experimental: go.sum @echo "--> Building Experimental version..." EXPERIMENTAL=true $(MAKE) build -build-no_cgo: - @echo "--> Building static binary with no CGO nor GLIBC dynamic linking..." - CGO_ENABLED=0 CGO_LDFLAGS="-static" $(MAKE) build - - go-mod-tidy: @contrib/scripts/go-mod-tidy-all.sh @@ -279,4 +274,18 @@ release-snapshot: --skip-validate\ --skip-publish -.PHONY: release release-dry-run release-snapshot \ No newline at end of file +.PHONY: release release-dry-run release-snapshot + +############################################################################### +### Docker ### +############################################################################### +RUNNER_BASE_IMAGE_DISTROLESS := gcr.io/distroless/static-debian11 + +docker-build: + @DOCKER_BUILDKIT=1 docker build \ + -t seda-chaind \ + --build-arg GO_VERSION=$(GO_VERSION) \ + --build-arg RUNNER_IMAGE=$(RUNNER_BASE_IMAGE_DISTROLESS) \ + --build-arg GIT_VERSION=$(VERSION) \ + --build-arg GIT_COMMIT=$(COMMIT) \ + -f static.Dockerfile . diff --git a/static.Dockerfile b/static.Dockerfile new file mode 100644 index 00000000..471f3fe7 --- /dev/null +++ b/static.Dockerfile @@ -0,0 +1,73 @@ +# syntax=docker/dockerfile:1 + +ARG GO_VERSION="1.20" +ARG RUNNER_IMAGE="gcr.io/distroless/static-debian11" + +# -------------------------------------------------------- +# Builder +# -------------------------------------------------------- + +FROM golang:${GO_VERSION}-alpine as builder + +ARG GIT_VERSION +ARG GIT_COMMIT + +RUN apk add --no-cache \ + ca-certificates \ + build-base \ + linux-headers + +# Download go dependencies +WORKDIR /seda-chain +COPY go.mod go.sum ./ +RUN --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/root/go/pkg/mod \ + go mod download + +# Cosmwasm - Download correct libwasmvm version +RUN ARCH=$(uname -m) && WASMVM_VERSION=$(go list -m github.com/CosmWasm/wasmvm | sed 's/.* //') && \ + wget https://github.com/CosmWasm/wasmvm/releases/download/$WASMVM_VERSION/libwasmvm_muslc.$ARCH.a \ + -O /lib/libwasmvm_muslc.a && \ + # verify checksum + wget https://github.com/CosmWasm/wasmvm/releases/download/$WASMVM_VERSION/checksums.txt -O /tmp/checksums.txt && \ + sha256sum /lib/libwasmvm_muslc.a | grep $(cat /tmp/checksums.txt | grep libwasmvm_muslc.$ARCH | cut -d ' ' -f 1) + +# Copy the remaining files +COPY . . + +# Build seda-chaind binary +RUN --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/root/go/pkg/mod \ + GOWORK=off go build \ + -mod=readonly \ + -tags "netgo,ledger,muslc" \ + -ldflags \ + "-X github.com/cosmos/cosmos-sdk/version.Name="seda-chain" \ + -X github.com/cosmos/cosmos-sdk/version.AppName="seda-chaind" \ + -X github.com/cosmos/cosmos-sdk/version.Version=${GIT_VERSION} \ + -X github.com/cosmos/cosmos-sdk/version.Commit=${GIT_COMMIT} \ + -X github.com/cosmos/cosmos-sdk/version.BuildTags=netgo,ledger,muslc \ + -w -s -linkmode=external -extldflags '-Wl,-z,muldefs -static'" \ + -trimpath \ + -o /seda-chain/build/seda-chaind \ + /seda-chain/cmd/seda-chaind/main.go + +# -------------------------------------------------------- +# Runner +# -------------------------------------------------------- + +FROM ${RUNNER_IMAGE} + +COPY --from=builder /seda-chain/build/seda-chaind /bin/seda-chaind + +ENV HOME /seda-chain +WORKDIR $HOME + +EXPOSE 26656 +EXPOSE 26657 +EXPOSE 1317 +# Note: uncomment the line below if you need pprof +# We disable it by default in out main Dockerfile for security reasons +# EXPOSE 6060 + +ENTRYPOINT ["seda-chaind"] From 2174fb6ec1a7e17041d419e41f2728fc3d19e9e2 Mon Sep 17 00:00:00 2001 From: Hyoung-yoon Kim Date: Thu, 26 Oct 2023 15:06:00 -0400 Subject: [PATCH 2/3] chore: add static build on alpine --- Makefile | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 936e7525..0f0bb5e4 100644 --- a/Makefile +++ b/Makefile @@ -280,12 +280,22 @@ release-snapshot: ### Docker ### ############################################################################### RUNNER_BASE_IMAGE_DISTROLESS := gcr.io/distroless/static-debian11 +RUNNER_BASE_IMAGE_ALPINE := alpine:3.17 -docker-build: +docker-static-build: @DOCKER_BUILDKIT=1 docker build \ - -t seda-chaind \ + -t seda-chain/seda-chaind-static-distroless \ --build-arg GO_VERSION=$(GO_VERSION) \ --build-arg RUNNER_IMAGE=$(RUNNER_BASE_IMAGE_DISTROLESS) \ --build-arg GIT_VERSION=$(VERSION) \ --build-arg GIT_COMMIT=$(COMMIT) \ -f static.Dockerfile . + +docker-static-build-alpine: + @DOCKER_BUILDKIT=1 docker build \ + -t seda-chain/seda-chaind-static-alpine \ + --build-arg GO_VERSION=$(GO_VERSION) \ + --build-arg RUNNER_IMAGE=$(RUNNER_BASE_IMAGE_ALPINE) \ + --build-arg GIT_VERSION=$(VERSION) \ + --build-arg GIT_COMMIT=$(COMMIT) \ + -f static.Dockerfile . From 9c4fe189ef3083a750d90526e74de891d93678e7 Mon Sep 17 00:00:00 2001 From: Hyoung-yoon Kim Date: Fri, 27 Oct 2023 09:32:40 -0400 Subject: [PATCH 3/3] chore: update dockerfiles naming convention --- .github/workflows/release.yml | 3 +-- Dockerfile | 4 ++-- Makefile | 4 ++-- .../Dockerfile.goreleaser | 0 .../seda-node.Dockerfile => dockerfiles/Dockerfile.node | 0 static.Dockerfile => dockerfiles/Dockerfile.static | 7 +------ 6 files changed, 6 insertions(+), 12 deletions(-) rename dockers/goreleaser.Dockerfile => dockerfiles/Dockerfile.goreleaser (100%) rename dockers/seda-node.Dockerfile => dockerfiles/Dockerfile.node (100%) rename static.Dockerfile => dockerfiles/Dockerfile.static (92%) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 529cbbc2..2f02cf0b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -66,8 +66,7 @@ jobs: uses: docker/build-push-action@v5 with: context: . - file: ./dockers/seda-node.Dockerfile + file: ./dockerfiles/Dockerfile.node push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} - diff --git a/Dockerfile b/Dockerfile index d4b54459..4231b9aa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ -# This Dockerfile si for .gorelease purposesBuild: docker build -t seda-chaind . -# If you are looking for a Dockerfile to run a node, see dockers/seda-node.Dockerfile +# This Dockerfile is for .gorelease purposesBuild: docker build -t seda-chaind . +# If you are looking for a Dockerfile to run a node, see dockerfiles/Dockerfile.node FROM scratch ENTRYPOINT ["/seda-chaind"] COPY seda-chaind / diff --git a/Makefile b/Makefile index 0f0bb5e4..efea6fca 100644 --- a/Makefile +++ b/Makefile @@ -289,7 +289,7 @@ docker-static-build: --build-arg RUNNER_IMAGE=$(RUNNER_BASE_IMAGE_DISTROLESS) \ --build-arg GIT_VERSION=$(VERSION) \ --build-arg GIT_COMMIT=$(COMMIT) \ - -f static.Dockerfile . + -f $(CURDIR)/dockerfiles/Dockerfile.static . docker-static-build-alpine: @DOCKER_BUILDKIT=1 docker build \ @@ -298,4 +298,4 @@ docker-static-build-alpine: --build-arg RUNNER_IMAGE=$(RUNNER_BASE_IMAGE_ALPINE) \ --build-arg GIT_VERSION=$(VERSION) \ --build-arg GIT_COMMIT=$(COMMIT) \ - -f static.Dockerfile . + -f $(CURDIR)/dockerfiles/Dockerfile.static . diff --git a/dockers/goreleaser.Dockerfile b/dockerfiles/Dockerfile.goreleaser similarity index 100% rename from dockers/goreleaser.Dockerfile rename to dockerfiles/Dockerfile.goreleaser diff --git a/dockers/seda-node.Dockerfile b/dockerfiles/Dockerfile.node similarity index 100% rename from dockers/seda-node.Dockerfile rename to dockerfiles/Dockerfile.node diff --git a/static.Dockerfile b/dockerfiles/Dockerfile.static similarity index 92% rename from static.Dockerfile rename to dockerfiles/Dockerfile.static index 471f3fe7..a89671d9 100644 --- a/static.Dockerfile +++ b/dockerfiles/Dockerfile.static @@ -63,11 +63,6 @@ COPY --from=builder /seda-chain/build/seda-chaind /bin/seda-chaind ENV HOME /seda-chain WORKDIR $HOME -EXPOSE 26656 -EXPOSE 26657 -EXPOSE 1317 -# Note: uncomment the line below if you need pprof -# We disable it by default in out main Dockerfile for security reasons -# EXPOSE 6060 +EXPOSE 26656 26657 1317 9090 ENTRYPOINT ["seda-chaind"]