“Postgres数据库系统已准备好接受连接”和Docker Compose

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

Posgres "database system is ready to accept connections" and docker compose

问题

我是新手,正在尝试创建一个简单的.NET应用程序,在Docker容器中运行一些微服务。其中一个容器包含一个Postgres数据库。

我的docker-compose.yml文件如下所示....

我的docker-compose.override.yml文件如下所示....

阅读了一些其他StackOverflow帖子,我得出的印象是discountdb部分的healthcheck将确保Postgres连接保持打开,以便我的.NET服务可以运行其"seed"?

但是当我运行docker-compose -f docker-compose.yml -f .\docker-compose.override.yml up -d时,我在portainer中查看discountdb的日志时看到以下内容....

我漏掉了什么?我查看了其他一些解决方案,需要安装或运行shell命令,但我在这方面有些新手,不太熟悉如何做。

英文:

I am new to docker and trying to create a simple .NET app that runs some microservices in docker containers. One of the containers has a postgres database.

My docker-compose.yml file looks like this....

  1. version: '3.4'
  2. services:
  3. catalogdb:
  4. image: mongo
  5. basketdb:
  6. image: redis:alpine
  7. discountdb:
  8. image: postgres
  9. portainer:
  10. image: portainer/portainer-ce
  11. catalog.api:
  12. image: ${DOCKER_REGISTRY-}catalogapi
  13. build:
  14. context: .
  15. dockerfile: Services/Catalog/Catalog.API/Dockerfile
  16. basket.api:
  17. image: ${DOCKER_REGISTRY-}basketapi
  18. build:
  19. context: .
  20. dockerfile: Services/Basket/Basket.API/Dockerfile
  21. discount.api:
  22. image: ${DOCKER_REGISTRY-}discountapi
  23. build:
  24. context: .
  25. dockerfile: Services/Discount/Discount.API/Dockerfile
  26. volumes:
  27. mongo_data:
  28. portainer_data:
  29. postgres_data:

My docker-compose.override.yml file looks like this....

  1. version: '3.4'
  2. services:
  3. catalogdb:
  4. container_name: catalogdb
  5. restart: always
  6. ports:
  7. - "27018:27017"
  8. volumes:
  9. - mongo_data:/data/db
  10. basketdb:
  11. container_name: basketdb
  12. restart: always
  13. ports:
  14. - "6379:6379"
  15. discountdb:
  16. container_name: discountdb
  17. restart: always
  18. ports:
  19. - "5430:5432"
  20. command: postgres
  21. environment:
  22. - POSTGRES_USER=admin
  23. - POSTGRES_PASSWORD=admin1234
  24. - POSTGRES_DB=DiscountDb
  25. healthcheck:
  26. test: [ "CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}" ]
  27. interval: 10s
  28. timeout: 5s
  29. retries: 5
  30. volumes:
  31. - postgres_data:/var/lib/postgesql/data/
  32. portainer:
  33. container_name: portainer
  34. restart: always
  35. ports:
  36. - "8080:8000"
  37. - "9000:9000"
  38. volumes:
  39. - /var/run/docker.sock:/var/run/docker.sock
  40. - portainer_data:/data
  41. catalog.api:
  42. container_name: catalog.api
  43. environment:
  44. - ASPNETCORE_ENVIRONMENT=Development
  45. - "DatabaseSettings:ConnectionString=mongodb://catalogdb:27017"
  46. depends_on:
  47. - catalogdb
  48. ports:
  49. - "8000:80"
  50. basket.api:
  51. container_name: basket.api
  52. environment:
  53. - ASPNETCORE_ENVIRONMENT=Development
  54. - "CacheSettings:ConnectionString=basketdb:6379"
  55. depends_on:
  56. - basketdb
  57. ports:
  58. - "8001:80"
  59. discount.api:
  60. container_name: discount.api
  61. environment:
  62. - ASPNETCORE_ENVIRONMENT=Development
  63. - "DatabaseSettings:ConnectionString=Server=discountdb;Port=5430;Database=DiscountDb;User Id=admin;Password=admin1234;"
  64. depends_on:
  65. - discountdb
  66. ports:
  67. - "8002:80"

Reading some other StackOverflow items I was under the impression that the healthcheck portion of the discountdb section would ensure the postgres connection stays open so that my .net service can run it's "seed"?

But when I run

  1. docker-compose -f docker-compose.yml -f .\docker-compose.override.yml up -d

