Skip to content

Commit

Permalink
Docker: ship a static binary with alpine image
Browse files Browse the repository at this point in the history
Update Makefile and Dockerfile to build a static binary by default and
get a much smaller footprint with alpine image.  It also includes CA
certificates needed for practical use.

Optimize ordering of build steps in Dockerfile to avoid needless
downloads.

Add Dockerfile.dev in .gitignore and .dockerignore.  It's to utilize
cached build steps on Dockerfile development.
  • Loading branch information
yaegashi committed Jan 21, 2019
1 parent 4731122 commit 156d4dd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dockerfile.dev
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ _testmain.go

# Editor swap/temp files
.*.swp

# Dockerfile.dev is ignored by both git and docker
# for faster development cycle of docker build
# cp Dockerfile Dockerfile.dev
# vi Dockerfile.dev
# docker build -f Dockerfile.dev .
Dockerfile.dev
20 changes: 12 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
FROM golang:1.11-stretch AS builder
WORKDIR /go/src/github.com/pusher/oauth2_proxy

# Download tools
RUN wget -O $GOPATH/bin/dep https://github.com/golang/dep/releases/download/v0.5.0/dep-linux-amd64
RUN chmod +x $GOPATH/bin/dep

# Copy sources
WORKDIR $GOPATH/src/github.com/pusher/oauth2_proxy
COPY . .

# Fetch dependencies
RUN wget -O dep https://github.com/golang/dep/releases/download/v0.5.0/dep-linux-amd64
RUN chmod +x dep
RUN mv dep $GOPATH/bin/dep
RUN dep ensure --vendor-only

# Build image
RUN ./configure && make clean oauth2_proxy
# Build binary
RUN ./configure && make build

# Copy binary to debian
FROM debian:stretch
# Copy binary to alpine
FROM alpine:3.8
RUN apk add --no-cache ca-certificates
COPY --from=builder /go/src/github.com/pusher/oauth2_proxy/oauth2_proxy /bin/oauth2_proxy

ENTRYPOINT ["/bin/oauth2_proxy"]
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dep:
build: clean $(BINARY)

$(BINARY):
$(GO) build -ldflags="-X main.VERSION=${VERSION}" -o $(BINARY) github.com/pusher/oauth2_proxy
CGO_ENABLED=0 $(GO) build -a -installsuffix cgo -ldflags="-X main.VERSION=${VERSION}" -o $@ github.com/pusher/oauth2_proxy

.PHONY: test
test: dep lint
Expand Down

0 comments on commit 156d4dd

Please sign in to comment.