我是第一次接触docker,并试图创建一个简单的.NET应用程序,在docker容器中运行一些微服务.其中一个集装箱有一个Postgres数据库.

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

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:

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

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"

读了其他一些StackOverflow项目后,我的印象是,discount tdb部分的Health Check部分将确保Postgres连接保持打开,这样我的.Net服务就可以运行它的"种子"?

但当我奔跑的时候

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

当我查看Portainer中的日志(log)时,我在折扣数据库的日志(log)中看到了以下内容...

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

我错过了什么吗?我已经研究了其他需要安装或运行shell 命令的解决方案,但我在这里是个新手,不太熟悉如何做到这一点?

推荐答案

您的容器都运行在由Docker Compose创建的网络中.这就是如何将服务名称用作主机.

关于端口映射的一个常见误解是它在网络中有影响.It does not.

类似"5430:5432"的映射将host端口5430映射到container端口5432.这允许您将主机应用程序连接到localhost:5430,以便查询Postgres数据库.

但在该网络内,所有集装箱港口都未受影响.Postgres DB在discountdb:5432上可用.

简而言之,你的DatabaseSettings:ConnectionString应该使用Server=discountdb;Port=5432.

Postgresql相关问答推荐

我需要一个变量来引用上周的星期五

Org.postgresql.util.PSQLException:错误:函数LOWER(BYTEA)不存在

在rust中,有什么方法可以从sqlx::query_with中返回rows_infected以及行本身?

使用doobie,如何将Scala case类映射到带有类型tstzmultirange的PostgreSQL列?

如何使用 Grafana 测量 PostgreSQL 中的复制

我在try 访问我的数据库表时在 postgresql 中收到 aclcheck_error 错误

从dump文件中恢复三张表遇到的问题

我可以在 Ruby on Rails 上编写 PostgreSQL 函数吗?

PostgreSQL 查询到 Excel 表

如何从 postgresql Select 查询中的 age() 函数中仅获取年份

如何在 psycopg2 中使用服务器端游标

在 PL/pgSQL 中声明行类型变量

从 Select 中创建一个临时表,如果表已经存在则插入

如何在psql中退出查询结果查看器?

django.db.utils.IntegrityError:duplicate key value violates unique constraint "django_content_type_pkey"

安装时 postgres 的默认用户

无法在 postgresql hibernate 中使用名为user的表

如何从我的 postgresql 查询中获取最小值、中值和最大值?

性能调优:为布尔列创建索引

PostgreSQL 的EXPLAIN解释中的 width字段是什么意思?