Unable to Get Containers to Connect Correctly

247 views Asked by At

I am relatively new to Docker and I have been trying to connect my web app, psql, and the golang-migrate function. However, I have faced error after error and I have finally arrived to a point whereby I cannot figure out what's going on.

My app provides this error:

Failed to connect to host=/tmp user=root database=: dial error (dial unix /tmp/.s.PGSQL.5432: connect: no such file or directory)

My migrate provides this error:

error: dial tcp: lookup postgres on 127.0.0.11:53: server misbehaving

I have been going through various forums but non seem to incorporate the migrate functionality.

Here is a copy of my updated docker-compose.yml file:

version: "3.1"

services:
db:
 image: postgres:latest
 container_name: db_postgres
 environment:
   - PG_USER=postgres  
   - POSTGRES_PASSWORD=password
   - POSTGRES_DB=buscompanion
   - DATABASE_HOST=fullstack-postgres
 ports:
   - "5432:5432"
 healthcheck:
   test: ["CMD-SHELL", "pg_isready -d buscompanion -U postgres"]
   interval: 10s
   timeout: 30s
   retries: 5
 networks:
   - fullstack

app:
 container_name: full_app
 build: .
 ports: 
   - 4000:4000
 restart: on-failure
 volumes:
     - api:/usr/src/app/
 depends_on:
     - db
     - migrate      
 networks:
     - fullstack

migrate:
 image: migrate/migrate
 networks:
   - fullstack
 volumes:
   - .:/migrations/migrations
 entrypoint: migrate
 command: -path /migrations -database postgres://postgres:password@postgres:5432/buscompanion?sslmode=disable -verbose up
 depends_on:
   db:
     condition: service_healthy

volumes:
api:
networks:
 fullstack:
   driver: bridge



EDIT Here is a how Im doing my sql.Open

func main()

addr := flag.String("port", ":4000", "HTTP network address")                                //Provides general information on where site will be located
dsn := flag.String("dsn", os.Getenv("POSTGRES_URL"), "PostgreSQL DSN (Data Source Name)") //Connects to PSQL Database. Must create shortcut and replace "COMPUTE_DB_DSN"
flag.Parse()

db, err := openDB(*dsn) //Opens Databse
if err != nil {         //Enters if the Database is not entered
    log.Println(err)
    return
}

openDB() function

func openDB(dsn string) (*sql.DB, error) {
    db, err := sql.Open("pgx", dsn)
    if err != nil {
        return nil, err
    }
    //use a context to check if the DB is reachable
    ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) //specify timeout
    defer cancel()                                                          //+Defer attached to a function and executes as the last thing
    //lets ping the DB
    err = db.PingContext(ctx)
    if err != nil {
        return nil, err
    }
    return db, nil
}
0

There are 0 answers