Skip to content

Commit

Permalink
add localcross.Dockerfile and use it when --local and -b flags are pr…
Browse files Browse the repository at this point in the history
…ovided (#161)
  • Loading branch information
agouin authored Aug 28, 2023
1 parent 212c0c9 commit 7d95b96
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 1 deletion.
4 changes: 3 additions & 1 deletion builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ func rawDockerfile(

case DockerfileTypeCosmos:
if local {
// local builds always use embedded Dockerfile.
if useBuildKit {
return dockerfileEmbeddedOrLocal("cosmos/localcross.Dockerfile", dockerfile.CosmosLocalCross)
}
return dockerfile.CosmosLocal
}
if useBuildKit {
Expand Down
174 changes: 174 additions & 0 deletions dockerfile/cosmos/localcross.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
ARG BASE_VERSION
FROM --platform=$BUILDPLATFORM golang:${BASE_VERSION} AS build-env

RUN apk add --update --no-cache curl make git libc-dev bash gcc linux-headers eudev-dev

ARG TARGETARCH
ARG BUILDARCH

RUN if [ "${TARGETARCH}" = "arm64" ] && [ "${BUILDARCH}" != "arm64" ]; then\
wget -c https://musl.cc/aarch64-linux-musl-cross.tgz -O - | tar -xzvv --strip-components 1 -C /usr;\
elif [ "${TARGETARCH}" = "amd64" ] && [ "${BUILDARCH}" != "amd64" ]; then\
wget -c https://musl.cc/x86_64-linux-musl-cross.tgz -O - | tar -xzvv --strip-components 1 -C /usr;\
fi

ARG GITHUB_ORGANIZATION
ARG REPO_HOST

WORKDIR /go/src/${REPO_HOST}/${GITHUB_ORGANIZATION}

ARG GITHUB_REPO
ARG VERSION
ARG BUILD_TIMESTAMP

ADD . .

WORKDIR /go/src/${REPO_HOST}/${GITHUB_ORGANIZATION}/${GITHUB_REPO}

ARG BUILD_TARGET
ARG BUILD_ENV
ARG BUILD_TAGS
ARG PRE_BUILD
ARG BUILD_DIR

RUN set -eux;\
LIBDIR=/lib;\
if [ "${TARGETARCH}" = "arm64" ]; then\
export ARCH=aarch64;\
if [ "${BUILDARCH}" != "arm64" ]; then\
LIBDIR=/usr/aarch64-linux-musl/lib;\
mkdir -p $LIBDIR;\
export CC=aarch64-linux-musl-gcc CXX=aarch64-linux-musl-g++;\
fi;\
elif [ "${TARGETARCH}" = "amd64" ]; then\
export ARCH=x86_64;\
if [ "${BUILDARCH}" != "amd64" ]; then\
LIBDIR=/usr/x86_64-linux-musl/lib;\
mkdir -p $LIBDIR;\
export CC=x86_64-linux-musl-gcc CXX=x86_64-linux-musl-g++;\
fi;\
fi;\
WASM_VERSION=$(go list -m all | grep github.com/CosmWasm/wasmvm | awk '{print $NF}');\
if [ ! -z "${WASM_VERSION}" ]; then\
wget -O $LIBDIR/libwasmvm_muslc.a https://github.com/CosmWasm/wasmvm/releases/download/${WASM_VERSION}/libwasmvm_muslc.$ARCH.a;\
fi;\
export GOOS=linux GOARCH=$TARGETARCH CGO_ENABLED=1 LDFLAGS='-linkmode external -extldflags "-static"';\
if [ ! -z "$PRE_BUILD" ]; then sh -c "${PRE_BUILD}"; fi;\
if [ ! -z "$BUILD_TARGET" ]; then\
if [ ! -z "$BUILD_ENV" ]; then export ${BUILD_ENV}; fi;\
if [ ! -z "$BUILD_TAGS" ]; then export "${BUILD_TAGS}"; fi;\
if [ ! -z "$BUILD_DIR" ]; then cd "${BUILD_DIR}"; fi;\
sh -c "${BUILD_TARGET}";\
fi

RUN if [ -d "/go/bin/linux_${TARGETARCH}" ]; then mv /go/bin/linux_${TARGETARCH}/* /go/bin/; fi

# Copy all binaries to /root/bin, for a single place to copy into final image.
# If a colon (:) delimiter is present, binary will be renamed to the text after the delimiter.
RUN mkdir /root/bin
ARG RACE
ARG BINARIES
ENV BINARIES_ENV ${BINARIES}
RUN bash -c 'set -eux;\
BINARIES_ARR=();\
IFS=, read -ra BINARIES_ARR <<< "$BINARIES_ENV";\
for BINARY in "${BINARIES_ARR[@]}"; do\
BINSPLIT=();\
IFS=: read -ra BINSPLIT <<< "$BINARY";\
BINPATH=${BINSPLIT[1]+"${BINSPLIT[1]}"};\
BIN="$(eval "echo "${BINSPLIT[0]+"${BINSPLIT[0]}"}"")";\
if [ ! -z "$RACE" ] && GOVERSIONOUT=$(go version -m $BIN); then\
if echo $GOVERSIONOUT | grep build | grep "-race=true"; then\
echo "Race detection is enabled in binary";\
else\
echo "Race detection not enabled in binary!";\
exit 1;\
fi;\
fi;\
if [ ! -z "$BINPATH" ]; then\
if [[ $BINPATH == *"/"* ]]; then\
mkdir -p "$(dirname "${BINPATH}")";\
cp "$BIN" "${BINPATH}";\
else\
cp "$BIN" "/root/bin/${BINPATH}";\
fi;\
else\
cp "$BIN" /root/bin/;\
fi;\
done'

RUN mkdir -p /root/lib
ARG LIBRARIES
ENV LIBRARIES_ENV ${LIBRARIES}
RUN bash -c 'set -eux;\
LIBRARIES_ARR=($LIBRARIES_ENV); for LIBRARY in "${LIBRARIES_ARR[@]}"; do cp $LIBRARY /root/lib/; done'

# Use minimal busybox from infra-toolkit image for final scratch image
FROM ghcr.io/strangelove-ventures/infra-toolkit:v0.0.7 AS infra-toolkit
RUN addgroup --gid 1025 -S heighliner && adduser --uid 1025 -S heighliner -G heighliner

# Use ln and rm from full featured busybox for assembling final image
FROM busybox:1.34.1-musl AS busybox-full

# Build final image from scratch
FROM scratch

LABEL org.opencontainers.image.source="https://github.com/strangelove-ventures/heighliner"

WORKDIR /bin

# Install ln (for making hard links) and rm (for cleanup) from full busybox image (will be deleted, only needed for image assembly)
COPY --from=busybox-full /bin/ln /bin/rm ./

# Install minimal busybox image as shell binary (will create hardlinks for the rest of the binaries to this data)
COPY --from=infra-toolkit /busybox/busybox /bin/sh

# Install jq
COPY --from=infra-toolkit /usr/local/bin/jq /bin/

# Add hard links for read-only utils
# Will then only have one copy of the busybox minimal binary file with all utils pointing to the same underlying inode
RUN for b in \
cat \
date \
df \
du \
env \
grep \
head \
less \
ls \
md5sum \
pwd \
sha1sum \
sha256sum \
sha3sum \
sha512sum \
sleep \
stty \
tail \
tar \
tee \
tr \
watch \
which \
; do ln sh $b; done

# Remove write utils
RUN rm ln rm

# Install chain binaries
COPY --from=build-env /root/bin /bin

# Install libraries
COPY --from=build-env /root/lib /lib

# Install trusted CA certificates
COPY --from=infra-toolkit /etc/ssl/cert.pem /etc/ssl/cert.pem

# Install heighliner user
COPY --from=infra-toolkit /etc/passwd /etc/passwd
COPY --from=infra-toolkit --chown=1025:1025 /home/heighliner /home/heighliner

WORKDIR /home/heighliner
USER heighliner
3 changes: 3 additions & 0 deletions dockerfile/dockerfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ var CosmosNative []byte
//go:embed cosmos/local.Dockerfile
var CosmosLocal []byte

//go:embed cosmos/localcross.Dockerfile
var CosmosLocalCross []byte

//go:embed imported/Dockerfile
var Imported []byte

Expand Down

0 comments on commit 7d95b96

Please sign in to comment.