亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法將 Go 應用程序連接到 docker-compose 中的 postgres 容器

無法將 Go 應用程序連接到 docker-compose 中的 postgres 容器

Go
瀟湘沐 2022-06-27 10:02:56
我一直在嘗試使用 docker-compose 將我的 go 應用程序連接到 postgresql db。這是我用于 Web 服務器的 DockerfileFROM golang:latestRUN mkdir /appCOPY . /appWORKDIR /appRUN go mod downloadEXPOSE 5000CMD ["go","run","main.go"]這是我的 docker-compose.yml 文件version: '3'services:  db:    image: postgres:latest    restart: always    network_mode: bridge    environment:      POSTGRES_USER: factly      POSTGRES_PASSWORD: qwertyui      POSTGRES_DB: factly    ports:      - "5432:5432"  server:    build: .    depends_on:      - db    ports:      - "5000:5000"但我無法連接到 postgresql 數據庫,因為它給出了來自Go serverStarting factly_db_1 ... Starting factly_db_1 ... doneRecreating factly_server_1 ... Recreating factly_server_1 ... doneAttaching to factly_db_1, factly_server_1db_1      | db_1      | PostgreSQL Database directory appears to contain a database; Skipping initializationdb_1      | db_1      | 2020-08-19 06:11:17.151 UTC [1] LOG:  starting PostgreSQL 12.4 (Debian 12.4-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bitdb_1      | 2020-08-19 06:11:17.151 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432db_1      | 2020-08-19 06:11:17.152 UTC [1] LOG:  listening on IPv6 address "::", port 5432db_1      | 2020-08-19 06:11:17.266 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"db_1      | 2020-08-19 06:11:17.906 UTC [26] LOG:  database system was shut down at 2020-08-19 05:56:39 UTCdb_1      | 2020-08-19 06:11:18.213 UTC [1] LOG:  database system is ready to accept connectionsserver_1  | host=127.0.0.1 port=5432 user=factly password=qwertyui dbname=factly sslmode=disableserver_1  | 2020/08/19 06:11:21 dial tcp 127.0.0.1:5432: connect: connection refusedserver_1  | exit status 1factly_server_1 exited with code 1我嘗試只運行 postgres 容器并將其與運行良好的 Go 應用程序連接,但在容器內運行 Go 應用程序會產生相同的錯誤(這讓我覺得 Go Web 服務器容器無法連接到它的網絡之外) . 我該如何糾正?
查看完整描述

1 回答

?
慕田峪4524236

TA貢獻1875條經驗 獲得超5個贊

我想問題是你試圖127.0.0.1從 go 程序連接,而不是使用數據庫容器的主機名db



查看完整回答
反對 回復 2022-06-27
  • 1 回答
  • 0 關注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號