在正常開發(包括構建二進制文件)中,go會查找文件,但在生產中,當使用映像時,它不會。config.yamlDockerfile我的項目文件夾是:|-cmd |- server |- main.go |- server (executable when built)|-config |-config.yaml |-config.goconfig.go 是:func readConfigFile(viperConfig ViperConfig) { // Set default values if nil if viperConfig.ConfigName == "" { viperConfig.ConfigName = "config" } if viperConfig.ConfigType == "" { viperConfig.ConfigType = "yaml" } if viperConfig.ConfigAddPath == "" { // main execute it, so it's path is relative to who execute it. viperConfig.ConfigAddPath = "../../config" } // Read the config viper.SetConfigName(viperConfig.ConfigName) viper.SetConfigType(viperConfig.ConfigType) // This path is from main viper.AddConfigPath(viperConfig.ConfigAddPath) err := viper.ReadInConfig() if err != nil { log.Panic(err) }}我的 dockerfile 是:FROM golang:alpine AS baseWORKDIR /appCOPY . .RUN go build -o ./cmd/server/server ./cmd/server/main.goFROM alpine AS finalWORKDIR /appCOPY --from=base /app/cmd/server/server ./cmd/server/COPY --from=base /app/config/config.yaml ./config/CMD [ "./cmd/server/server" ]從該構建映像運行容器時顯示的錯誤(panic)是:2021/05/12 18:08:32 Config File "config" Not Found in "[/config]"如何指向配置文件所在的位置?/app/config/config.yaml
在開發上找到的 Viper 配置文件,但在使用 Dockerfile 的生產環境中找不到
慕碼人2483693
2022-08-30 15:23:09