从 Docker 容器连接到 PostgresSql 时出现问题

从 Docker 容器连接到 PostgresSql 时出现问题

我有两台Ubuntu Servers 20.04服务器,它们的外部 IP 为 185.253.74.227。第一台服务器的本地 IP 为 192.168.1.30,我在该服务器上nginx运行 Docker 容器,并在其上托管我的 Docker 容器。
第二台服务器的本地 IP 为 192.168.1.118,我在该服务器上运行PostgreSQL v12

我已经创建了一个数据库并成功连接到本地,并配置了外部访问,以便我的同事可以通过pgAdmin4

"ConnectionStrings": "Server=185.253.74.227;Port=5432;Database=forex_crm; ;User Id=ampiex;Password=my_password;Trust Server Certificate=true;"

当我运行 Docker 容器时出现问题

sudo docker run -d --name crm_front --network ampitex_default -p 3002:3002 ac0f95b5c3e0
sudo docker run -d --name crm_api --network ampitex_default -p 8080:80 75d048cb9146 

crm_front- 这是我的应用程序的客户端
crm_api- 应该连接到数据库的 asp.net core web api 应用程序

运行容器后,我尝试登录我的应用程序的网站并收到 500 内部服务器错误。

docker 日志 crm_api

fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
      An error occurred using the connection to database 'forex_crm' on server ''.
fail: Microsoft.EntityFrameworkCore.Query[10100]
      An exception occurred while iterating over the results of a query for context type 'AmpitexCrm.Repositories.Contexts.CRMContext'.
      System.InvalidOperationException: An exception has been raised that is likely due to a transient failure.
       ---> Npgsql.NpgsqlException (0x80004005): Failed to connect to 185.253.74.227:5432
       ---> System.TimeoutException: Timeout during connection attempt
fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HMVGERBOVK44", Request id "0HMVGERBOVK44:00000002": An unhandled exception was thrown by the application.
      System.InvalidOperationException: An exception has been raised that is likely due to a transient failure.
       ---> Npgsql.NpgsqlException (0x80004005): Failed to connect to 185.253.74.227:5432
       ---> System.TimeoutException: Timeout during connection attempt

我尝试从第一台服务器连接数据库

root@ampitex:~# psql -h 185.253.74.227 -U postgres -d postgres -p 5432

psql (12.16 (Ubuntu 12.16-0ubuntu0.20.04.1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

postgres=#    

然后从 crm_api 容器

root@ampitex:~# docker exec -it crm_api bash
root@3088e6d8fa7c:/app# psql -h 185.253.74.227 -U postgres -d postgres -p 5432
psql: error: could not connect to server: Connection timed out
        Is the server running on host "185.253.74.227" and accepting
        TCP/IP connections on port 5432?

现在我将提供配置文件和我的操作结果

root@ampitex:~# ufw status
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
80                         ALLOW       Anywhere
443                        ALLOW       Anywhere
8080                       ALLOW       Anywhere
3000                       ALLOW       Anywhere
3002                       ALLOW       Anywhere
5432                       ALLOW       Anywhere
5432                       ALLOW       172.23.0.4
443/tcp on br-3abfdd99a830 ALLOW       Anywhere
5432/tcp on br-3abfdd99a830 ALLOW       Anywhere
5432/tcp on br-3abfdd99a830 ALLOW       172.23.0.4
5432/tcp                   ALLOW       172.23.0.4
OpenSSH                    ALLOW       Anywhere

这里我向你展示容器在哪个网络上 root@ampitex:~# docker inspect 3088e6d8fa7c

                "Gateway": "172.22.0.1",
                "IPAddress": "172.22.0.3",
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "MacAddress": "02:42:ac:16:00:03",
                "DriverOpts": null
         

现在,我在第二台服务器上

root@database:~# netstat -nlp | grep 5432
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      384242/postgres
tcp6       0      0 :::5432                 :::*                    LISTEN      384242/postgres
unix  2      [ ACC ]     STREAM     LISTENING     1425099  384242/postgres      /var/run/postgresql/.s.PGSQL.5432
root@database:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:19:99:df:31:51 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.118/24 brd 192.168.1.255 scope global enp2s0
       valid_lft forever preferred_lft forever
    inet6 fe80::219:99ff:fedf:3151/64 scope link
       valid_lft forever preferred_lft forever
root@database:~# ufw status
Status: active

To                         Action      From
--                         ------      ----
5432                       ALLOW       Anywhere
5432                       ALLOW       172.23.0.4
22                         ALLOW       Anywhere
192.168.1.118 5432/tcp     ALLOW       Anywhere
192.168.1.118 5432/udp     ALLOW       Anywhere
5432                       ALLOW       172.22.0.3
5432/tcp                   ALLOW       Anywhere
OpenSSH                    ALLOW       Anywhere
5432 (v6)                  ALLOW       Anywhere (v6)
22 (v6)                    ALLOW       Anywhere (v6)
5432/tcp (v6)              ALLOW       Anywhere (v6)
OpenSSH (v6)               ALLOW       Anywhere (v6)
root@database:~# nano /etc/postgresql/12/main/pg_hba.conf
local   all             all                                     peer

host    all             all             0.0.0.0/0               md5
hostssl all             all          0.0.0.0/0                  md5
host    all             all             ::1/128                 md5
local   replication     all                                     peer
host    all             all              ::/128                 md5

root@database:~# nano /etc/postgresql/12/main/postgresql.conf
listen_addresses = '*'                                     
port = 5432                             
max_connections = 1000                  
#superuser_reserved_connections = 3     
unix_socket_directories = '/var/run/postgresql' 

我也尝试创建一个docker-compose

version: '3.8'

services:
  crm_api:
    image: ampitexcrmapi:latest
    ports:
      - "8080:80"
    depends_on:
      - postgres
    environment:
      DATABASE_CONNECTION_STRING: "Server=postgres;Port=5432;Ssl Mode=Require;Database=postgres;User Id=postgres;Password=my_password;Trust Server Certificate=true;"

  crm_front:
    image: crm_front:latest
    ports:
      - "3002:3002"

  postgres:
    image: postgres:latest
    environment:
      POSTGRES_DB: postgres
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: my_password
      POSTGRES_PORT: 5432

然而,这并没有产生任何结果。我在搜索信息时尝试了很多技巧,因此我的服务器看起来已经被污染了,这让情况变得更糟。所以我需要你的帮助,如果你缺乏任何信息来了解情况,我会发给你。我很乐意接受任何建议

相关内容