【问题标题】:Docker Compose - Postgres/Postgis - ShinyappDocker Compose - Postgres/Postgis - Shinyapp
【发布时间】:2022-10-16 02:15:53
【问题描述】:

我创建了一个组合的 docker 镜像,它基于以下组件

version: "3"
services:
  db:
    image: kartoza/postgis:14-3.2
    environment:
      - POSTGRES_DB=AAAAAAAA
      - POSTGRES_USER=BBBBBBBBBB
      - POSTGRES_PASS=CCCCCCCCCC
      - POSTGRES_MULTIPLE_EXTENSIONS=postgis,postgis_raster
    ports:
      - "5432:5432"
    restart: on-failure
    healthcheck:
      test: "exit 0"
  shiny:
    container_name: shiny
    build: ./webapp4
    ports:
      - "8787:8787"
    depends_on:
      - "db"

一切都建立良好并投入运行。

但是当 shinyapp 尝试使用以下代码连接数据库时

remote_conn <- dbConnect(RPostgres::Postgres(),
                         dbname = "AAAAAAAA",
                         host="localhost",
                         port="5432",
                         user="BBBBBBBBBB",
                         password="CCCCCCCCCC")

我有以下输出

Warning: Error in : could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?

  55: <Anonymous>
  54: stop
  53: connection_create
  52: .local
  51: dbConnect
  49: server
   3: runApp
   2: print.shiny.appobj
   1: <Anonymous>
Error : could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?

有人可以解释我如何解决这个问题吗? 谢谢

【问题讨论】:

    标签: postgresql docker-compose postgis shinyapps


    【解决方案1】:

    您是否尝试向两个容器添加相同的网络? 只需定义网络,然后将网络名称添加到两个容器,并在连接时确保使用服务名称作为主机参数,在你的情况下它会是

    host="db",而不是 host="localhost"

    例如看https://github.com/Tornike-Skhulukhia/postgres-to-mongo-importer/blob/main/docker-compose.yml

    【讨论】:

    • 正如你所建议的,我已经创建了网络并将“localhost”替换为“db”,现在一切正常。谢谢
    • 太好了,您可以将问题答案标记为已解决。