首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Adminer &迁移不能连接到Postgresql Docker容器

Adminer &迁移不能连接到Postgresql Docker容器
EN

Stack Overflow用户
提问于 2020-05-04 13:42:23
回答 1查看 3K关注 0票数 3

我有三个码头容器(postgresql、adminer和go/migrate),我已经向主机公开了adminer和postgres端口。我可以在浏览器中访问adminer,postico也可以连接到DB。当我试图从adminer内部连接到db时,它会引发以下错误:

代码语言:javascript
复制
SQLSTATE[08006] [7] could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP
connections on port 5432? could not connect to server: Address not available
Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?

迁移容器也抛出此错误:

error: dial tcp: 127.0.0.1:5432: connect: connection refused

因此,很明显,容器之间的通信方式存在一个问题。我需要建立一个码头网络吗?

代码语言:javascript
复制
version: '3.1'

services:
  db:
    image: postgres
    environment:
        POSTGRES_DB: mydbname
        POSTGRES_USER: mydbuser
        POSTGRES_PASSWORD: mydbpwd
    ports:
      - "5432:5432"

  migrate:
    image: migrate/migrate
    volumes:
        - .:/migrations
    command: ["-database", "postgres://mydbuser:mydbpwd@localhost:5432/mydbname?sslmode=disable", "-path", "/migrations", "up"]
    links: 
        - db

  adminer:
    image: adminer
    restart: always
    ports:
      - "8081:8080"
    depends_on:
      - db
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-04 13:49:18

您不需要创建链接,停靠-撰写创建默认网络。服务到服务通信也应该使用服务名称,localhost指的是这个容器(迁移)而不是DB容器。

localhost更改为db

代码语言:javascript
复制
  migrate:
    image: migrate/migrate
    volumes:
        - .:/migrations
    command: ["-database", "postgres://mydbuser:mybpwd@db:5432/mydbname?sslmode=disable", "-path", "/migrations", "up"]

因为您的DB服务名称是db,所以您可以使用它的名称连接到db容器。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61593675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档