doufuxi7093 2017-11-05 10:29
浏览 106
已采纳

连接时出现Gorm + Docker错误

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?

  • 写回答

1条回答 默认 最新

  • drxv39706 2017-11-05 10:54
    关注

    You are unable to connect to your psql container because you haven't linked it. Take a look at Docker-Compose documentation on links.

    Also, I'm assuming you aren't scheduling your container startup order. Psql container must start before golang's one. Take a look at Docker Compose documentation on startup order. You can use wait-for-it to achieve such objective ast stated in the docs. Just wget it and save it in your project.

    I don't know the contents of your Dockerfile but I'll assume it's something like:

    FROM golang:1.9
    
    RUN mkdir -p /go/src/github.com/pavel/gorm-psql
    WORKDIR /go/src/github.com/pavel/gorm-psql
    
    ADD . /go/src/github.com/pavel/gorm-psql
    
    RUN go get -v
    

    So, your docker-compose.yml should be edited to first run wait-for-it.sh and link psql container to your app with something like:

    version: '3.3'
    services:
      db:
        image: postgres
        environment:
          POSTGRES_DB: dnz-dev
          POSTGRES_USER: dnz-dev
          POSTGRES_PASSWORD: dnz-dev
        ports:
          - 6000:5432
      api:
        build: .
        command: ["./wait-for-it.sh", "db:6000", "--", "go", "run", "main.go"]
        volumes:
          - .:/go/src/github.com/pavel/gorm-psql
        ports:
          - "5000:3080"
        depends_on:
          - db
        links:
          - db
    

    If your main has another name just change it. Edit your volumes to point whatever path you need. I've set a standard one with /go/src/github.com/pavel/gorm-psql. I'm assuming you created a db named dnz-dev, if the name is different just edit it.

    $ go env:
    
    . . .
    GOPATH="/home/pavel/go"
    GOROOT="/usr/lib/go"
    . . .
    

    Just run docker-compose up and it should work just fine. I'm relying on postgresql and golang latest images.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功