Skip to content

Instantly share code, notes, and snippets.

@wallyqs
Last active March 15, 2021 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wallyqs/bdce71f796d039ba2fc30122fe1b9879 to your computer and use it in GitHub Desktop.
Save wallyqs/bdce71f796d039ba2fc30122fe1b9879 to your computer and use it in GitHub Desktop.
NATS Go Dockerfile + Go mod workflow
FROM golang:1.16-alpine AS builder
WORKDIR $GOPATH/src/github.com/nats-io/nats-server
MAINTAINER Waldemar Quevedo <wally@synadia.com>
RUN apk add --update git
COPY . .
RUN CGO_ENABLED=0 GO111MODULE=off go build -v -a -tags netgo -installsuffix netgo -ldflags "-s -w -X github.com/nats-io/nats-server/server.gitCommit=`git rev-parse --short HEAD`" -o /nats-server
FROM alpine:latest
RUN apk add --update ca-certificates && mkdir -p /nats/bin && mkdir /nats/conf
COPY docker/nats-server.conf /nats/conf/nats-server.conf
COPY --from=builder /nats-server /nats/bin/nats-server
# NOTE: For backwards compatibility, we add a symlink to /gnatsd which is
# where the binary from the scratch container image used to be located.
RUN ln -ns /nats/bin/nats-server /bin/nats-server && ln -ns /nats/bin/nats-server /nats-server && ln -ns /nats/bin/nats-server /gnatsd
# Expose client, cluster, monitoring, leafnode, gateway ports
EXPOSE 4222 6222 8222 7422 7522
ENTRYPOINT ["/bin/nats-server"]
CMD ["-c", "/nats/conf/nats-server.conf"]
# Start: go.mod does not include test dependencies, only those for binary.
# Create go.sum that does not include test dependencies.
go build -mod=mod -modfile=go.mod
# Updates the vendoring directory:
#
# - May bring test deps and update both `vendor/modules.txt' and `go.sum'
#
go mod vendor
# Builds the binary with what is in vendor folder only, not network access required, better for Docker, etc...
go build
# (Implicitly this is what is happening since there is a vendor folder)
go build -mod=vendor -modfile=go.mod
# DEV: Running tests updating dependencies if needed.
go test ./... -mod=mod -modfile go_test.mod -run TestJetStreamPushConsumersPullError -v
# Cleanup entries in go_test.sum
go mod tidy -mod=mod -modfile=go_test.mod
# CI: Run the tests without modifying deps.
go test ./... -mod=readonly -modfile go_test.mod -v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment