Skip to content

Commit 73c19fd

Browse files
authored
Merge pull request #97 from soulteary/feat-docker-detect
2 parents f583b9b + aa91c24 commit 73c19fd

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ docker build -t grimd:latest . && \
137137
docker run -v $PWD/sources:/sources --rm -it -P --name grimd-test grimd:latest --config /sources/grimd.toml --update
138138
```
139139

140-
For Mac docker set `'api = "0.0.0.0:8080"'` instead of `'api = "127.0.0.1:8080"'`.
140+
By default, if the program runs in a docker, it will automatically replace `127.0.0.1` in the default configuration with `0.0.0.0` to ensure that the API interface is available.
141+
141142
```shell
142143
curl -H "Accept: application/json" http://127.0.0.1:55006/application/active
143144
```

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)