简体   繁体   English

无法初始化数据库,出现错误无法连接到`host=db user=database=`:拨号错误(dial tcp xxxx: connect: connection refused)

[英]failed to initialize database, got error failed to connect to `host=db user= database=`: dial error (dial tcp xxxx: connect: connection refused)

I am getting the failed to initialize error whenever I start up my docker container service.每当我启动我的 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

I am using air for live reload, please find the air.toml file我正在使用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())
        }
    }

The connection gets established if you save the code again and air live reload the appliation如果您再次保存代码并实时重新加载应用程序,则会建立连接

You need to wait until the postgres database has been initialized.您需要等到postgres数据库初始化完成。

Have a look at https://docs.docker.com/compose/compose-file/compose-file-v3/#healthcheck看看https://docs.docker.com/compose/compose-file/compose-file-v3/#healthcheck

Add a healthcheck for db servicedb服务添加健康healthcheck

healthcheck:
    test: ["CMD-SHELL", "pg_isready"]
    interval: 10s
    timeout: 5s
    retries: 5

And change depend_on as below并更改depend_on如下

depends_on:
  db:
    condition: service_healthy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 连接到在 Docker 上运行的 Postgres 数据库时出错:“拨打 tcp:[...] 没有这样的主机” - Error connecting to Postgres database running on Docker: “dial tcp: […] no such host” 无法使用 go 和 docker 连接到 mysql 服务器 - 拨打 tcp 127.0.0.1:3306:连接:连接被拒绝 - Unable to connect to mysql server with go and docker - dial tcp 127.0.0.1:3306: connect: connection refused Node js mongodb数据库连接错误无法连接到服务器 - Node js mongodb database connection error failed to connect to server 尝试连接到Joomla数据库时出错:jtablesession :: Store失败DB函数失败,错误号为1146 - Error trying to connect to Joomla database: jtablesession::Store Failed DB function failed with error number 1146 数据库连接失败-错误 - Database Connection Failed - Error 连接数据库时出错,目标计算机主动拒绝它 - error to connect database, target machine actively refused it 数据库连接错误 (3): 无法连接到数据库', - Database connection error (3): Could not connect to database', Jmeter 连接数据库(Postgres)失败 - Failed to connect to the database (Postgres) by Jmeter TALEND:网络错误IOException:连接被拒绝:connect - TALEND: Network error IOException: Connection refused: connect PostgreSQL:无法连接到服务器 - 连接被拒绝错误 - PostgreSQL: could not connect to server - Connection refused error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM