Skip to content

Commit

Permalink
refactor(docker): updated docker base image and dockerfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
xmlking committed Jun 21, 2020
1 parent 6a7c878 commit 08540c1
Show file tree
Hide file tree
Showing 52 changed files with 378 additions and 1,042 deletions.
13 changes: 8 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Accept the Go Micro version for the image to be set as a build argument.
ARG GO_MICRO_VERSION=latest
ARG BASE_VERSION=latest

# First stage: build the executable.
FROM micro/go-micro:${GO_MICRO_VERSION} AS builder
FROM docker.pkg.github.com/xmlking/grpc-starter-kit/base:${BASE_VERSION} AS builder

# Set the environment variables for the go command:
# * CGO_ENABLED=0 to build a statically-linked executable
Expand Down Expand Up @@ -32,15 +32,18 @@ ARG TARGET=account
RUN pkger -o $TYPE/$TARGET -include /config/config.yaml -include /config/config.prod.yaml -include /config/certs
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
-o /app ./$TYPE/$TARGET/main.go

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

# copy 1 MiB busybox executable
COPY --from=busybox:1.31.1 /bin/busybox /bin/busybox

# copy dumb-ini from micro
# copy grpc-health-probe to use with readiness and liveness probes
COPY --from=builder /bin/grpc_health_probe /bin/grpc_health_probe

# copy dumb-ini from base
COPY --from=builder /usr/bin/dumb-init /usr/bin/dumb-init

# Import the user and group files from the first stage.
Expand Down Expand Up @@ -82,7 +85,7 @@ LABEL org.label-schema.build-date=$BUILD_DATE \
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 -e MICRO_SERVER_ADDRESS=0.0.0.0:8080 -p 8080:8080 ${DOCKER_REGISTRY:+${DOCKER_REGISTRY}/}${DOCKER_CONTEXT_PATH}/${TARGET}-${TYPE}:${VERSION}"
org.label-schema.docker.cmd="docker run -it -e CONFIGOR_ENV=prod -p 8080:8080 ${DOCKER_REGISTRY:+${DOCKER_REGISTRY}/}${DOCKER_CONTEXT_PATH}/${TARGET}-${TYPE}:${VERSION}"

# Run the compiled binary.
ENTRYPOINT ["/usr/bin/dumb-init", "/app"]
33 changes: 33 additions & 0 deletions Dockerfile.base
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
FROM golang:1.14-alpine

