davidbyttow / govips

A lightning fast image processing and resizing library for Go
MIT License
1.31k stars 203 forks source link

Building with -extldflags=-static #339

Open Janso123 opened 1 year ago

Janso123 commented 1 year ago

Hi, Is there an option to build it with a static flag?

That's my Dockerfile and it core-dumps on me or throws linker error `FROM golang:alpine as builder

RUN apk add musl-dev vips-dev gcc

WORKDIR /src COPY . .

RUN go build -o /bin/app --ldflags '-linkmode external -extldflags=-static' . `

sonu27 commented 1 year ago

@Janso123 did you manage to solve it? I have the same issue

sonu27 commented 1 year ago

@AttilaTheFun @tonimelisma have you guys been able to build a static go binary with libvips?

/usr/local/go/pkg/tool/linux_arm64/link: running gcc failed: exit status 1
/usr/lib/gcc/aarch64-alpine-linux-musl/12.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: cannot find -lvips: No such file or directory
/usr/lib/gcc/aarch64-alpine-linux-musl/12.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: cannot find -lgio-2.0: No such file or directory
/usr/lib/gcc/aarch64-alpine-linux-musl/12.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: cannot find -lgobject-2.0: No such file or directory
/usr/lib/gcc/aarch64-alpine-linux-musl/12.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: cannot find -lglib-2.0: No such file or directory
/usr/lib/gcc/aarch64-alpine-linux-musl/12.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: cannot find -lintl: No such file or directory
tonimelisma commented 1 year ago

Hey @sonu27 I've never tried it personally.

g-mero commented 1 year ago

Same issue.

moguchev commented 1 year ago

The same issue

moguchev commented 1 year ago

Here is my Dockerfile:

FROM --platform=$BUILDPLATFORM golang:1.21 as builder

RUN apt-get update && apt-get install -y gcc-aarch64-linux-gnu gcc-x86-64-linux-gnu && apt-get clean
RUN dpkg --add-architecture arm64 && dpkg --add-architecture amd64

ARG TARGETOS TARGETARCH

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY main.go /app/main.go

RUN apt-get update && apt-get install --no-install-recommends -y libvips libvips-dev

RUN --mount=type=cache,target=/root/.cache/go-build \
    --mount=type=cache,target=/go/pkg \
    if [ "${TARGETARCH}" = "arm64" ]; then CC=aarch64-linux-gnu-gcc && CC_FOR_TARGET=gcc-aarch64-linux-gnu; \
    elif [ "${TARGETARCH}" = "amd64" ]; then CC=x86_64-linux-gnu-gcc && CC_FOR_TARGET=gcc-x86-64-linux-gnu; fi && \
    CGO_ENABLED=1 GOOS=linux GOARCH=${TARGETARCH} CC=$CC CC_FOR_TARGET=$CC_FOR_TARGET go build -a --ldflags '-linkmode external -extldflags "-static" -s -w' -o output/main main.go

FROM alpine:latest
WORKDIR /root
COPY --from=builder /app/output/main .
CMD /root/main

BUILDPLATFORM=linux/amd64:

17.10 /usr/lib/gcc-cross/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/bin/ld: cannot find -lvips: No such file or directory
17.10 /usr/lib/gcc-cross/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/bin/ld: cannot find -lgio-2.0: No such file or directory
17.10 /usr/lib/gcc-cross/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/bin/ld: cannot find -lgobject-2.0: No such file or directory
17.10 /usr/lib/gcc-cross/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/bin/ld: cannot find -lglib-2.0: No such file or directory
17.10 collect2: error: ld returned 1 exit status

BUILDPLATFORM=linux/arm64/v8:

18.55 /usr/local/go/pkg/tool/linux_arm64/link: running aarch64-linux-gnu-gcc failed: exit status 1
18.55 /usr/bin/ld: cannot find -lvips: No such file or directory
18.55 /usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/12/../../../aarch64-linux-gnu/libglib-2.0.a(gutils.c.o): in function `g_get_user_database_entry':
18.55 (.text+0x28c): warning: Using 'getpwuid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
18.55 /usr/bin/ld: (.text+0xe8): warning: Using 'getpwnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
18.55 /usr/bin/ld: (.text+0x11c): warning: Using 'getpwuid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
18.55 collect2: error: ld returned 1 exit status

Without options --ldflags '-linkmode external -extldflags "-static" -s -w' the same errors. It seems I'm doing something wrong...

omar391 commented 10 months ago

Did you try with musl libc?

try this one: https://github.com/davidbyttow/govips/issues/376#issue-1832134159

moguchev commented 10 months ago

Yes. It's seems that static build is impossible (Or some magic should happen). Without static flag everything is fine.

Here is my final Docker file (build is working on MacOS Sonoma 14.1.1 with M1 and also on Ubuntu):

#################################
# STEP 1. Build executable binary
#################################
FROM --platform=linux/amd64 golang:1.21-alpine3.18 AS build

ARG TARGETOS TARGETARCH
ARG BUILDPLATFORM TARGETPLATFORM

RUN echo "I am running on $(uname -m), building for $TARGETPLATFORM, OS=$TARGETOS, ARCH=$TARGETARCH"

# Download gcc (cross-compilers) for CGO
RUN apk update && apk add build-base

# Install required libraries for libvips and deps
RUN apk update && \
    apk add \
    pkgconfig \
    vips-dev

WORKDIR /src

# Download necessary Go modules
COPY go.mod .
COPY go.sum .
RUN CGO_ENABLED=1 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go mod download

# Copy all files
COPY . .

# Build go binary
ENV CC=gcc
ENV GOOS=linux
ENV GOARCH=amd64
ENV CGO_ENABLED=1
RUN go build -v -a -tags vips --ldflags="-s -w -linkmode external" -o /bin/facade -v ./cmd/facade
# CGO_ENABLED=1 GOOS=${TARGETOS} GOARCH=${TARGETARCH} CC=gcc 
# #############################
# # STEP 2. Build a small image
# #############################
FROM --platform=linux/amd64 alpine:3.18.0 AS final

# Add SSL certs
ADD certs/root.crt /etc/ssl/certs/
ADD certs/root.crt ~/.postgresql/
ADD certs/root.crt ~/.opensearch/

# Copy builded libraries from build image
COPY --from=build /bin/facade /facade

RUN apk update && \
    apk add \
    glib \
    libexpat \
    libwebp \
    libwebp-tools \
    libpng \
    libjpeg-turbo \
    vips \
    file 

RUN file /facade 
RUN ldd /facade

EXPOSE 8080
EXPOSE 8082

CMD /facade
aisnote commented 8 months ago

same issue on windows native building, it worked on libvips-8.10, but can not worked on 8.14. Anyone has idea? C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgio-2.0: No such file or directory C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lintl: No such file or directory C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgmodule-2.0: No such file or directory C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lintl: No such file or directory C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lintl: No such file or directory