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

huangapple go评论172阅读模式
英文:

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

问题

每当我启动我的Docker容器服务时,都会出现“无法初始化”的错误。

  1. version: '3'
  2. services:
  3. app:
  4. container_name: api
  5. build:
  6. context: .
  7. dockerfile: local.Dockerfile
  8. ports:
  9. - "9090:9090"
  10. - "40000:40000"
  11. security_opt:
  12. - "seccomp:unconfined"
  13. cap_add:
  14. - SYS_PTRACE
  15. restart: on-failure
  16. environment:
  17. PORT: 9090
  18. DB_CONN: "postgres://admin:pass@db:5432/test?sslmode=disable"
  19. volumes:
  20. - .:/app
  21. depends_on:
  22. - db
  23. links:
  24. - db
  25. db:
  26. image: postgres
  27. container_name: db
  28. ports:
  29. - "5432:5432"
  30. environment:
  31. POSTGRES_USER: "admin"
  32. POSTGRES_PASSWORD: "pass"
  33. POSTGRES_DB: "test"
  34. TZ: "UTC"
  35. PGTZ: "UTC"
  36. volumes:
  37. - ./tmp:/var/lib/postgresql/data

我正在使用air进行实时重载,请找到air.toml文件。

  1. root="."
  2. tmp_dir="tmp"
  3. [build]
  4. cmd="go build -gcflags=\"all=-N -l\" -o ./bin/main ."
  5. bin="/app/bin"
  6. full_bin="/app/bin/main"
  7. log="air_errors.log"
  8. include_ext=["go", "yaml"]
  9. exclude_dir=["tmp"]
  10. delay=1000
  11. [log]
  12. time=true
  13. [misc]
  14. clean_on_exit=true
  1. func main() {
  2. Instance, err = gorm.Open(postgres.Open(conn), &gorm.Config{
  3. Logger: logger.New(
  4. log.New(os.Stdout, "", log.LstdFlags), logger.Config{
  5. LogLevel: logger.Info,
  6. Colorful: true,
  7. }),
  8. })
  9. if err != nil {
  10. panic("Cannot connect to DB" + err.Error())
  11. }
  12. }

如果您再次保存代码并使用air进行实时重载应用程序,则连接将建立。

英文:

I am getting the failed to initialize error whenever I start up my docker container service.

  1. version: '3'
  2. services:
  3. app:
  4. container_name: api
  5. build:
  6. context: .
  7. dockerfile: local.Dockerfile
  8. ports:
  9. - "9090:9090"
  10. - "40000:40000"
  11. security_opt:
  12. - "seccomp:unconfined"
  13. cap_add:
  14. - SYS_PTRACE
  15. restart: on-failure
  16. environment:
  17. PORT: 9090
  18. DB_CONN: "postgres://admin:pass@db:5432/test?sslmode=disable"
  19. volumes:
  20. - .:/app
  21. depends_on:
  22. - db
  23. links:
  24. - db
  25. db:
  26. image: postgres
  27. container_name: db
  28. ports:
  29. - "5432:5432"
  30. environment:
  31. POSTGRES_USER: "admin"
  32. POSTGRES_PASSWORD: "pass"
  33. POSTGRES_DB: "test"
  34. TZ: "UTC"
  35. PGTZ: "UTC"
  36. volumes:
  37. - ./tmp:/var/lib/postgresql/data

I am using air for live reload, please find the air.toml file

  1. root="."
  2. tmp_dir="tmp"
  3. [build]
  4. cmd="go build -gcflags=\"all=-N -l\" -o ./bin/main ."
  5. bin="/app/bin"
  6. full_bin="/app/bin/main"
  7. log="air_errors.log"
  8. include_ext=["go", "yaml"]
  9. exclude_dir=["tmp"]
  10. delay=1000
  11. [log]
  12. time=true
  13. [misc]
  14. clean_on_exit=true
  15. func main() {
  16. Instance, err = gorm.Open(postgres.Open(conn), &gorm.Config{
  17. Logger: logger.New(
  18. log.New(os.Stdout, "", log.LstdFlags), logger.Config{
  19. LogLevel: logger.Info,
  20. Colorful: true,
  21. }),
  22. })
  23. if err != nil {
  24. panic("Cannot connect to DB" + err.Error())
  25. }
  26. }

The connection gets established if you save the code again and air live reload the appliation

答案1

得分: 1

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

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

db服务添加一个healthcheck

  1. healthcheck:
  2. test: ["CMD-SHELL", "pg_isready"]
  3. interval: 10s
  4. timeout: 5s
  5. retries: 5

并将depend_on更改如下:

  1. depends_on:
  2. db:
  3. condition: service_healthy
英文:

You need to wait until the postgres database has been initialized.

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

Add a healthcheck for db service

  1. healthcheck:
  2. test: ["CMD-SHELL", "pg_isready"]
  3. interval: 10s
  4. timeout: 5s
  5. retries: 5

And change depend_on as below

  1. depends_on:
  2. db:
  3. condition: service_healthy

huangapple
  • 本文由 发表于 2022年12月16日 22:23:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/74825752.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定