nps icon indicating copy to clipboard operation
nps copied to clipboard

docker 版本日志无法输出到文件

Open magicds opened this issue 3 years ago • 4 comments
trafficstars

Describe the bug

docker 版本日志无法输出到文件

https://hub.docker.com/r/ffdfgdfg/nps

日志仅能通过 docker logs 命令查看, 无法输出到文件。

尝试过如下方式均无法获取:

  1. 配置文件配置 log_path, docker 挂载目录 =>目录下无文件输出
  2. sudo docker cp nps:/var/log/nps.log nps.log => No such container:path: nps:/var/log/nps.log

magicds avatar Mar 29 '22 02:03 magicds

Show your config file nps.conf

看看配置文件

aschenmaker avatar Mar 31 '22 12:03 aschenmaker

@aschenmaker

# log level LevelEmergency->0  LevelAlert->1 LevelCritical->2 LevelError->3 LevelWarning->4 LevelNotice->5 LevelInformational->6 LevelDebug->7
log_level=7
log_path=nps.log

magicds avatar Mar 31 '22 12:03 magicds

问题我认为是dockerfile的原因,dockerfile 没有一个启动参数。

here <-

	if len(os.Args) > 1 && os.Args[1] == "service" {
		_ = logs.SetLogger(logs.AdapterFile, `{"level":`+level+`,"filename":"`+logPath+`","daily":false,"maxlines":100000,"color":true}`)
	} else {
		_ = logs.SetLogger(logs.AdapterConsole, `{"level":`+level+`,"color":true}`)
	}

dockerfile CMD

VOLUME /conf
CMD ["/nps"]

修改一下启动命令重新打包镜像 挂载log volumn就可以了

aschenmaker avatar Mar 31 '22 14:03 aschenmaker

我编译了一个 测试了一下没有问题

docker run -d --name nps-as --net=host -v /root/nps/conf/:/conf -v /var/log/nps/:/logs   aschenmaker/nps:v0.0.1

conf/nps.conf

log_level=7
log_path=/logs/nps.log

日志

tail -f /var/log/nps/nps.log

dockerfile

FROM golang:1.15 as builder
ARG GOPROXY="https://goproxy.cn,direct"
WORKDIR /go/src/ehang.io/nps
COPY . .
RUN go get -d -v ./... 
RUN CGO_ENABLED=0 go build -ldflags="-w -s -extldflags -static" ./cmd/nps/nps.go

FROM scratch
COPY --from=builder /go/src/ehang.io/nps/nps /
COPY --from=builder /go/src/ehang.io/nps/web /web
VOLUME /conf
CMD ["/nps", "service"]

aschenmaker avatar Mar 31 '22 15:03 aschenmaker