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

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

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....

version: '3.4'

services:
  catalogdb:
    image: mongo

  basketdb: 
    image: redis:alpine

  discountdb: 
    image: postgres

  portainer:
    image: portainer/portainer-ce

  catalog.api:
    image: ${DOCKER_REGISTRY-}catalogapi
    build:
      context: .
      dockerfile: Services/Catalog/Catalog.API/Dockerfile

  basket.api:
    image: ${DOCKER_REGISTRY-}basketapi
    build:
      context: .
      dockerfile: Services/Basket/Basket.API/Dockerfile

  discount.api:
    image: ${DOCKER_REGISTRY-}discountapi
    build:
      context: .
      dockerfile: Services/Discount/Discount.API/Dockerfile

volumes:
  mongo_data:
  portainer_data:
  postgres_data:

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

version: '3.4'

services:
  catalogdb:
    container_name: catalogdb
    restart: always
    ports:
      - "27018:27017"
    volumes:
      - mongo_data:/data/db
 
  basketdb:
    container_name: basketdb
    restart: always
    ports:
      - "6379:6379"

  discountdb:
      container_name: discountdb
      restart: always
      ports: 
        - "5430:5432"
      command: postgres
      environment:
      - POSTGRES_USER=admin
      - POSTGRES_PASSWORD=admin1234
      - POSTGRES_DB=DiscountDb 
      healthcheck:
        test: [ "CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}" ]
        interval: 10s
        timeout: 5s
        retries: 5
      volumes:
        - postgres_data:/var/lib/postgesql/data/ 

  portainer:
    container_name: portainer
    restart: always
    ports:
      - "8080:8000"
      - "9000:9000"      
    volumes:
    - /var/run/docker.sock:/var/run/docker.sock
    - portainer_data:/data 

  catalog.api:
    container_name: catalog.api
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - "DatabaseSettings:ConnectionString=mongodb://catalogdb:27017"
    depends_on:
      - catalogdb
    ports:
      - "8000:80"

  basket.api:
    container_name: basket.api
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - "CacheSettings:ConnectionString=basketdb:6379"
    depends_on:
      - basketdb
    ports:
      - "8001:80"


  discount.api:
    container_name: discount.api
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - "DatabaseSettings:ConnectionString=Server=discountdb;Port=5430;Database=DiscountDb;User Id=admin;Password=admin1234;"
    depends_on:
      - discountdb
    ports:
      - "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

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....

This user must also own the server process.
The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
initdb: warning: enabling "trust" authentication for local connections
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.
syncing data to disk ... ok
Success. You can now start the database server using:
    pg_ctl -D /var/lib/postgresql/data -l logfile start
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
2023-05-18 01:47:02.076 UTC [48] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-05-18 01:47:02.083 UTC [51] LOG:  database system was shut down at 2023-05-18 01:47:01 UTC
2023-05-18 01:47:02.090 UTC [48] LOG:  database system is ready to accept connections
 done
server started
CREATE DATABASE
/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
waiting for server to shut down....2023-05-18 01:47:02.289 UTC [48] LOG:  received fast shutdown request
2023-05-18 01:47:02.291 UTC [48] LOG:  aborting any active transactions
2023-05-18 01:47:02.293 UTC [48] LOG:  background worker "logical replication launcher" (PID 54) exited with exit code 1
2023-05-18 01:47:02.293 UTC [49] LOG:  shutting down
2023-05-18 01:47:02.295 UTC [49] LOG:  checkpoint starting: shutdown immediate
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
2023-05-18 01:47:02.377 UTC [48] LOG:  database system is shut down
 done
server stopped
PostgreSQL init process complete; ready for start up.
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
2023-05-18 01:47:02.414 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2023-05-18 01:47:02.414 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2023-05-18 01:47:02.418 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-05-18 01:47:02.424 UTC [64] LOG:  database system was shut down at 2023-05-18 01:47:02 UTC
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:

确定