英文:
How to install particular go command in docker container?
问题
我想在 Docker 容器中使用 go mod graph
命令来生成 Go 仓库的依赖图。我只使用这个命令,不使用其他的 Go 功能。我已经尝试了其他工具,如 godegraph、gomod、dept,但没有一个比 go mod graph
更好。目前,我安装了所有的 Go 功能,并增加了大约 400MB 的 Docker 镜像大小。
问题:我可以通过在 Golang 中安装特定的命令来减小 Docker 容器的大小吗?或者我可以获取 go mod graph
的二进制文件,以减小容器的大小吗?
英文:
I want to use go mod graph
inside the docker container to generate dependency graph in go repository. I only use that command and not use the other go functionalities. I have try the other tools like godegraph, gomod, dept and nothing can do better than go mod graph
. Currently I installed all the go functionalities and add about 400MB of my docker image size.
Question : Can I reduce the size of the docker container by installing spesific command in golang ? Or can I get the binary of go mod graph
so its can reduce the size of the container?
答案1
得分: 1
如果在执行镜像(docker run)时不需要Go,只需要在构建镜像(docker build)时使用它,那么可以使用多阶段构建。以下是一个示例,来自Mike Kaperys的“使用构建器模式创建精简的Docker镜像”:
每个FROM
指令都为后续指令定义了一个新的基础镜像,并开始了一个新的构建阶段。在多阶段构建之前,可以实现相同的效果,但该过程需要两个Dockerfile。
使用多阶段构建允许您在构建阶段之间选择性地复制文件 - 这是构建器模式的基础。
可以在kaperys/blog/docker-builder-pattern
中查看完整的示例。
FROM golang:1.16
COPY . /go/src/github.com/kaperys/blog/docker-builder-pattern
WORKDIR /go/src/github.com/kaperys/blog/docker-builder-pattern
RUN go get && CGO_ENABLED=0 GOOS=linux go build -o server .
FROM scratch
LABEL maintainer="Mike Kaperys <mike@kaperys.io>"
COPY --from=0 /go/src/github.com/kaperys/blog/docker-builder-pattern/server /opt/kaperys/vision/server
COPY --from=0 /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
ADD html/ /opt/kaperys/vision/html
EXPOSE 8080
WORKDIR /opt/kaperys/vision
ENTRYPOINT [ "./server" ]
这样,您可以从已安装Go(包括go mod
)的镜像开始,然后从任何其他镜像构建您的实际镜像(在此示例中为“scratch”)。
英文:
If you don't need Go when executing your image (docker run), but only need it for building your image (docker build), then, as commented, use a multi-stage build.
Here is an example: "Create lean Docker images using the Builder Pattern" from Mike Kaperys:
> Each FROM
instruction defines a new base image for the following instructions, and begins a new build stage.
Before multi-stage builds it was possible to to achieve the same effect, but the process required 2 Dockerfiles.
>
> Using multi-stage builds allows you to selectively copy files between build stages — this is the basis of the builder pattern.
See kaperys/blog/docker-builder-pattern
FROM golang:1.16
COPY . /go/src/github.com/kaperys/blog/docker-builder-pattern
WORKDIR /go/src/github.com/kaperys/blog/docker-builder-pattern
RUN go get && CGO_ENABLED=0 GOOS=linux go build -o server .
FROM scratch
LABEL maintainer="Mike Kaperys <mike@kaperys.io>"
COPY --from=0 /go/src/github.com/kaperys/blog/docker-builder-pattern/server /opt/kaperys/vision/server
COPY --from=0 /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
ADD html/ /opt/kaperys/vision/html
EXPOSE 8080
WORKDIR /opt/kaperys/vision
ENTRYPOINT [ "./server" ]
That way, you start from an image where Go is already installed (included go mod
), then you build your actual image from any other one you want (including, in this example, "scratch").
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论