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

连接时出现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.

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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化