Skip to content

Commit

Permalink
chore: statically build in docker
Browse files Browse the repository at this point in the history
  • Loading branch information
hacheigriega committed Oct 26, 2023
1 parent 5e50913 commit 87595d8
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 6 deletions.
21 changes: 15 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -279,4 +274,18 @@ release-snapshot:
--skip-validate\
--skip-publish

.PHONY: release release-dry-run release-snapshot
.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 .
73 changes: 73 additions & 0 deletions static.Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]

0 comments on commit 87595d8

Please sign in to comment.