try to build minimal image

This commit is contained in:
yedf2 2021-08-05 14:02:29 +08:00
parent 6eefd24fae
commit 00d4b5047b
5 changed files with 16 additions and 19 deletions

12
aux/Dockerfile-release Normal file
View File

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

View File

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

View File

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

View File

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

View File

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