图像的大小golang
和alpine
相差约300Mb
。
使用golang
映像而不是纯粹的alpine
有什么优点?
图像的大小golang
和alpine
相差约300Mb
。
使用golang
映像而不是纯粹的alpine
有什么优点?
golang:alpine
和alpine
之间的差异更加公平。golang
镜像基于Debian构建,这与Alpine不同。 我将引用来自Docker Hub的文档:
和
golang:<version>
这是事实上的镜像。如果您不确定您的需求是什么,那么您可能想使用它。它旨在被用作可丢弃容器(挂载源代码并启动容器以启动应用程序),以及作为构建其他镜像的基础。
golang
版本构建二进制文件,并使用golang:alpine
、alpine
或如上面的评论中提到的scratch
部署到生产环境中。alpine
镜像,而在开发和构建镜像时使用 golang;alpine
镜像? - Shettyh为什么不选择 scratch?
您可以构建一个静态的 Go 二进制文件,并将其复制到 Docker 镜像中。
Docker 镜像的大小将等于二进制文件的大小。
假设您的 Go 二进制文件名为 main_go,这是您需要的 Dockerfile:
FROM centurylink/ca-certs
ADD main_go /
CMD ["/main_go"]
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main_go .
ARG GO_VERSION=1.15.6
# STAGE 1: building the executable
FROM golang:${GO_VERSION}-alpine AS build
RUN apk add --no-cache git
RUN apk --no-cache add ca-certificates
# add a user here because addgroup and adduser are not available in scratch
RUN addgroup -S myapp \
&& adduser -S -u 10000 -g myapp myapp
WORKDIR /src
COPY ./go.mod ./go.sum ./
RUN go mod download
COPY ./ ./
# Run tests
RUN CGO_ENABLED=0 go test -timeout 30s -v github.com/gbaeke/go-template/pkg/api
# Build the executable
RUN CGO_ENABLED=0 go build \
-installsuffix 'static' \
-o /app ./cmd/app
# STAGE 2: build the container to run
FROM scratch AS final
LABEL maintainer="gbaeke"
COPY --from=build /app /app
# copy ca certs
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
# copy users from builder (use from=0 for illustration purposes)
COPY --from=0 /etc/passwd /etc/passwd
USER myapp
ENTRYPOINT ["/app"]
More info can be found here.
scratch
和 centurylink/ca-certs
的选择很好。但是如果你使用了 import "C"
或者使用了一个包装 C 库的库,那么你就不能真正使用 scratch
了吧? - Michael Hampton
scratch
。 - Mattgolang:alpine
。 - Roman Kiselenkogolang:alpine
,但比基本的alpine
多了约200Mb。只是想知道使用golang镜像的优势是什么。 - Shettyhgo
语言工具(以及许多其他工具,如git
或bash
),这些工具在普通的alpine docker镜像中不可用。这里有一个关于__小型__docker镜像的好帖子。我认为你的问题可能在这里不太相关。 - Roman Kiselenko