golang应用程序如何在aws服务器中使用docker与postgres对话?

我有一个这样的docker实例

docker run --name my-db-name -e POSTGRES_PASSWORD=mysecretpassword -d postgres:latest 

在服务器上运行

我有我的golang应用程序包装在docker运行在同一台服务器

 func main() { db, _ := sql.Open("postgres", "postgres://postgres:@192.168.99.100:5432/postgres?sslmode=disable") http.HandleFunc("/test", handler) http.ListenAndServe(":8080", nil) } 

以上是不工作,因为IP是不正确的。

由于我使用mac,我需要使用docker机ip连接到dockerpostgres数据库,但在aws我不

什么是configuration这个好方法?

你问如何为你的本地开发和AWSconfiguration不同的环境?

如果是这样,你可以使用这样的环境variables:

 env := os.Getenv("GO_ENV") dbIP := "DOCKER_MACHINE_IP" if env == "production" { dbIP = "AWS_POSTGRES_IP" } 

只需将设置为productionGO_ENV环境variables提供给GO_ENV容器即可。