Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
990 views
in Technique[技术] by (71.8m points)

postgresql - Error connecting to Postgres database running on Docker: "dial tcp: [...] no such host"

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

question from:https://stackoverflow.com/questions/65894760/error-connecting-to-postgres-database-running-on-docker-dial-tcp-no-suc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

add to db service

ports: 
   - "5432:5432"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...