plan to use docker

This commit is contained in:
yedongfu 2021-07-10 21:45:43 +08:00
parent edd2470102
commit 14ac7ce698
4 changed files with 41 additions and 0 deletions

View File

@ -3,4 +3,5 @@ WORKDIR /app/dtm
RUN go env -w GO111MODULE=on
RUN go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct
EXPOSE 8080
ENV IS_DOCKER 1
CMD [ "/bin/bash", "-c", "go build app/main.go && /app/dtm/main" ]

View File

@ -4,6 +4,7 @@ import (
"database/sql"
"fmt"
"regexp"
"strings"
"time"
"github.com/sirupsen/logrus"
@ -87,6 +88,9 @@ func (op *tracePlugin) Initialize(db *gorm.DB) (err error) {
}
func GetDsn(conf map[string]string) string {
if IsDocker() {
conf["host"] = strings.Replace(conf["host"], "localhost", "host.docker.internal", 1)
}
return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true&loc=Local", conf["user"], conf["password"], conf["host"], conf["port"], conf["database"])
}

View File

@ -188,6 +188,9 @@ func init() {
// RestyClient.SetRetryCount(2)
// RestyClient.SetRetryWaitTime(1 * time.Second)
RestyClient.OnBeforeRequest(func(c *resty.Client, r *resty.Request) error {
if IsDocker() {
r.URL = strings.Replace(r.URL, "localhost", "host.docker.internal", 1)
}
logrus.Printf("requesting: %s %s %v %v", r.Method, r.URL, r.Body, r.QueryParam)
return nil
})
@ -269,3 +272,7 @@ func GetFuncName() string {
pc, _, _, _ := runtime.Caller(1)
return runtime.FuncForPC(pc).Name()
}
func IsDocker() bool {
return os.Getenv("IS_DOCKER") != ""
}

29
compose.yml Normal file
View File

@ -0,0 +1,29 @@
version: '3.3'
services:
mysql:
image: 'mysql:5.7'
environment:
MYSQL_ROOT_PASSWORD: my-secret-pw
TZ: Asia/shanghai
command:
[
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_unicode_ci',
'--default-time-zone=+8:00',
]
ports:
- '3306:3306'
services:
dtm:
image: 'mysql:5.7'
environment:
MYSQL_ROOT_PASSWORD: my-secret-pw
TZ: Asia/shanghai
command:
[
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_unicode_ci',
'--default-time-zone=+8:00',
]
ports:
- '3306:3306'