Pavel Perevezencev
Pavel Perevezencev

Reputation: 2978

Gorm + Docker error while connection

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?

Upvotes: 1

Views: 3886

Answers (1)

AndreaM16
AndreaM16

Reputation: 3985

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.

Upvotes: 4

Related Questions