Browse Source

try to build minimal image

pull/15/head
yedf2 5 years ago
parent
commit
00d4b5047b
  1. 12
      aux/Dockerfile-release
  2. 15
      compose.mysql.yml
  3. 2
      compose.yml
  4. 2
      dtmcli/utils.go
  5. 4
      dtmcli/utils_test.go

12
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"]

15
compose.mysql.yml

@ -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'

2
compose.yml

@ -3,7 +3,7 @@ services:
api:
build: .
environment:
IS_DOCKER_COMPOSE: '1'
IS_DOCKER: '1'
ports:
- '8080:8080'
volumes:

2
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

4
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)
}

Loading…
Cancel
Save