2

I've set up a GitHub workflow to start a Postgres instance inside a docker container. I then execute my web API to simply prove it connects. This satisfies the workflow.

My workflow

name: Build

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:

  build:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:10.8
        env:
          POSTGRES_USER: test
          POSTGRES_PASSWORD: test
          POSTGRES_DB: test
          POSTGRES_PORT: 5432
        ports:
          - 5432/tcp
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

    steps:
      - uses: actions/checkout@v2

      - name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.15

      - name: Run
        env:
          # These are the expected envs in my code
          DB_HOST: localhost
          DB_USER: test
          DB_PASSWORD: test
          DB_NAME: test
          DB_PORT: 5432
          DB_DIALECT: postgres
          PORT: 8080
        run: make run

For brevity I've trimmed irrelavent bits of code out, this is my database package:

// Config Model
type Config struct {
    Host     string
    Name     string
    User     string
    Password string
    Port     string
    Dialect  string
}

// Open a new connection to a database
func Open(c Config) (*gorm.DB, error) {

    dsn := fmt.Sprintf("host=%s port=%s dbname=%s password=%s user=%s sslmode=disable",
        c.Host,
        c.Port,
        c.Name,
        c.Password,
        c.User,
    )

    db, err := gorm.Open(postgres.New(postgres.Config{
        DriverName: c.Dialect,
        DSN:        dsn,
    }), &gorm.Config{})

    if err != nil {
        return nil, fmt.Errorf("sql.Open: %v", err)
    }

    return db, nil
}

And this is called from my main package

func main() {
    // Create Database Configuration
    dbConfig := database.Config{
        Host:     os.Getenv("DB_HOST"),
        Name:     os.Getenv("DB_NAME"),
        User:     os.Getenv("DB_USER"),
        Port:     os.Getenv("DB_PORT"),
        Password: os.Getenv("DB_PASSWORD"),
        Dialect:  os.Getenv("DB_DIALECT"),
    }

    // Connect to database
    db, err := database.Open(dbConfig)
    if err != nil {
        log.Fatalf("failed to connect to database: %s", err)
    }
}

The code fails to connect during the workflow with the given error:

go run -ldflags "-X main.Version=afc0042" cmd/api/main.go
2021/02/10 09:55:12 Running Version: afc0042
2021/02/10 09:55:12 failed to connect to database: sql.Open: dial tcp [::1]:5432: connect: connection refused

2021/02/10 09:55:12 /home/runner/work/database/database.go:32
[error] failed to initialize database, got error dial tcp [::1]:5432: connect: connection refused

1 Answer 1

2

you can use localhost when only you are trying to connect your server locally but as docker is not local in your machine so you can't use localhost except you are inside docker container.

try with port forward.

ports:
  - 5432:5432 //update this one
Sign up to request clarification or add additional context in comments.

6 Comments

It's there, the code runs fine locally. I can start a docker container with Postgres and connect. I just can't seem to get it to connect on github.
are you trying from inside docker or from outside docker?
I believe outside. So I want Postgres inside the container and then I want to run my go file outside of it which will try and connect to the DB using the above code.
first you need to do port forward. as you connect to docker from localhost. do you have done that?
Ahh, yes. - 5432:5432 i've updated that and now it seems to work. If you like to post an answer I can accept it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.