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

optimize docker image size #76

Merged
merged 1 commit into from
Mar 13, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Rockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# build image
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the magic of Rocker is we can separate the build and runtime image.
the build image container all resource or dependencies required at build time.
once the build success, copy it to the runtime image which based on alpine.
so the final image is alpine + MailHog executable binary.

FROM golang:latest
RUN go get -v github.com/mailhog/MailHog && \
cd /go/src && \
CGO_ENABLED=0 go build -a -installsuffix cgo -v -o /go/bin/MailHog.o github.com/mailhog/MailHog/main.go
Copy link
Contributor Author

Choose a reason for hiding this comment

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

use go get to download the main.go and all dependencies first.
then build the binary.

Since alpine linux and therefor gliderlabs/alpine docker containers use musl instead of gnu libc your golang binaries will not work inside alpine. so we need build with static linking enabled.
see also: http://www.blang.io/posts/2015-04_golang-alpine-build-golang-binaries-for-alpine-linux/

EXPORT /go/bin/ /target

# run image
FROM alpine:latest
IMPORT /target/ /tmp
RUN cp /tmp/MailHog.o /MailHog
EXPOSE 1025 8025
CMD ["/MailHog"]
TAG mailhog/mailhog:{{ or .VERSION "latest" }}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

if we want to tag the build with version:

rocker build -var VERSION=x.y.z

the result will be a tagged image if you docker images.