diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000000000..d114ba901dc2b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,10 @@ +.git/ +bazel-bin/ +bazel-out/ +bazel-tidb/ +bazel-testlogs/ +bin/ +tidb-server/tidb-server +*.test.bin +cmd/ +Dockerfile diff --git a/.github/licenserc.yml b/.github/licenserc.yml index 68e70685a12f8..e1add7017983b 100644 --- a/.github/licenserc.yml +++ b/.github/licenserc.yml @@ -6,6 +6,7 @@ header: - "docs/" - "br/" - ".gitignore" + - ".dockerignore" - ".gitattributes" - ".cilinter.yaml" - ".golangci.yml" diff --git a/Dockerfile b/Dockerfile index e96254e0dd126..8416ef542a3d3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -# Copyright 2019 PingCAP, Inc. +# Copyright 2022 PingCAP, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,45 +13,19 @@ # limitations under the License. # Builder image -FROM golang:1.19.1-alpine as builder +FROM alpine:edge as builder -RUN apk add --no-cache \ - wget \ - make \ - git \ - gcc \ - binutils-gold \ - musl-dev +ADD . https://raw.githubusercontent.com/njhallett/apk-fastest-mirror/c4ca44caef3385d830fea34df2dbc2ba4a17e021/apk-fastest-mirror.sh ./proxy +RUN sh ./proxy/apk-fastest-mirror.sh -t 50 && apk add --no-cache git build-base go -RUN wget -O /usr/local/bin/dumb-init https://github.com/Yelp/dumb-init/releases/download/v1.2.2/dumb-init_1.2.2_amd64 \ - && chmod +x /usr/local/bin/dumb-init +COPY . /tidb +ARG GOPROXY +RUN export GOPROXY=${GOPROXY} && cd /tidb && make server -RUN mkdir -p /go/src/github.com/pingcap/tidb -WORKDIR /go/src/github.com/pingcap/tidb +FROM alpine:latest -# Cache dependencies -COPY go.mod . -COPY go.sum . -COPY parser/go.mod parser/go.mod -COPY parser/go.sum parser/go.sum - -RUN GO111MODULE=on go mod download - -# Build real binaries -COPY . . -RUN make - -# Executable image -FROM alpine - -RUN apk add --no-cache \ - curl - -COPY --from=builder /go/src/github.com/pingcap/tidb/bin/tidb-server /tidb-server -COPY --from=builder /usr/local/bin/dumb-init /usr/local/bin/dumb-init +COPY --from=builder /tidb/bin/tidb-server /tidb-server WORKDIR / - EXPOSE 4000 - -ENTRYPOINT ["/usr/local/bin/dumb-init", "/tidb-server"] +ENTRYPOINT ["/tidb-server"] diff --git a/Makefile b/Makefile index bc350cea3a255..f72ea097ff98b 100644 --- a/Makefile +++ b/Makefile @@ -140,30 +140,30 @@ race: failpoint-enable server: ifeq ($(TARGET), "") - CGO_ENABLED=1 $(GOBUILD) $(RACE_FLAG) -ldflags '$(LDFLAGS) $(CHECK_FLAG)' -o bin/tidb-server tidb-server/main.go + CGO_ENABLED=1 $(GOBUILD) $(RACE_FLAG) -ldflags '$(LDFLAGS) $(CHECK_FLAG)' -o bin/tidb-server ./tidb-server else - CGO_ENABLED=1 $(GOBUILD) $(RACE_FLAG) -ldflags '$(LDFLAGS) $(CHECK_FLAG)' -o '$(TARGET)' tidb-server/main.go + CGO_ENABLED=1 $(GOBUILD) $(RACE_FLAG) -ldflags '$(LDFLAGS) $(CHECK_FLAG)' -o '$(TARGET)' ./tidb-server endif server_debug: ifeq ($(TARGET), "") - CGO_ENABLED=1 $(GOBUILD) -gcflags="all=-N -l" $(RACE_FLAG) -ldflags '$(LDFLAGS) $(CHECK_FLAG)' -o bin/tidb-server-debug tidb-server/main.go + CGO_ENABLED=1 $(GOBUILD) -gcflags="all=-N -l" $(RACE_FLAG) -ldflags '$(LDFLAGS) $(CHECK_FLAG)' -o bin/tidb-server-debug ./tidb-server else - CGO_ENABLED=1 $(GOBUILD) -gcflags="all=-N -l" $(RACE_FLAG) -ldflags '$(LDFLAGS) $(CHECK_FLAG)' -o '$(TARGET)' tidb-server/main.go + CGO_ENABLED=1 $(GOBUILD) -gcflags="all=-N -l" $(RACE_FLAG) -ldflags '$(LDFLAGS) $(CHECK_FLAG)' -o '$(TARGET)' ./tidb-server endif server_check: ifeq ($(TARGET), "") - $(GOBUILD) $(RACE_FLAG) -ldflags '$(CHECK_LDFLAGS)' -o bin/tidb-server tidb-server/main.go + $(GOBUILD) $(RACE_FLAG) -ldflags '$(CHECK_LDFLAGS)' -o bin/tidb-server ./tidb-server else - $(GOBUILD) $(RACE_FLAG) -ldflags '$(CHECK_LDFLAGS)' -o '$(TARGET)' tidb-server/main.go + $(GOBUILD) $(RACE_FLAG) -ldflags '$(CHECK_LDFLAGS)' -o '$(TARGET)' ./tidb-server endif linux: ifeq ($(TARGET), "") - GOOS=linux $(GOBUILD) $(RACE_FLAG) -ldflags '$(LDFLAGS) $(CHECK_FLAG)' -o bin/tidb-server-linux tidb-server/main.go + GOOS=linux $(GOBUILD) $(RACE_FLAG) -ldflags '$(LDFLAGS) $(CHECK_FLAG)' -o bin/tidb-server-linux ./tidb-server else - GOOS=linux $(GOBUILD) $(RACE_FLAG) -ldflags '$(LDFLAGS) $(CHECK_FLAG)' -o '$(TARGET)' tidb-server/main.go + GOOS=linux $(GOBUILD) $(RACE_FLAG) -ldflags '$(LDFLAGS) $(CHECK_FLAG)' -o '$(TARGET)' ./tidb-server endif server_coverage: @@ -461,3 +461,9 @@ bazel_addindextest: failpoint-enable bazel_ci_prepare bazel_lint: bazel_prepare bazel build //... --//build:with_nogo_flag=true + +docker: + docker build -t "$(DOCKERPREFIX)tidb:latest" --build-arg 'GOPROXY=$(shell go env GOPROXY),' -f Dockerfile . + +docker-test: + docker buildx build --platform linux/amd64,linux/arm64 --push -t "$(DOCKERPREFIX)tidb:latest" --build-arg 'GOPROXY=$(shell go env GOPROXY),' -f Dockerfile .