Failed to initialize database with error Unable to connect to `host=db user=database=`: Dial-up error (dial-up tcp xxxx: connect: connection refused)

王林
Release: 2024-02-06 09:25:14
forward
1236 people have browsed it

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

Question content

Whenever I start the docker container service, I receive the failed to initialize error.

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
Copy after login

I use air for live reloading, please find the air.toml file

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())
        }
    }
Copy after login

If you save the code again and reload the application live, the connection will be established


Correct answer


Need to wait postgresDatabase initialization completed.

View https://docs. docker.com/compose/compose-file/compose-file-v3/#healthcheck

Add healthcheck for db

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

and change depend_on as follows

depends_on:
  db:
    condition: service_healthy
Copy after login

The above is the detailed content of Failed to initialize database with error Unable to connect to `host=db user=database=`: Dial-up error (dial-up tcp xxxx: connect: connection refused). For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!