1

Problem

$ go run cmd/syndicate/main.go 2021/01/25 16:37:25 error connecting to database: dial tcp: lookup db: no such host

Unable to connect to database when attempting to run:

$ go run cmd/syndicate/main.go
2021/01/25 16:37:25 error connecting to database: dial tcp: lookup db: no such host

&

$ migrate -source file://migrations -database postgres://postgres:secret@db:5432/syndicate?sslmode=disable up
error: dial tcp: lookup db on [2001:558:feed::1]:53: no such host

What do these two commands have in common?... Database URL. I am nearly certain my database URL is incorrect.

I have verified my postgres container is running:

$ docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED      STATUS      PORTS                    NAMES
4e578bf646c7   adminer    "entrypoint.sh docke…"   3 days ago   Up 3 days   0.0.0.0:8080->8080/tcp   syndicate_adminer_1
729fc179aa6f   postgres   "docker-entrypoint.s…"   3 days ago   Up 3 days   5432/tcp                 syndicate_db_1

Here's where I might be overlooking something...

$ docker-compose ps
       Name                      Command               State           Ports         
-------------------------------------------------------------------------------------
syndicate_adminer_1   entrypoint.sh docker-php-e ...   Up      0.0.0.0:8080->8080/tcp
syndicate_db_1        docker-entrypoint.sh postgres    Up      5432/tcp    

5432/tcp???

I see that my adminer container is clearly mapped to my local port (0.0.0.0:8080->8080/tcp), however my postgres container is only showing 5432/tcp (and not 0.0.0.0:5432->5432/tcp)

  • I am new to docker.. Can anyone explain why my postgres port isn't associated with my local port?
  • Am I on the right track?

Here's my docker-compose.yml:

version: "3.8"

services:
  db:
    image: postgres
    environment:
      POSTGRES_DB: $POSTGRES_DB
      POSTGRES_USER: $POSTGRES_USER
      POSTGRES_PASSWORD: $POSTGRES_PASSWORD

  migrate:
    image: migrate/migrate
    volumes:
      - ./migrations:/migrations
    depends_on:
      - db
    command: -source=file://migrations -database postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@db:5432/$POSTGRES_DB?sslmode=disable up

  adminer:
    image: adminer
    restart: always
    ports:
      - "8080:8080"
    environment:
      ADMINER_DEFAULT_SERVER: db
    depends_on:
      - db
  • PS. I tried adding port: "5432:5432" variable for db servicd

Browse my repository at this time in history

Thank you! Connor

2 Answers 2

2

add to db service

ports: 
   - "5432:5432"
Sign up to request clarification or add additional context in comments.

Comments

1

migrate -path D:/works/go/go-fiber-api-server/backend/platform/migrations -database "postgres://postgres:password@cgapp-postgres:5432/postgres?sslmode=disable" up error: dial tcp: lookup cgapp-postgres: no such host

Then I have fixed this issue to change db-host name(cgapp-postgres) into "IP ADDRESS" or host.docker.internal.

migrate -path D:/works/go/go-fiber-api-server/backend/platform/migrations -database "postgres://postgres:[email protected]:5432/postgres?sslmode=disable" up 1/u create_init_tables (20.0987ms)

migrate -path D:/works/go/go-fiber-api-server/backend/platform/migrations -database "postgres://postgres:[email protected]:5432/postgres?sslmode=disable" up 1/u create_init_tables (20.0987ms)

"host.docker.internal" works.

Comments

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.