dongliu5475
2017-10-06 09:00
浏览 115

使用docker-compose时如何从golang应用连接到Postgres?

My docker-compose file

version: "2" 
services:   db:
   restart: always
   image: postgres:latest
   ports:
     - "5435:5432"
   environment:
      POSTGRES_PASSWORD: password
      POSTGRES_USER: user
      POSTGRES_DB: db   adminer:    
   web:
      image: golang:1.7
      working_dir: /go/src/app
      command: go run bot.go
      ports:
        - "3000:3000"
      volumes:
        - ./bot:/go/src/app
      links:
        - db
      environment:
      PORT: 3000
      CONNECTION_STRING_DEV: postgres://user:password@db/db

and my bot.go, where I try connect

db, err = sql.Open("postgres", "user=user password=password host=db dbname=db port=5432 sslmode=verify-full ")

When I bring up my containers, I see errors:

panic: dial tcp 5.61.14.99:5432: getsockopt: connection refused

I changed the port on 5432 and tried connect like this:

db, err = sql.Open("postgres", "postgres://user:password@db/db")

but I get the same errors

What's wrong with my docker-compose setup?

图片转代码服务由CSDN问答提供 功能建议

我的docker-compose文件

 版本:“ 2”  
服务:db:
重新启动:始终
图像:postgres:最新
端口:
-“ 5435:5432” 
环境:
 POSTGRES_PASSWORD:密码
 POSTGRES_USER:用户
 POSTGRES_DB:数据库管理员:  
网络:
图片:golang:1.7 
 working_dir:/ go / src / app 
命令:运行bot.go 
端口:
-“ 3000:3000” 
卷:
-。  / bot:/ go / src / app 
链接:
-db 
环境:
端口:3000 
 CONNECTION_STRING_DEV:postgres:// user:password @ db / db 
   
 
 

和我的bot.go,尝试连接的地方

  db,err = sql.Open(“ postgres”,“ user =用户密码= 密码host = db dbname = db port = 5432 sslmode = verify-full“)
   
 
 

提起容器时,我看到错误: \ n

  panic:拨打tcp 5.61.14.99:5432:getsockopt:连接被拒绝
   
 
 

我在5上更改了端口 432,并尝试这样连接:

  db,err = sql.Open(“ postgres”,“ postgres:// user:password @ db / db”)
 <  / code>  
 
 

,但我遇到相同的错误

我的docker-compose设置有什么问题?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题