TransWikia.com

Why is my final docker image in this multi-stage build so large?

Stack Overflow Asked by Luke Joshua Park on December 25, 2021

After reading about the enormous image size reductions that are possible with multi-stage docker builds, I’m attempting to slim down the image size for a Dockerfile I have for building a Go binary. My Dockerfile is below.

# 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

The resulting image, however, doesn’t seem to be size reduced at all. I suspect that the golang:alpine image is being included somehow. Below is a screenshot of the results of running docker build . on the Dockerfile above.

docker images

The alpine:latest image is only 4.15MB. Adding the size of the compiled binary (which is relatively small) I would expect no more than, say, maybe 15MB for the final image. But it’s 407MB. I’m clearly doing something wrong!

How can I adjust my Dockerfile to produce an image of less size?

One Answer

Buried deep in the Docker documentation I found that my ARG and ENV definitions were cleared when I started the final FROM. Redefining them solved the issue:

# 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

Answered by Luke Joshua Park on December 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP