-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
40 lines (38 loc) · 1.28 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
FROM golang:1.10.3 as builder
# install xz
RUN apt-get update && apt-get install -y \
xz-utils \
&& rm -rf /var/lib/apt/lists/*
# install UPX
ADD https://github.com/upx/upx/releases/download/v3.94/upx-3.94-amd64_linux.tar.xz /usr/local
RUN xz -d -c /usr/local/upx-3.94-amd64_linux.tar.xz | \
tar -xOf - upx-3.94-amd64_linux/upx > /bin/upx && \
chmod a+x /bin/upx
# install dep
RUN go get github.com/golang/dep/cmd/dep
# create a working directory
WORKDIR /go/src/github.com/williamhgough/go-postgres-api
# add source code
COPY . /go/src/github.com/williamhgough/go-postgres-api
# add Gopkg.toml and Gopkg.lock
ADD Gopkg.toml Gopkg.toml
ADD Gopkg.lock Gopkg.lock
# install packages
RUN dep ensure
# create a working directory
WORKDIR /go/src/github.com/williamhgough/go-postgres-api/cmd
# build the source
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main main.go
# strip and compress the binary
RUN strip --strip-unneeded main
RUN upx main
# use a minimal alpine image
FROM alpine:3.7
# add ca-certificates in case you need them
RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*
# set working directory
WORKDIR /root
# copy the binary from builder
COPY --from=builder /go/src/github.com/williamhgough/go-postgres-api/cmd/main .
# run the binary
CMD ["./main"]