我创建了一个组合的 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"
一切顺利并投入运行。
但是当闪亮的应用程序尝试使用以下代码连接数据库时
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?
有人可以解释一下如何解决这个问题吗?
您是否尝试过向两个容器添加相同的网络? 只需定义网络,然后将网络名称添加到两个容器,连接时确保使用服务名称作为主机参数,在您的情况下,它是
host =“db”,而不是host =“localhost”
有关示例,请参见 https://github.com/Tornike-Skhulukhia/postgres-to-mongo-importer/blob/main/docker-compose.yml