外部PostgreSQLおよびRedisを使用したDiscourse

既存のサーバーには複数のウェブサイトがあり、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.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 を見つけ、Docker をホストネットワークで使用して、DISCOURSE_DB_HOST を localhost に設定するという解決策を見つけました。

./launcher rebuild app --docker-args --net=host --skip-mac-address

この場合、すべて正常に動作しますが、唯一の問題は、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.