RUN mkdir /user && \
echo 'nobody:x:65534:65534:nobody:/:' > /user/passwd && \
echo 'nobody:x:65534:' > /user/group && \
apk --no-cache add make git gcc libtool musl-dev ca-certificates dumb-init && \
rm -rf /var/cache/apk/* /tmp/* && \
GRPC_HEALTH_PROBE_VERSION=v0.3.2 && \
wget -q -O /bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 && \
chmod +x /bin/grpc_health_probe

ENV GO111MODULE=on

WORKDIR /
COPY ./go.mod ./go.sum ./
RUN go mod download && rm go.mod go.sum

# Metadata params
ARG BUILD_DATE
ARG VCS_URL=grpc-starter-kit
ARG VCS_REF=1
ARG VENDOR=sumo

# Metadata
LABEL org.label-schema.build-date=$BUILD_DATE \
org.label-schema.name="base" \
org.label-schema.description="grpc-starter-kit base docker image" \
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"
17 changes: 10 additions & 7 deletions cgo.dockerfile → Dockerfile.cgo
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Accept the Go Micro version for the image to be set as a build argument.
ARG GO_MICRO_VERSION=latest
ARG BASE_VERSION=latest

# First stage: build the executable.
FROM micro/go-micro:${GO_MICRO_VERSION} AS builder
FROM docker.pkg.github.com/xmlking/grpc-starter-kit/base:${BASE_VERSION} AS builder

# Set the environment variables for the go command:
# * CGO_ENABLED=0 to build a statically-linked executable
Expand All @@ -29,18 +29,21 @@ ARG VERSION=0.0.1
ARG TYPE=service
ARG TARGET=account

RUN pkger -o $TYPE/$TARGET -include /config
RUN pkger -o $TYPE/$TARGET -include /config/config.yaml -include /config/config.prod.yaml -include /config/certs
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
-o /app ./$TYPE/$TARGET/main.go

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

# copy 1 MiB busybox executable
COPY --from=busybox:1.31.1 /bin/busybox /bin/busybox

# copy dumb-ini from micro
# copy grpc-health-probe to use with readiness and liveness probes
COPY --from=builder /bin/grpc_health_probe /bin/grpc_health_probe

# copy dumb-ini from base
COPY --from=builder /usr/bin/dumb-init /usr/bin/dumb-init

# Import the user and group files from the first stage.
Expand All @@ -54,7 +57,7 @@ ARG VERSION=0.0.1
ARG TYPE=service
ARG TARGET=account
COPY --from=builder /app /app
COPY --from=builder src/config/base/service/${TARGET}/config /config
COPY --from=builder src/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
Expand Down Expand Up @@ -82,7 +85,7 @@ LABEL org.label-schema.build-date=$BUILD_DATE \
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 -e MICRO_SERVER_ADDRESS=0.0.0.0:8080 -p 8080:8080 ${DOCKER_REGISTRY:+${DOCKER_REGISTRY}/}${DOCKER_CONTEXT_PATH}/${TARGET}-${TYPE}:${VERSION}"
org.label-schema.docker.cmd="docker run -it -e CONFIGOR_ENV=prod -p 8080:8080 ${DOCKER_REGISTRY:+${DOCKER_REGISTRY}/}${DOCKER_CONTEXT_PATH}/${TARGET}-${TYPE}:${VERSION}"

# Run the compiled binary.
ENTRYPOINT ["/usr/bin/dumb-init", "/app"]
17 changes: 10 additions & 7 deletions no_cgo.dockerfile → Dockerfile.no_cgo
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Accept the Go Micro version for the image to be set as a build argument.
ARG GO_MICRO_VERSION=latest
ARG BASE_VERSION=latest

# First stage: build the executable.
FROM micro/go-micro:${GO_MICRO_VERSION} AS builder
FROM docker.pkg.github.com/xmlking/grpc-starter-kit/base:${BASE_VERSION} AS builder

# Set the environment variables for the go command:
# * CGO_ENABLED=0 to build a statically-linked executable
Expand All @@ -29,18 +29,21 @@ ARG VERSION=0.0.1
ARG TYPE=service
ARG TARGET=account

RUN pkger -o $TYPE/$TARGET -include /config
RUN pkger -o $TYPE/$TARGET -include /config/config.yaml -include /config/config.prod.yaml -include /config/certs
RUN go build -a \
-ldflags="-w -s $(govvv -flags -version ${VERSION} -pkg $(go list ./shared/config) )" \
-o /app ./$TYPE/$TARGET/main.go ./$TYPE/$TARGET/plugin.go
-o /app ./$TYPE/$TARGET/main.go

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

# copy 1 MiB busybox executable
COPY --from=busybox:1.31.1 /bin/busybox /bin/busybox

# copy dumb-ini from micro
# copy grpc-health-probe to use with readiness and liveness probes
COPY --from=builder /bin/grpc_health_probe /bin/grpc_health_probe

# copy dumb-ini from base
COPY --from=builder /usr/bin/dumb-init /usr/bin/dumb-init

# Import the user and group files from the first stage.
Expand All @@ -54,7 +57,7 @@ ARG VERSION=0.0.1
ARG TYPE=service
ARG TARGET=account
COPY --from=builder /app /app
COPY --from=builder src/config/base/service/${TARGET}/config /config
COPY --from=builder src/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
Expand Down Expand Up @@ -82,7 +85,7 @@ LABEL org.label-schema.build-date=$BUILD_DATE \
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 -e MICRO_SERVER_ADDRESS=0.0.0.0:8080 -p 8080:8080 ${DOCKER_REGISTRY:+${DOCKER_REGISTRY}/}${DOCKER_CONTEXT_PATH}/${TARGET}-${TYPE}:${VERSION}"
org.label-schema.docker.cmd="docker run -it -e CONFIGOR_ENV=prod -p 8080:8080 ${DOCKER_REGISTRY:+${DOCKER_REGISTRY}/}${DOCKER_CONTEXT_PATH}/${TARGET}-${TYPE}:${VERSION}"

# Run the compiled binary.
ENTRYPOINT ["/usr/bin/dumb-init", "/app"]
15 changes: 11 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ DOCKER_REGISTRY := docker.pkg.github.com
DOCKER_CONTEXT_PATH := $(GITHUB_REPO_OWNER)/$(GITHUB_REPO_NAME)
# DOCKER_REGISTRY := docker.io
# DOCKER_CONTEXT_PATH := xmlking
GO_MICRO_VERSION := latest
BASE_VERSION := latest

VERSION := $(shell git describe --tags || echo "HEAD")
GOPATH := $(shell go env GOPATH)
Expand Down Expand Up @@ -245,7 +245,7 @@ docker docker-%:
docker build --rm \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--build-arg VERSION=$(VERSION) \
--build-arg GO_MICRO_VERSION=$(GO_MICRO_VERSION) \
--build-arg BASE_VERSION=$(BASE_VERSION) \
--build-arg TYPE=$${type} \
--build-arg TARGET=$${target} \
--build-arg DOCKER_REGISTRY=${DOCKER_REGISTRY} \
Expand All @@ -260,7 +260,7 @@ docker docker-%:
docker build --rm \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--build-arg VERSION=$(VERSION) \
--build-arg GO_MICRO_VERSION=$(GO_MICRO_VERSION) \
--build-arg BASE_VERSION=$(BASE_VERSION) \
--build-arg TYPE=${TYPE} \
--build-arg TARGET=${TARGET} \
--build-arg DOCKER_REGISTRY=${DOCKER_REGISTRY} \
Expand All @@ -286,8 +286,15 @@ docker_push:
docker push $$image; \
done;

docker_base:
docker build --build-arg BUILD_DATE=$(shell date +%FT%T%Z) -f Dockerfile.base -t ${DOCKER_REGISTRY}/${DOCKER_CONTEXT_PATH}/base:$(VERSION) .
docker tag ${DOCKER_REGISTRY}/${DOCKER_CONTEXT_PATH}/base:$(VERSION) ${DOCKER_REGISTRY}/${DOCKER_CONTEXT_PATH}/base:latest
# docker push ${DOCKER_REGISTRY}/${DOCKER_CONTEXT_PATH}/base:$(VERSION)
# docker push ${DOCKER_REGISTRY}/${DOCKER_CONTEXT_PATH}/base:latest


kustomize: OVERLAY := local
kustomize: NS := default
kustomize: NS := default
kustomize:
# @kustomize build --load_restrictor none config/envs/${OVERLAY}/ | sed -e "s|\$$(NS)|${NS}|g" -e "s|\$$(IMAGE_VERSION)|${VERSION}|g" | kubectl apply -f -
@kustomize build --load_restrictor none config/envs/${OVERLAY}/ | sed -e "s|\$$(NS)|${NS}|g" -e "s|\$$(IMAGE_VERSION)|${VERSION}|g" > build/kubernetes.yaml
Expand Down
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,12 @@ kubectl delete -f build/kubernetes.yaml

### External Docs

1. [Go-Micro Getting Started](https://itnext.io/micro-in-action-getting-started-a79916ae3cac)
2. [examples](https://github.com/micro/examples) - example usage code for micro
3. [microhq](https://github.com/microhq) - a place for prebuilt microservices
4. [explorer](https://micro.mu/explore/) - which aggregates micro based open source projects
5. [micro-plugins](https://github.com/micro/go-plugins) extensible micro plugins
6. [step-by-step-guide-micro](https://github.com/micro-in-cn/tutorials/tree/master/microservice-in-micro)
7. [micro-in-cn](https://github.com/micro-in-cn/tutorials/tree/master/examples)
8. [Platform Web](https://github.com/micro-in-cn/platform-web)
9. [grpc template](https://github.com/vtolstov/micro-template-grpc)
10. [Simple API backed by PostgresQL, Golang and gRPC](https://medium.com/@vptech/complexity-is-the-bane-of-every-software-engineer-e2878d0ad45a)
1. [examples](https://github.com/micro/examples) - example usage code for micro
1. [microhq](https://github.com/microhq) - a place for prebuilt microservices
1. [explorer](https://micro.mu/explore/) - which aggregates micro based open source projects
1. [micro-plugins](https://github.com/micro/go-plugins) extensible micro plugins
1. [step-by-step-guide-micro](https://github.com/micro-in-cn/tutorials/tree/master/microservice-in-micro)
1. [micro-in-cn](https://github.com/micro-in-cn/tutorials/tree/master/examples)
1. [Platform Web](https://github.com/micro-in-cn/platform-web)
1. [grpc template](https://github.com/vtolstov/micro-template-grpc)
1. [Simple API backed by PostgresQL, Golang and gRPC](https://medium.com/@vptech/complexity-is-the-bane-of-every-software-engineer-e2878d0ad45a)
49 changes: 22 additions & 27 deletions cmd/account/main.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
package main

import (
"context"
"flag"
"context"
"flag"
"fmt"

grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/metadata"

"github.com/golang/protobuf/ptypes/wrappers"
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/rs/zerolog/log"

"github.com/xmlking/grpc-starter-kit/micro/middleware/rpclog"
userv1 "github.com/xmlking/grpc-starter-kit/mkit/service/account/user/v1"
"github.com/xmlking/grpc-starter-kit/shared/config"
_ "github.com/xmlking/grpc-starter-kit/shared/logger"
userv1 "github.com/xmlking/grpc-starter-kit/mkit/service/account/user/v1"
"github.com/xmlking/grpc-starter-kit/shared/config"
"github.com/xmlking/grpc-starter-kit/shared/constants"
_ "github.com/xmlking/grpc-starter-kit/shared/logger"
"github.com/xmlking/grpc-starter-kit/shared/util"
)

var (
cfg = config.GetConfig()
)

func main() {
log.Debug().Msgf("IsProduction? %v", config.IsProduction())
log.Debug().Msgf("IsProduction? %t", config.IsProduction())
//log.Debug().Interface("Dialect", cfg.Database.Dialect).Send()
//log.Debug().Msg(cfg.Database.Host)
//log.Debug().Uint32("Port", cfg.Database.Port).Send()
//log.Debug().Uint64("FlushInterval", cfg.Features.Tracing.FlushInterval).Send()
//log.Debug().Msgf("cfg is %v", cfg)
//log.Debug().Msgf("cfg is %+v", cfg)

username := flag.String("username", "sumo", "username of user to be create")
email := flag.String("email", "sumo@demo.com", "email of user to be create")
Expand All @@ -36,13 +36,7 @@ func main() {

log.Debug().Str("username", *username).Str("email", *email).Uint64("limit", *limit).Msg("Flags Using:")

conn, err := grpc.Dial(
cfg.Services.Account.Endpoint, grpc.WithInsecure(),
grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy":"round_robin"}`),
grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(
rpclog.UnaryClientInterceptor(),
)),
)
conn, err := config.GetClientConn(cfg.Services.Account)
if err != nil {
log.Fatal().Msgf("did not connect: %s", err)
}
Expand All @@ -53,13 +47,13 @@ func main() {
//md := metadata.Pairs("k1", "v1", "k1", "v2", "k2", "v3")
//ctx := metadata.NewOutgoingContext(context.Background(), md)
// create a new context with some metadata - (Optional) Just for demonstration
ctx := metadata.AppendToOutgoingContext(context.Background(), "X-User-Id", "john", "X-From-Id", "script")

ctx := metadata.AppendToOutgoingContext(context.Background(), constants.TraceIDKey, "12345", constants.FromServiceKey, "e2e-account-test-client")
suffix := util.RandomStringLower(5)
if rsp, err := userClient.Create(ctx, &userv1.CreateRequest{
Username: &wrappers.StringValue{Value: "sumo"},
FirstName: &wrappers.StringValue{Value: "sumo"},
LastName: &wrappers.StringValue{Value: "demo"},
Email: &wrappers.StringValue{Value: "sumo@demo.com"},
Username: &wrappers.StringValue{Value: "u_" + suffix},
FirstName: &wrappers.StringValue{Value: "f_" + suffix},
LastName: &wrappers.StringValue{Value: "l_" + suffix},
Email: &wrappers.StringValue{Value: fmt.Sprintf("e_%s@demo.com", suffix)},
}); err != nil {
log.Error().Err(err).Send()
} else {
Expand All @@ -70,7 +64,8 @@ func main() {
}

func getUserList(us userv1.UserServiceClient, limit uint32) {
if rsp, err := us.List(context.Background(), &userv1.ListRequest{Limit: &wrappers.UInt32Value{Value: limit}}); err != nil {
ctx := metadata.AppendToOutgoingContext(context.Background(), constants.TraceIDKey, "12345", constants.FromServiceKey, "e2e-account-test-client")
if rsp, err := us.List(ctx, &userv1.ListRequest{Limit: &wrappers.UInt32Value{Value: limit}}); err != nil {
log.Fatal().Err(err).Msg("Unable to List Users")
} else {
log.Info().Interface("listRsp", rsp).Send()
Expand Down
25 changes: 0 additions & 25 deletions config/base/etcd-operator/README.md

This file was deleted.

Loading

0 comments on commit 08540c1

Please sign in to comment.