简体   繁体   English

Golang应用程序如何在AWS服务器上使用Docker与Postgres对话?

[英]How does golang app talk to postgres using docker in aws servers?

I have a docker instance like this 我有一个这样的码头工人实例

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

running in a server 在服务器上运行

And I have my golang app wrapped by docker running in the same server 我的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)
}

The above is not working because the ip is not correct. 上面的方法不起作用,因为ip不正确。

Since I am using mac, I need to use docker machine ip to connect to the docker postgres db, but in aws I don't 由于我使用的是Mac,因此需要使用docker machine ip连接到docker postgres db,但是在AWS中我不需要

What is a good way to configure this? 什么是配置此内容的好方法?

Are you asking how to configure different environments for your local development and for AWS? 您是否在问如何为本地开发和AWS配置不同的环境?

If so, you could use environment variables like this: 如果是这样,则可以使用如下环境变量:

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

And simply provide the GO_ENV environment variable set to production to your docker container on AWS. 只需将设置为productionGO_ENV环境变量提供给GO_ENV容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM