Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dockerfile): bugfix for broken docker build + optimization #4154

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 24 additions & 12 deletions Dockerfile
@@ -1,23 +1,35 @@
FROM golang:1.9.0-alpine3.6 AS build
# GitHub: https://github.com/gohugoio/
# Docker: https://hub.docker.com/r/gohugoio/
# Twitter: https://twitter.com/gohugoio

RUN apk add --no-cache --virtual git musl-dev
RUN go get github.com/golang/dep/cmd/dep
FROM golang:1.10-rc-alpine3.7 AS build

WORKDIR /go/src/github.com/gohugoio/hugo
RUN dep ensure
ADD . /go/src/github.com/gohugoio/hugo/
RUN go install -ldflags '-s -w'

FROM alpine:3.6
RUN \
adduser -h /site -s /sbin/nologin -u 1000 -D hugo && \
apk add --no-cache \
dumb-init
git \
musl-dev && \
go get github.com/golang/dep/cmd/dep && \
go get github.com/kardianos/govendor && \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not relying on the current working directory?
Wouldn't it be something interesting to be able to generate an image from the current directory (for development or testing for example)?

govendor get github.com/gohugoio/hugo && \
cd /go/src/github.com/gohugoio/hugo && \
dep ensure && \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although you consider it breaks docker best practices, you may consider that doing this requires re-building
and re-downloading everything for every single docker build.

It is okay when the build step is built by a remote server that always discards cache, but might be interesting when iterating on docker build locally. Docker has even edited its best practices explaining latest versions of docker have "have mitigated the need" of minimizing the number of layers.

As suggested in #4077 distinguishing installing dependent tools, dependencies and actual binary generation allows to benefit the docker build cache while building the image.
Having a dockerfile of (as set in #4077 ):

RUN apk add --no-cache \
    git \
    musl-dev \
 && go get github.com/golang/dep/cmd/dep

COPY Gopkg.lock Gopkg.toml /go/src/github.com/gohugoio/hugo/
WORKDIR /go/src/github.com/gohugoio/hugo
RUN dep ensure -vendor-only
COPY . /go/src/github.com/gohugoio/hugo/
RUN go install -ldflags '-s -w'

considers that:

  • dep, git, musl-dev are external tools that change the less, without a strong dependency on the exact version
  • go dependencies are changing slower than the code itself, and quite long to install that are worth caching in local developments, and needs to be re-built every time Gopkg.* changes
  • hugo code changes the most often and thus needs to be re-build every time a file is changed

go install -ldflags '-s -w'

# ---

FROM alpine:3.7

COPY --from=build /go/bin/hugo /bin/hugo

RUN \
adduser -h /site -s /sbin/nologin -u 1000 -D hugo && \
apk add --no-cache dumb-init

USER hugo
WORKDIR /site
VOLUME /site
EXPOSE 1313

ENTRYPOINT ["/usr/bin/dumb-init", "--", "hugo"]
ENTRYPOINT [ "/usr/bin/dumb-init", "--", "hugo" ]
CMD [ "--help" ]