我的现有服务器上有多个网站,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?