I see the following in my logs for the discountdb when I look at the log for that in portainer....

  1. This user must also own the server process.
  2. The database cluster will be initialized with locale "en_US.utf8".
  3. The default database encoding has accordingly been set to "UTF8".
  4. The default text search configuration will be set to "english".
  5. Data page checksums are disabled.
  6. fixing permissions on existing directory /var/lib/postgresql/data ... ok
  7. creating subdirectories ... ok
  8. selecting dynamic shared memory implementation ... posix
  9. selecting default max_connections ... 100
  10. selecting default shared_buffers ... 128MB
  11. selecting default time zone ... Etc/UTC
  12. creating configuration files ... ok
  13. running bootstrap script ... ok
  14. performing post-bootstrap initialization ... ok
  15. initdb: warning: enabling "trust" authentication for local connections
  16. initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.
  17. syncing data to disk ... ok
  18. Success. You can now start the database server using:
  19. pg_ctl -D /var/lib/postgresql/data -l logfile start
  20. waiting for server to start....2023-05-18 01:47:02.074 UTC [48] LOG: starting PostgreSQL 15.3 (Debian 15.3-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
  21. 2023-05-18 01:47:02.076 UTC [48] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
  22. 2023-05-18 01:47:02.083 UTC [51] LOG: database system was shut down at 2023-05-18 01:47:01 UTC
  23. 2023-05-18 01:47:02.090 UTC [48] LOG: database system is ready to accept connections
  24. done
  25. server started
  26. CREATE DATABASE
  27. /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
  28. waiting for server to shut down....2023-05-18 01:47:02.289 UTC [48] LOG: received fast shutdown request
  29. 2023-05-18 01:47:02.291 UTC [48] LOG: aborting any active transactions
  30. 2023-05-18 01:47:02.293 UTC [48] LOG: background worker "logical replication launcher" (PID 54) exited with exit code 1
  31. 2023-05-18 01:47:02.293 UTC [49] LOG: shutting down
  32. 2023-05-18 01:47:02.295 UTC [49] LOG: checkpoint starting: shutdown immediate
  33. 2023-05-18 01:47:02.370 UTC [49] LOG: checkpoint complete: wrote 918 buffers (5.6%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.015 s, sync=0.054 s, total=0.077 s; sync files=250, longest=0.003 s, average=0.001 s; distance=4217 kB, estimate=4217 kB
  34. 2023-05-18 01:47:02.377 UTC [48] LOG: database system is shut down
  35. done
  36. server stopped
  37. PostgreSQL init process complete; ready for start up.
  38. 2023-05-18 01:47:02.414 UTC [1] LOG: starting PostgreSQL 15.3 (Debian 15.3-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
  39. 2023-05-18 01:47:02.414 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
  40. 2023-05-18 01:47:02.414 UTC [1] LOG: listening on IPv6 address "::", port 5432
  41. 2023-05-18 01:47:02.418 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
  42. 2023-05-18 01:47:02.424 UTC [64] LOG: database system was shut down at 2023-05-18 01:47:02 UTC
  43. 2023-05-18 01:47:02.429 UTC [1] LOG: database system is ready to accept connections

What am I missing here? I've looked at other solutions that require installing or running shell commands, but I am somewhat of a noob here and not really familiar with how to do that?

答案1

得分: 1

Your containers all run within a network created by Docker Compose. That's how you can use the service names as hosts.

A common misconception about port mapping is that it has an effect within the network. It does not.

A mapping like "5430:5432" maps your host port 5430 to the container port 5432. This allows you to connect host applications to localhost:5430 in order to query the Postgres DB.

But within the network, all container ports are untouched. The Postgres DB is available on discountdb:5432.

In short, your DatabaseSettings:ConnectionString should use Server=discountdb;Port=5432.

英文:

Your containers all run within a network created by Docker Compose. That's how you can use the service names as hosts.

A common misconception about port mapping is that it has an effect within the network. It does not.

A mapping like "5430:5432" maps your host port 5430 to the container port 5432. This allows you to connect host applications to localhost:5430 in order to query the Postgres DB.

But within the network, all container ports are untouched. The Postgres DB is available on discountdb:5432.

In short, your DatabaseSettings:ConnectionString should use Server=discountdb;Port=5432.

huangapple
  • 本文由 发表于 2023年5月18日 09:49:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277228.html
匿名

发表评论

匿名网友

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

确定