Skip to content

Commit

Permalink
add arm64 support
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchristianschroeter committed Nov 5, 2024
1 parent 660f9ca commit d04d258
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 58 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ jobs:
strategy:
matrix:
feature: [aws, azure, gcloud, minio, rclone]
platform:
- linux/amd64
- linux/arm64
include:
- feature: aws
arg: EN_AWS_CLI=true
Expand Down Expand Up @@ -66,7 +63,7 @@ jobs:
push: ${{ github.event_name != 'pull_request' }}
tags: |
danielschroeter/mgob:${{ needs.set-build-env.outputs.buildId }}-${{ matrix.feature }}
platforms: ${{ matrix.platform }}
platforms: linux/amd64,linux/arm64
build-args: |
BUILDKIT_MULTI_PLATFORM=1
BUILD_DATE=${{ needs.set-build-env.outputs.now }}
Expand Down
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ ARG EN_RCLONE=false
ARG VERSION

# Stage 1: tools-builder stage for MongoDB tools
FROM --platform=$BUILDPLATFORM danielschroeter/mongo-tool:${MONGODB_TOOLS_VERSION} AS tools-builder
FROM danielschroeter/mongo-tool:${MONGODB_TOOLS_VERSION} AS tools-builder

# Stage 2: mgob-builder stage for the mgob binary
FROM --platform=$BUILDPLATFORM golang:1.21 AS mgob-builder
FROM golang:1.21 AS mgob-builder
ARG VERSION
ARG TARGETOS
ARG TARGETARCH
Expand All @@ -25,7 +25,7 @@ RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go test ./pkg/... && \
CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags "-X main.version=$VERSION" -a -installsuffix cgo -o mgob github.com/stefanprodan/mgob/cmd/mgob

# Stage 3: final image setup with Alpine
FROM --platform=$BUILDPLATFORM alpine:3.18
FROM alpine:3.20
ARG BUILD_DATE
ARG VCS_REF
ARG VERSION
Expand Down Expand Up @@ -65,7 +65,7 @@ ENV PATH="/google-cloud-sdk/bin:${PATH}"
COPY --from=mgob-builder /go/src/github.com/stefanprodan/mgob/mgob .

# Copy MongoDB tools from the correct path
COPY --from=tools-builder /usr/bin/* /usr/bin/
COPY --from=tools-builder /usr/local/bin/* /usr/bin/

# Volumes for storage
VOLUME ["/storage", "/tmp", "/data"]
Expand Down
77 changes: 27 additions & 50 deletions mongo-tool/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,59 +1,36 @@
# Stage 1: Build MongoDB tools on Ubuntu
FROM --platform=$BUILDPLATFORM ubuntu:22.04 AS mongo-tools-builder
# Use a specific version of the Go Alpine image
FROM golang:1.22.8-alpine3.20 AS builder

# Set MongoDB tools version as a build argument
# Build arguments
ARG MONGODB_TOOLS_VERSION=100.8.0
ARG TARGETOS
ARG TARGETARCH

# Install build dependencies
RUN apt-get update && \
apt-get install -y --no-install-recommends \
git \
build-essential \
libkrb5-dev \
golang-go \
ca-certificates && \
rm -rf /var/lib/apt/lists/*

# Set environment variables for Go
ENV GOPATH=/go
ENV PATH=$GOPATH/bin:/usr/local/go/bin:$PATH

# Clone the MongoDB tools repository
RUN git clone https://github.com/mongodb/mongo-tools.git --depth 1 -b $MONGODB_TOOLS_VERSION /go/mongo-tools

# Set working directory
WORKDIR /go/mongo-tools

# List of MongoDB tools to build
ENV TOOLS="bsondump mongoimport mongoexport mongodump mongorestore mongostat mongofiles mongotop"

# Conditionally build tools for each architecture
RUN for tool in $TOOLS; do \
if [ "$TARGETARCH" = "amd64" ]; then \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o /build/$tool -ldflags "-X main.VersionStr=$MONGODB_TOOLS_VERSION -X main.GitCommit=$(git rev-parse HEAD)" -tags "ssl sasl gssapi failpoints" $tool/main/$tool.go; \
elif [ "$TARGETARCH" = "arm64" ]; then \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o /build/$tool -ldflags "-X main.VersionStr=$MONGODB_TOOLS_VERSION -X main.GitCommit=$(git rev-parse HEAD)" -tags "ssl sasl failpoints" $tool/main/$tool.go; \
else \
echo "Unsupported architecture: $TARGETARCH"; \
exit 1; \
fi; \
done

# Stage 2: Create the final lightweight image with MongoDB tools
FROM alpine:3.18
# Set environment variables for Go build
ENV GOCACHE="/go-cache" \
GOTMPDIR="/tmp" \
GOMODCACHE="/go/pkg/mod"

# Install only necessary libraries for MongoDB tools to run
RUN apk add --no-cache \
libc6-compat \
krb5-libs \
ca-certificates
# Install dependencies and clone the repository
RUN apk add --no-cache git build-base krb5-dev && \
git clone https://github.com/mongodb/mongo-tools.git /src/mongo-tools --branch v$MONGODB_TOOLS_VERSION --depth 1

# Copy the MongoDB tools from the build stage
COPY --from=mongo-tools-builder /build/* /usr/bin/
WORKDIR /src/mongo-tools

# Verify the installation by displaying the version of each tool
RUN for tool in bsondump mongoimport mongoexport mongodump mongorestore mongostat mongofiles mongotop; do \
/usr/bin/$tool --version || echo "$tool not found"; \
# Build MongoDB tools
RUN mkdir -p bin && \
for bin in bsondump mongodump mongoexport mongofiles mongoimport mongorestore mongostat mongotop; do \
OOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o bin/$bin \
-ldflags="-X main.VersionStr=$MONGODB_TOOLS_VERSION -X main.GitCommit=alpine" \
-tags "ssl sasl gssapi failpoints" \
$bin/main/$bin.go; \
done

# You might want to use a smaller image for the final stage
FROM alpine:3.20

# Copy the built binaries from the builder
COPY --from=builder /src/mongo-tools/bin/* /usr/local/bin/

# Ensure the binaries are executable
RUN chmod +x /usr/local/bin/*

0 comments on commit d04d258

Please sign in to comment.