Skip to content

config service not work if running with Docker #1880

@Ales999

Description

@Ales999

Hello.

encore version v1.46.19

Simple code:

type OnePrefix struct {
	Prefix config.String
	Asn    config.String
	Ignore config.Bool
}

type AppConfigType struct {
	ProdMode config.Bool              // Development OR Prod mode
	Readonly config.Bool              // Readonly mode
	Prefixes config.Values[OnePrefix] // Test using Prefixes
}

var cfg *AppConfigType = config.Load[*AppConfigType]()

// encore:api public
func GetProdMode(ctx context.Context) (*ProdMode, error) {
	if cfg != nil {
		return &ProdMode{AppMode: cfg.ProdMode()}, nil
	} else {
		log.Println("Alert: CFG is NULL")
		return nil, errs.B().Code(errs.InvalidArgument).Msg("cfg is not initialised").Err()
	}
}

Then, if running as ```encore run`` all work:

Image

If running with docker - panic: runtime error: invalid memory address or nil pointer dereference

2025-04-21 13:57:24 {"level":"debug","time":"2025-04-21T08:57:24Z","message":"maxprocs: Leaving GOMAXPROCS=4: CPU quota undefined"}
2025-04-21 13:57:24 {"level":"info","time":"2025-04-21T08:57:24Z","message":"registered 8 API endpoints"}
2025-04-21 13:57:24 2025/04/21 08:57:24 AppConfig iniutialized
2025-04-21 13:57:24 {"level":"info","time":"2025-04-21T08:57:24Z","message":"listening for incoming HTTP requests"}
2025-04-21 13:57:24 {"level":"info","service":"appconfig","endpoint":"GetProdMode","trace_id":"8pgiqk0omhm7afg8vffelmpj4c","time":"2025-04-21T08:57:24Z","message":"starting request"}
2025-04-21 13:57:24 2025/04/21 08:57:24 Alert: CFG is NULL
2025-04-21 13:57:24 {"level":"error","service":"appconfig","endpoint":"GetProdMode","trace_id":"8pgiqk0omhm7afg8vffelmpj4c","error":"cfg is not initialised","code":"invalid_argument","time":"2025-04-21T08:57:24Z","message":"request failed"}
2025-04-21 13:57:24 {"level":"info","service":"appconfig","endpoint":"GetProdMode","trace_id":"8pgiqk0omhm7afg8vffelmpj4c","duration":0.06476,"code":"invalid_argument","time":"2025-04-21T08:57:24Z","message":"request completed"}
2025-04-21 13:57:24 {"level":"error","error":"invalid_argument: cfg is not initialised","time":"2025-04-21T08:57:24Z","message":"call failed"}
2025-04-21 13:57:24 2025/04/21 08:57:24 invalid_argument: cfg is not initialised
2025-04-21 13:57:24 panic: runtime error: invalid memory address or nil pointer dereference
2025-04-21 13:57:24 [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xf0b81f]
2025-04-21 13:57:24 
2025-04-21 13:57:24 goroutine 49 [running]:
2025-04-21 13:57:24 encore.app/backend/bgpalert.initBgpAlert()
2025-04-21 13:57:24 /home/xxxxx/go/src/local/wanmon/backend/bgpalert/bgpalert.go:27 +0x13f
2025-04-21 13:57:24 encore.dev/appruntime/apisdk/service.doSetupService[...](0xc000418000, 0x227eb40, 0x227eb70)
2025-04-21 13:57:24 /home/xxxxx/.encore/runtimes/go/appruntime/apisdk/service/service.go:82 +0x268
2025-04-21 13:57:24 encore.dev/appruntime/apisdk/service.(*Decl[...]).InitService.func1()
2025-04-21 13:57:24 /home/xxxxx/.encore/runtimes/go/appruntime/apisdk/service/singleton.go:80 +0x32
2025-04-21 13:57:24 encore.dev/appruntime/shared/syncutil.(*Once).Do(0x227eb70, 0x0?)
2025-04-21 13:57:24 /home/xxxxx/.encore/runtimes/go/appruntime/shared/syncutil/once.go:57 +0x98
2025-04-21 13:57:24 encore.dev/appruntime/apisdk/service.(*Decl[...]).InitService(...)
2025-04-21 13:57:24 /home/xxxxx/.encore/runtimes/go/appruntime/apisdk/service/singleton.go:79
2025-04-21 13:57:24 encore.dev/appruntime/apisdk/service.(*Manager).InitializeServices.func1()
2025-04-21 13:57:24 /home/xxxxx/.encore/runtimes/go/appruntime/apisdk/service/service.go:170 +0x53
2025-04-21 13:57:24 created by encore.dev/appruntime/apisdk/service.(*Manager).InitializeServices in goroutine 1
2025-04-21 13:57:24 /home/xxxxx/.encore/runtimes/go/appruntime/apisdk/service/service.go:169 +0x5a

CUE file:

		// By default we're not in read only mode
Readonly: bool | *false
// Mode app
ProdMode: bool | *false

Prefixes: [
	{
		Prefix: "1.1.1.0/24"
		Asn:    "1111"
		Ignore: false
	},
	{
		Prefix: "2.2.2.0/24"
		Asn:    "2222"
		Ignore: false
	},

]

// An application running due to `encore run`
if #Meta.Environment.Type == "development" && #Meta.Environment.Cloud == "local" {
	ProdMode: true
	Readonly: true
}

// An application running in a development environment in the Cloud
if #Meta.Environment.Type == "development" && #Meta.Environment.Cloud != "local" {
	ProdMode: false
}

// An application running in a production environment
if #Meta.Environment.Type == "production" {
	ProdMode: true
}

// An application running in an environment that Encore has created
// for an open Pull Request on Github
if #Meta.Environment.Type == "ephemeral" {
	ProdMode: false
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions