noborus / trdsql

CLI tool that can execute SQL queries on CSV, LTSV, JSON, YAML and TBLN. Can output to various formats.
https://noborus.github.io/trdsql/
MIT License
1.96k stars 73 forks source link

build Dockerfile with multi-stage ? It also supports ARM64 #166

Closed opvexe closed 2 years ago

opvexe commented 2 years ago

this is my dockerfile for test.

But the mirror image is still huge, about 800M

FROM --platform=$BUILDPLATFORM golang:1.16 as builder
ENV TZ=Asia/Shanghai LANG="C.UTF-8"
ARG TARGETARCH
ARG TARGETOS

WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
ENV GO111MODULE=on
ENV GOPROXY=https://goproxy.io,direct

# Copy the go source
COPY query query

# Build
RUN CGO_ENABLED=1 GOOS=$TARGETOS GOARCH=$TARGETARCH go build --tags='json1' -a -o queryd query/cmd/main.go

FROM golang:1.16-buster
WORKDIR /
COPY --from=builder /workspace/queryd .
ENTRYPOINT ["/queryd"]
noborus commented 2 years ago

Thank you for the issue. I have no solution, so I need someone's help.

opvexe commented 2 years ago

I tried to use this method, and this image typed out at 80M .. I'm still testing whether the ARM64 mirror works?

FROM --platform=$BUILDPLATFORM golang:1.16 as builder
ENV TZ=Asia/Shanghai LANG="C.UTF-8"
ARG TARGETARCH
ARG TARGETOS
RUN apt-get update && apt-get install -y gcc-aarch64-linux-gnu

WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
ENV GO111MODULE=on
ENV GOPROXY=https://goproxy.io,direct

# Copy the go source
COPY query query

# Build
# RUN CGO_ENABLED=1 GOOS=$TARGETOS GOARCH=$TARGETARCH go build --tags='json1' -a -o queryd query/cmd/main.go

# Recommended to use
RUN if [ "$TARGETARCH" = "arm64" ]; then CC=aarch64-linux-gnu-gcc && CC_FOR_TARGET=gcc-aarch64-linux-gnu; fi && \
  CGO_ENABLED=1 GOOS=$TARGETOS GOARCH=$TARGETARCH CC=$CC CC_FOR_TARGET=$CC_FOR_TARGET go build --tags='json1' -a -ldflags '-extldflags "-static"' -o queryd query/cmd/main.go

FROM --platform=$BUILDPLATFORM debian:stable-slim
WORKDIR /
COPY --from=builder /workspace/queryd .
ENTRYPOINT ["/queryd"]