Skip to content

Commit 8d218de

Browse files
committed
feat: Automatically replace the default listening address in the docker with
1 parent f583b9b commit 8d218de

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

api.go

+23
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ import (
1313
"gopkg.in/gin-contrib/cors.v1"
1414
)
1515

16+
func isRunningInDockerContainer() bool {
17+
// slightly modified from blog: https://paulbradley.org/indocker/
18+
// docker creates a .dockerenv file at the root
19+
// of the directory tree inside the container.
20+
// if this file exists then the viewer is running
21+
// from inside a container so return true
22+
23+
if _, err := os.Stat("/.dockerenv"); err == nil {
24+
return true
25+
}
26+
27+
return false
28+
}
29+
1630
// StartAPIServer starts the API server
1731
func StartAPIServer(config *Config,
1832
reloadChan chan bool,
@@ -23,6 +37,15 @@ func StartAPIServer(config *Config,
2337
}
2438

2539
router := gin.Default()
40+
41+
// Automatically replace the default listening address in the docker with `0.0.0.0`
42+
if isRunningInDockerContainer() {
43+
const localhost = "127.0.0.1:"
44+
if strings.HasPrefix(config.API, localhost) {
45+
config.API = strings.Replace(config.API, localhost, "0.0.0.0:", 1)
46+
}
47+
}
48+
2649
server := &http.Server{
2750
Addr: config.API,
2851
Handler: router,

0 commit comments

Comments
 (0)