Skip to content

Commit

Permalink
example docker file
Browse files Browse the repository at this point in the history
build with:
     docker build -t intercert .

run with
    docker run -p 6300 intercert serve --agree --dns-provider=azure
  • Loading branch information
kbudde authored and evenh committed May 1, 2019
1 parent 8aa026c commit ea3c3b2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
59 changes: 59 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Accept the Go version for the image to be set as a build argument.
ARG GO_VERSION=1.12

ARG INTERCERT_VERSION="DEV-SNAPSHOT"

# First stage: build the executable.
FROM golang:${GO_VERSION}-alpine AS builder

# 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 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 ./
RUN go mod download

# Import the code from the context.
COPY ./ ./

# Build the executable to `/app`. Mark the build as statically linked.
RUN CGO_ENABLED=0 go build -ldflags \
"-s -w -X main.Version=DEV-SNAPSHOT -X main.Commit=$(`echo git rev-parse --short HEAD`)" -o /app .

# Final stage: the running container.
FROM scratch AS final

# 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 first stage.
COPY --from=builder /app /app

# Declare the port on which the server will be exposed.
# As we're going to run the executable as an unprivileged user, we can't bind
# to ports below 1024.
EXPOSE 6300

VOLUME /server-data
VOLUME /.intercert

# Perform any further action as an unprivileged user.
USER nobody:nobody

# Run the compiled binary.
ENTRYPOINT ["/app"]
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"runtime"

Expand Down Expand Up @@ -39,7 +40,8 @@ func initConfig() {

// Try to read config
if err := viper.ReadInConfig(); err != nil {
PrintErrorAndExit(err)
errMsg := fmt.Sprintf("Failed to read configuration: %v", err)
PrintErrorAndExit(errors.New(errMsg))
}
}

Expand Down

0 comments on commit ea3c3b2

Please sign in to comment.