From 00d4b5047b52d62909a4b78380aaf8fe33ebb1d5 Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Thu, 5 Aug 2021 14:02:29 +0800 Subject: [PATCH] try to build minimal image --- aux/Dockerfile-release | 12 ++++++++++++ compose.mysql.yml | 15 --------------- compose.yml | 2 +- dtmcli/utils.go | 2 +- dtmcli/utils_test.go | 4 ++-- 5 files changed, 16 insertions(+), 19 deletions(-) create mode 100644 aux/Dockerfile-release delete mode 100644 compose.mysql.yml diff --git a/aux/Dockerfile-release b/aux/Dockerfile-release new file mode 100644 index 0000000..2e07246 --- /dev/null +++ b/aux/Dockerfile-release @@ -0,0 +1,12 @@ +FROM golang as builder +WORKDIR /app/dtm +RUN go env -w GO111MODULE=on +RUN go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct +EXPOSE 8080 +COPY . . +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" app/main.go + +FROM scratch as runner +COPY --from=builder /app/dtm/main /app/dtm/main +ENV IS_DOCKER=1 +CMD ["/app/dtm/main", "dtmsvr"] diff --git a/compose.mysql.yml b/compose.mysql.yml deleted file mode 100644 index c640fa0..0000000 --- a/compose.mysql.yml +++ /dev/null @@ -1,15 +0,0 @@ -version: '3.3' -services: - mysql: - image: 'mysql:5.7' - environment: - MYSQL_ALLOW_EMPTY_PASSWORD: 1 - TZ: Asia/shanghai - command: - [ - '--character-set-server=utf8mb4', - '--collation-server=utf8mb4_unicode_ci', - '--default-time-zone=+8:00', - ] - ports: - - '3306:3306' diff --git a/compose.yml b/compose.yml index 5d40ab9..7da7b86 100644 --- a/compose.yml +++ b/compose.yml @@ -3,7 +3,7 @@ services: api: build: . environment: - IS_DOCKER_COMPOSE: '1' + IS_DOCKER: '1' ports: - '8080:8080' volumes: diff --git a/dtmcli/utils.go b/dtmcli/utils.go index e1103a6..9da4a5c 100644 --- a/dtmcli/utils.go +++ b/dtmcli/utils.go @@ -174,7 +174,7 @@ func GetFuncName() string { // MayReplaceLocalhost when run in docker compose, change localhost to host.docker.internal for accessing host network func MayReplaceLocalhost(host string) string { - if os.Getenv("IS_DOCKER_COMPOSE") != "" { + if os.Getenv("IS_DOCKER") != "" { return strings.Replace(host, "localhost", "host.docker.internal", 1) } return host diff --git a/dtmcli/utils_test.go b/dtmcli/utils_test.go index a67485e..a59d628 100644 --- a/dtmcli/utils_test.go +++ b/dtmcli/utils_test.go @@ -70,10 +70,10 @@ func TestSome(t *testing.T) { func1 := GetFuncName() assert.Equal(t, true, strings.HasSuffix(func1, "TestSome")) - os.Setenv("IS_DOCKER_COMPOSE", "1") + os.Setenv("IS_DOCKER", "1") s := MayReplaceLocalhost("http://localhost") assert.Equal(t, "http://host.docker.internal", s) - os.Setenv("IS_DOCKER_COMPOSE", "") + os.Setenv("IS_DOCKER", "") s2 := MayReplaceLocalhost("http://localhost") assert.Equal(t, "http://localhost", s2) }