无法在多阶段构建中构建go程序,Docker中出现未定义的错误

mdfafbf1  于 2023-08-01  发布在  Go
关注(0)|答案(1)|浏览(88)

我正在努力学习围棋,我已经在一些简单的东西上构建了我的第一个围棋程序。

❯ tree .
.
├── README.md
├── convert.go
├── dockerfile
├── go.mod
├── go.sum
├── main.go
├── pageindices.go
├── pageindices_test.go
├── pdf2png.go
└── pdf2png_test.go

字符串
我的docker文件看起来像下面这样

ARG GOLANG_IMAGE_TAG="1.20.6-alpine"
FROM golang:${GOLANG_IMAGE_TAG} as base

ARG PDF2IMG_VERSION=dev

# So using 8.14.2 for now
ARG VIPS_VERSION="8.14.2"

RUN apk update && apk add --no-cache \
     automake build-base pkgconfig glib-dev expat-dev gobject-introspection \
     libjpeg-turbo-dev libpng-dev libwebp-dev giflib-dev librsvg-dev libexif-dev lcms2-dev
# Exit 0 added because warnings tend to exit the build at a non-zero status
RUN cd /root \
    && mkdir vips \
    && wget -q0- "https://github.com/libvips/libvips/releases/download/v${VIPS_VERSION}/vips-${VIPS_VERSION}.tar.gz" | tar -xzf - -C vips --strip-components=1 \
    && cd vips \
    && CFLAGS="-g -O3" CXXFLAGS="-D_GLIBCXX_USE_CXX11_ABI=0 -g -O3" ./configure \
        --disable-debug \
        --disable-dependency-tracking \
        --disable-introspection \
        --disable-static \
        --enable-gtk-doc-html=no \
        --enable-gtk-doc=no \
        --enable-pyvips8=no && \
    make && make install-strip && ldconfig; exit 0
RUN export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/vips/lib
RUN apk del automake build-base

# create a working directory inside the image
WORKDIR /go/src/github.com/felixgao/pdf2img
# Cache go modules
ENV GO111MODULE=on

# Copy go mod and sum files
COPY go.mod go.sum ./

# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
RUN go mod download

# copy directry files i.e all files ending with .go 
COPY *.go .

# Build the Go app with ldflags
RUN CGO_ENABLED=0 GOOS=linux go build -a \
    -ldflags="-s -w -h -X main.Version=${PDF2IMG_VERSION}" \
    -o pdf2png

# build target image
FROM golang:${GOLANG_IMAGE_TAG} as prod
WORKDIR /app/
RUN apk --update add --no-cache \
    fftw glib expat libjpeg-turbo libpng \
    libwebp giflib librsvg libgsf libexif lcms2
WORKDIR /root/
COPY --from=base /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=base /root/libs/* /root/libs/
COPY --from=base /usr/local/lib/* /usr/local/lib/
COPY --from=base /go/src/github.com/felixgao/pdf2img/pdf2png .

#tell docker what port to expose
EXPOSE 8080

# Run the binary program produced by `go install`
CMD ["./pdf2png"]


我的go.mod文件看起来像

module github.com/felixgao/pdf_to_png

go 1.20

require github.com/davidbyttow/govips/v2 v2.13.0

require (
    golang.org/x/image v0.9.0 // indirect
    golang.org/x/net v0.12.0 // indirect
    golang.org/x/text v0.11.0 // indirect
)


当我构建它的时候,它一直告诉我所需的包不在那里。我以为去国防部下载会拉下来给我。我错过了什么吗?

=> CACHED [base  6/10] WORKDIR /go/src/github.com/felixgao/pdf2img                    0.0s
 => CACHED [base  7/10] COPY go.mod go.sum ./                                          0.0s
 => [base  8/10] RUN go mod download                                                   6.0s
 => [base  9/10] COPY *.go .                                                           0.0s
 => ERROR [base 10/10] RUN CGO_ENABLED=0 GOOS=linux go build -a     -ldflags="-s -w -  4.5s
------
 > [base 10/10] RUN CGO_ENABLED=0 GOOS=linux go build -a     -ldflags="-s -w -h -X main.Version=dev"     -o pdf2png:
#18 4.394 # github.com/felixgao/pdf_to_png
#18 4.394 ./pdf2png.go:29:29: undefined: vips.ImageType
#18 4.394 ./pdf2png.go:30:7: undefined: vips.ImageTypeJPEG
#18 4.394 ./pdf2png.go:31:7: undefined: vips.ImageTypePNG
#18 4.394 ./pdf2png.go:32:7: undefined: vips.ImageTypeTIFF
#18 4.394 ./pdf2png.go:35:41: undefined: vips.ImageType
#18 4.394 ./pdf2png.go:36:15: undefined: vips.ImageTypeJPEG
#18 4.394 ./pdf2png.go:37:15: undefined: vips.ImageTypePNG
#18 4.394 ./pdf2png.go:38:15: undefined: vips.ImageTypeTIFF
#18 4.394 ./pdf2png.go:41:25: undefined: vips.ImageRef
#18 4.394 ./pdf2png.go:41:86: undefined: vips.ImageMetadata
#18 4.394 ./pdf2png.go:41:86: too many errors
------
executor failed running [/bin/sh -c CGO_ENABLED=0 GOOS=linux go build -a     -ldflags="-s -w -h -X main.Version=${PDF2IMG_VERSION}"     -o pdf2png]: exit code: 1pdf2png]: exit code: 1


当在本地运行build命令时,它工作正常。

❯ go build -a -ldflags="-s -w -h -X main.Version=dev" -o pdf2png && echo $?
0
❯ ls -al pdf2png
-rwxr-xr-x  1 ggao  staff  6334226 Jul 18 17:52 pdf2png


我希望go mod下载将拉下必要的包,并允许构建通过,因为它是在我的本地传递,而不使用docker。

ndasle7k

ndasle7k1#

问题是go mod tidy。Dockerfile在任何Go源文件被复制到构建容器之前运行此命令。所以Go通过删除 * 所有 * 依赖项来积极地整理go.mod。
在这种情况下,我认为您可以删除go mod tidy。特别是因为这不是Dockerfile中的最终构建阶段。

相关问题