Golang testcontainers,无法使网络工作

golang testcontainers,无法使网络工作

php小编草莓在使用Golang testcontainers时遇到了一个问题,即无法使网络正常工作。Golang testcontainers是一个用于在测试中运行容器的工具,它可以帮助开发人员在测试环境中快速启动和销毁容器。然而,在实际使用中,php小编草莓发现无法在容器中正常使用网络功能,这给测试带来了一定的困扰。接下来,我们将一起探讨这个问题的解决方案。

问题内容

我正在尝试在我的微服务中创建一些测试,我想创建一个网络,将我的数据库测试容器(postgres)和我的微服务测试容器附加到该网络。无论我尝试什么,我都无法让我的微服务连接到数据库。我的微服务是使用 Fiber 和 Gorm 的 golang。我尝试连接到 db.go 配置文件中的数据库,如下所示:

func SetupDB(port string, host string) *gorm.DB { dsn := "host=" + host + " user=postgres password=password dbname=prescription port=" + port + " sslmode=disable" db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) if err != nil { panic("error connecting to database") } db.AutoMigrate(&model.Prescription{}) return db }登录后复制