I trying connection to my postgresql database with Docker:
package main
import (
"fmt"
"log"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
type Product struct {
gorm.Model
Code string
Price uint
}
var db *gorm.DB
func init() {
connection := fmt.Sprintf("host=db sslmode=disable user=dnz-dev password=dnz-dev")
db, err := gorm.Open("postgres", connection)
if err != nil {
log.Fatalln(err)
}
defer db.Close()
}
func main() {
// Migrate the schema
db.AutoMigrate(&Product{})
}
and docker-compose
version: "3.3"
services:
db:
build: ./dnz-db
container_name: dnz-database
ports:
- "6000:5432"
volumes:
- ./dnz-db/data:/var/lib/postgresql/data
environment:
- POSTGRES_USER=dnz-dev
- POSTGRES_PASSWORD=dnz-dev
api:
build: ./dnz-api
container_name: dnz-api
volumes:
- ./dnz-api:/go/src/app
ports:
- "5000:3000"
depends_on:
- db
I run docker-compose up --build
and I get this error:
Attaching to dnz-database, dnz-api dnz-api | 2017/11/05 10:23:46 dial tcp 172.21.0.2:5432: getsockopt: connection refused dnz-api | exit status 1 dnz-api exited with code 1
What I doing wrong?