iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >初始化数据库失败,出现错误无法连接到`host=db user=database=`:拨号错误(拨号tcp xxxx:连接:连接被拒绝)
  • 626
分享到

初始化数据库失败,出现错误无法连接到`host=db user=database=`:拨号错误(拨号tcp xxxx:连接:连接被拒绝)

2024-02-06 09:02:17 626人浏览 八月长安
摘要

问题内容 每当启动 Docker 容器服务时,我都会收到 failed to initialize 错误。 version: '3' services: app: con

问题内容

每当启动 Docker 容器服务时,我都会收到 failed to initialize 错误。

version: '3'

services:
  app:
    container_name: api
    build:
      context: .
      dockerfile: local.dockerfile
    ports:
      - "9090:9090"
      - "40000:40000"
    security_opt:
      - "seccomp:unconfined"
    cap_add:
      - sys_ptrace
    restart: on-failure
    environment:
      port: 9090
      db_conn: "postgres://admin:pass@db:5432/test?sslmode=disable"
    volumes:
      - .:/app
    depends_on:
      - db
    links:
      - db

  db:
    image: postgres
    container_name: db
    ports:
      - "5432:5432"
    environment:         
      postgres_user: "admin"
      postgres_passWord: "pass"
      postgres_db: "test"
      tz: "utc"
      pgtz: "utc"
    volumes:
      - ./tmp:/var/lib/postgresql/data

我使用air进行实时重新加载,请找到air.toml文件

root="."
tmp_dir="tmp"

[build]
cmd="Go build -GCflags=\"all=-N -l\" -o ./bin/main ."
bin="/app/bin"
full_bin="/app/bin/main"
log="air_errors.log"
include_ext=["go", "yaml"]
exclude_dir=["tmp"]
delay=1000

[log]
time=true


[misc]
clean_on_exit=true




func main() {
    Instance, err = gORM.Open(postgres.Open(conn), &gorm.Config{
            Logger: logger.New(
                log.New(os.Stdout, "", log.LstdFlags), logger.Config{
                    LogLevel: logger.Info,
                    Colorful: true,
                }),
        })
        if err != nil {
            panic("Cannot connect to DB" + err.Error())
        }
    }

如果您再次保存代码并实时重新加载应用程序,则会建立连接


正确答案


需要等待postgres数据库初始化完成。

查看 https://docs。 docker.com/compose/compose-file/compose-file-v3/#healthcheck

db服务添加healthcheck

healthcheck:
    test: ["cmd-shell", "pg_isready"]
    interval: 10s
    timeout: 5s
    retries: 5

并更改 depend_on 如下

depends_on:
  db:
    condition: service_healthy

以上就是初始化数据库失败,出现错误无法连接到`host=db user=database=`:拨号错误(拨号tcp xxxx:连接:连接被拒绝)的详细内容,更多请关注编程网其它相关文章!

--结束END--

本文标题: 初始化数据库失败,出现错误无法连接到`host=db user=database=`:拨号错误(拨号tcp xxxx:连接:连接被拒绝)

本文链接: https://www.lsjlt.com/news/561373.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作