Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我在这个多阶段构建中的最终docker形象如此之大?_Docker_Go_Dockerfile_Docker Multi Stage Build - Fatal编程技术网

为什么我在这个多阶段构建中的最终docker形象如此之大?

为什么我在这个多阶段构建中的最终docker形象如此之大?,docker,go,dockerfile,docker-multi-stage-build,Docker,Go,Dockerfile,Docker Multi Stage Build,在阅读了关于多阶段docker构建可能带来的巨大图像大小缩减的内容后,我正在尝试缩小用于构建Go二进制文件的docker文件的图像大小。我的Dockerfile在下面 # Configure environment and build settings. FROM golang:alpine AS buildstage ARG name=ddmnh ENV GOPATH=/gopath # Create the working directory. WORKDIR ${GOPATH} # C

在阅读了关于多阶段docker构建可能带来的巨大图像大小缩减的内容后,我正在尝试缩小用于构建Go二进制文件的docker文件的图像大小。我的Dockerfile在下面

# Configure environment and build settings.
FROM golang:alpine AS buildstage
ARG name=ddmnh
ENV GOPATH=/gopath

# Create the working directory.
WORKDIR ${GOPATH}

# Copy the repository into the image.
ADD . ${GOPATH}

# Move to GOPATH, install dependencies and build the binary.
RUN cd ${GOPATH} && go get ${name}
RUN CGO_ENABLED=0 GOOS=linux go build ${name}

# Multi-stage build, we just use plain alpine for the final image.
FROM alpine:latest

# Copy the binary from the first stage.
COPY --from=buildstage ${GOPATH}/${name} ./${name}
RUN chmod u+x ./${name}

# Expose Port 80.
EXPOSE 80

# Set the run command.
CMD ./ddmnh
然而,生成的图像似乎根本没有缩小。我怀疑
golang:alpine
图像不知何故被包括在内。下面是在上面的docker文件上运行
docker build.
的结果截图

阿尔卑斯山:最新的图像只有4.15MB。加上编译后的二进制文件的大小(相对较小),我预计最终图像的大小不会超过15MB。但它是407MB。我显然做错了什么


如何调整Docker文件以生成较小的图像?

在Docker文档的深处,我发现当我从开始最终的
时,我的
ARG
ENV
定义已被清除。重新定义它们解决了这个问题:

# Configure environment and build settings.
FROM golang:alpine AS buildstage
ARG name=ddmnh
ENV GOPATH=/gopath

# Create the working directory.
WORKDIR ${GOPATH}

# Copy the repository into the image.
ADD . ${GOPATH}

# Move to GOPATH, install dependencies and build the binary.
RUN cd ${GOPATH} && go get ${name}
RUN CGO_ENABLED=0 GOOS=linux go build ${name}

# Multi-stage build, we just use plain alpine for the final image.
FROM alpine:latest
ARG name=ddmnh
ENV GOPATH=/gopath

# Copy the binary from the first stage.
COPY --from=buildstage ${GOPATH}/${name} ./${name}
RUN chmod u+x ./${name}

# Expose Port 80.
EXPOSE 80

# Set the run command.
CMD ./ddmnh

你能尝试一下docker history吗?没有trunc
来查看图像中的内容吗?因此,
/
是从第一阶段的容器中复制的。