使用 Discourse 连接外部 PostgreSQL 和 Redis

我的现有服务器上有多个网站,PostgreSQL(在 Docker 中)和 Redis(也在 Docker 中)。我想在 Discourse 中重用现有的 Postgres 和 Redis。

这是我的 Discourse containers/app.yml

templates:
    #  - "templates/postgres.template.yml"
    #- "templates/redis.template.yml"
  - "templates/web.template.yml"
  ## Uncomment the next line to enable the IPv6 listener
  #- "templates/web.ipv6.template.yml"
  - "templates/web.ratelimited.template.yml"
  ## Uncomment these two lines if you wish to add Lets Encrypt (https)
  #- "templates/web.ssl.template.yml"
  #- "templates/web.letsencrypt.ssl.template.yml"
  - "templates/web.socketed.template.yml" # Added

extra_hosts:
    - "host.docker.internal:host-gateway"

expose:
    - "8881:80"   # http
    #  - "443:443" # https

env:
  DISCOURSE_DB_NAME: discourse
  DISCOURSE_DB_USERNAME: discourse
  DISCOURSE_DB_HOST: host.docker.internal
  DISCOURSE_DB_PASSWORD: XXXXXXXXXXX

  DISCOURSE_REDIS_HOST: host.docker.internal
  DISCOURSE_REDIS_PORT: 6379

其他网站在没有 docker 的情况下运行,并且能够通过 localhost 成功连接到 Redis 和 PostgreSQL。这是 postgres/redis 的 docker-compose 文件:

version: "1.0"
services:
    postgres:
        container_name: postgres_db
        build: postgres_th
        # some settings were removed, to make file shorter for this example
        ports:
            - "5432:5432"
        restart: unless-stopped
        networks:
            - main_network

    redis:
        container_name: redis
        image: redis:7.2.1
        ports:
            - "6379:6379"
        healthcheck:
            test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
        restart: unless-stopped
        networks:
            - main_network

networks:
  main_network:
    external: true

当我尝试构建 Discourse docker 镜像时,我收到以下错误:

./launcher rebuild app

FAILED
--------------------
Pups::ExecError: cd /var/www/discourse && su discourse -c 'bundle exec rake db:migrate' failed with return #<Process::Status: pid 358 exit 1>
Location of failure: /usr/local/lib/ruby/gems/3.2.0/gems/pups-1.2.1/lib/pups/exec_command.rb:132:in `spawn'
exec failed with the params {"cd"=>"$home", "hook"=>"db_migrate", "cmd"=>["su discourse -c 'bundle exec rake db:migrate'"]}
bootstrap failed with exit code 1
** FAILED TO BOOTSTRAP ** please scroll up and look for earlier error messages, there may be more than one.

我在 Configure Discourse to use a separate PostgreSQL server 找到了一个解决方案,可以使用 host 网络与 docker 结合使用。

./launcher rebuild app --docker-args --net=host --skip-mac-address
并设置 DISCOURSE_DB_HOST 为 localhost。

在这种情况下,一切正常,唯一的问题是 Discourse 镜像由于使用主机网络而不是桥接网络而没有暴露端口。

有没有办法在不将 Discourse docker 设置为主机网络的情况下连接到外部 Postgres 服务器?作为替代解决方案,如果我使用主机网络,如何将外部 nginx(在同一服务器上运行但不在 Docker 中)连接到 Discourse docker 中的 nginx?

我设置了特定的主机名来连接 Postgres 和 Redis,它们在同一个 Docker 网络中工作:

version: "1.0"
services:
    postgres:
        container_name: postgres_db
        ports:
            - "5432:5432"
        networks:
            - main_network
        hostname: postgres-main-docker
    redis:
        container_name: redis
        image: redis:7.2.1
        ports:
            - "6379:6379"
        healthcheck:
            test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
        restart: unless-stopped
        networks:
            - main_network
        hostname: redis-docker
$ docker exec -it 1705c01635f2 bash
root@postgres-main-docker:/# ping redis-docker
PING redis-docker (172.27.0.2) 56(84) bytes of data.
64 bytes from redis.main_network (172.27.0.2): icmp_seq=1 ttl=64 time=0.128 ms
64 bytes from redis.main_network (172.27.0.2): icmp_seq=2 ttl=64 time=0.073 ms

$ docker network inspect main_network
[
    {
        "Name": "main_network",
        "Id": "8eb1973cccb151bd92f6a3b89589cd7a3e428f3ef5dadc0b80a9bb3ed60278fb",
        "Created": "2023-09-22T12:21:17.935299029+03:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.27.0.0/16",
                    "Gateway": "172.27.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "0cfb64e494057bffe3ada9848d8094517ff3badf9212338bfac869ab022f6856": {
                "Name": "redis",
                "EndpointID": "0434f2475e0137f46f2d4c09ae70862612e3734e234d8511228c1cb2f22ece83",
                "MacAddress": "02:42:ac:1b:00:05",
                "IPv4Address": "172.27.0.2/16",
                "IPv6Address": ""
            },
            "93af234be3ae1ca27824e27ff4a90711d9a8184204ed1fb152863ea29f173c37": {
                "Name": "postgres_db",
                "EndpointID": "250d1f7ce883a62c5518d7bdac3ab837522acabd34c8922de0d014a410e20183",
                "MacAddress": "02:42:ac:1b:00:04",
                "IPv4Address": "172.27.0.4/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "main_network",
            "com.docker.compose.project": "XXXXXXXXXXXXXXX",
            "com.docker.compose.version": "2.20.2"
        }
    }
]

然后我尝试通过在 containers/app.yml 中添加以下内容将 Discourse 添加到同一个 Docker 网络 main_network

networks:
  - main_network

DISCOURSE_DB_HOST: postgres-main-docker
DISCOURSE_REDIS_HOST: redis-docker

但它没有成功。有什么想法吗?

所以我尝试使用以下命令构建镜像:
./launcher rebuild app --docker-args '--network main_network',它成功了,这可能是一个解决方案(但不太方便,因为我必须在命令行而不是配置文件中指定网络)。

1 个赞

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.