forked from xmlking/micro-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgo.dockerfile
97 lines (77 loc) · 3.31 KB
/
cgo.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Accept the Go version for the image to be set as a build argument.
# Set default to Go v1.13
ARG GO_VERSION=1.13
# First stage: build the executable.
FROM golang:${GO_VERSION}-alpine AS builder
RUN apk add --no-cache gcc musl-dev
# Create the user and group files that will be used in the running container to
# run the process as an unprivileged user.
RUN mkdir /user && \
echo 'nobody:x:65534:65534:nobody:/:' > /user/passwd && \
echo 'nobody:x:65534:' > /user/group
# Install the Certificate-Authority certificates for the app to be able to make
# calls to HTTPS endpoints.
# Git is required for fetching the dependencies.
RUN apk add --no-cache ca-certificates git
# Set the environment variables for the go command:
# * CGO_ENABLED=0 to build a statically-linked executable
# * GOFLAGS=-mod=vendor to force `go build` to look into the `/vendor` folder.
#ENV CGO_ENABLED=0 GOFLAGS=-mod=vendor
ENV CGO_ENABLED=1
# Set the working directory outside $GOPATH to enable the support for modules.
WORKDIR /src
# Fetch dependencies first; they are less susceptible to change on every build
# and will therefore be cached for speeding up the next build
COPY ./go.mod ./go.sum ./
# Get dependancies - will also be cached if we won't change mod/sum
RUN go mod download && \
cd / && go get github.com/ahmetb/govvv && cd /src
# COPY the source code as the last step
COPY ./ ./
# Build the executable to `/app`. Mark the build as statically linked.
ARG VERSION=0.0.1
ARG TYPE=srv
ARG TARGET=account
RUN go build -a \
-ldflags="-w -s -linkmode external -extldflags '-static' $(govvv -flags -version ${VERSION} -pkg $(go list ./shared/config) )" \
-o /app ./$TYPE/$TARGET/main.go ./$TYPE/$TARGET/plugin.go
# Final stage: the running container.
FROM scratch AS final
# copy 1 MiB busybox executable
COPY --from=busybox:1.31.0 /bin/busybox /bin/busybox
# Import the user and group files from the first stage.
COPY --from=builder /user/group /user/passwd /etc/
# Import the Certificate-Authority certificates for enabling HTTPS.
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
# Import the compiled executable from the second stage.
ARG VERSION=0.0.1
ARG TYPE=srv
ARG TARGET=account
COPY --from=builder /app /app
COPY --from=builder src/deploy/bases/${TARGET}-${TYPE}/config /config
# Declare the port on which the webserver will be exposed.
# As we're going to run the executable as an unprivileged user, we can't bind
# to ports below 1024.
EXPOSE 8080
# Perform any further action as an unprivileged user.
USER nobody:nobody
# Metadata params
ARG DOCKER_REGISTRY
ARG DOCKER_CONTEXT_PATH=xmlking
ARG BUILD_DATE
ARG VCS_URL=micro-starter-kit
ARG VCS_REF=1
ARG VENDOR=sumo
# Metadata
LABEL org.label-schema.build-date=$BUILD_DATE \
org.label-schema.name="${TARGET}-${TYPE}" \
org.label-schema.description="Example of multi-stage docker build" \
org.label-schema.url="https://example.com" \
org.label-schema.vcs-url=https://github.com/xmlking/$VCS_URL \
org.label-schema.vcs-ref=$VCS_REF \
org.label-schema.vendor=$VENDOR \
org.label-schema.version=$VERSION \
org.label-schema.docker.schema-version="1.0" \
org.label-schema.docker.cmd=docker="run -it -p 8080:8080 ${DOCKER_REGISTRY:+${DOCKER_REGISTRY}/}${DOCKER_CONTEXT_PATH}/${TARGET}-${TYPE}:${VERSION}"
# Run the compiled binary.
ENTRYPOINT ["/app"]