[英]Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?
[英]Docker: Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?
我在使用 Ruby on Rail 在本地系統上設置和運行 docker 實例時遇到問題。 請查看我的 docker 配置文件:-
Dockerfile
FROM ruby:2.3.1
RUN useradd -ms /bin/bash web
RUN apt-get update -qq && apt-get install -y build-essential
RUN apt-get -y install nginx
RUN apt-get -y install sudo
# for postgres
RUN apt-get install -y libpq-dev
# for nokogiri
RUN apt-get install -y libxml2-dev libxslt1-dev
# for a JS runtime
RUN apt-get install -y nodejs
RUN apt-get update
# For docker cache
WORKDIR /tmp
ADD ./Gemfile Gemfile
ADD ./Gemfile.lock Gemfile.lock
ENV BUNDLE_PATH /bundle
RUN gem install bundler --no-rdoc --no-ri
RUN bundle install
# END
ENV APP_HOME /home/web/cluetap_app
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME
ADD . $APP_HOME
RUN chown -R web:web $APP_HOME
ADD ./nginx/nginx.conf /etc/nginx/
RUN unlink /etc/nginx/sites-enabled/default
ADD ./nginx/cluetap_nginx.conf /etc/nginx/sites-available/
RUN ln -s /etc/nginx/sites-available/cluetap_nginx.conf /etc/nginx/sites-enabled/cluetap_nginx.conf
RUN usermod -a -G sudo web
碼頭工人-compose.yml
version: '2'
services:
postgres:
image: 'postgres:9.6'
environment:
- PGDATA=/var/lib/postgresql/data/pgdata
- POSTGRES_PASSWORD=
- POSTGRES_USER=postgres
- POSTGRES_HOST=cluetapapi_postgres_1
networks:
- default
- service-proxy
ports:
- '5432:5432'
volumes:
- 'postgres:/var/lib/postgresql/data'
labels:
description: "Postgresql Database"
service: "postgresql"
web:
container_name: cluetap_api
build: .
command: bash -c "thin start -C config/thin/development.yml && nginx -g 'daemon off;'"
volumes:
- .:/home/web/cluetap_app
ports:
- "80:80"
depends_on:
- 'postgres'
networks:
service-proxy:
volumes:
postgres:
當我運行docker-compose build和docker-compose up -d這兩個命令時,它運行成功但是當我從 url 中點擊它時,它是內部服務器錯誤並且錯誤是
Unexpected error while processing request: 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?
我已經應用了一些解決方案,但它對我不起作用,請指導我,我是 docker 和 AWS 的新手。
問題是您正在嘗試連接到 DB 容器內的 localhost。 您為 postgres 執行的端口映射5432:5432
將 5432 映射到主機的 localhost。
現在您的web
容器代碼正在容器內運行。 它的 localhost:5432 上沒有任何內容。
因此,您需要更改配置中的連接詳細信息以連接到postgres:5432
,這是因為您將 postgres DB 服務命名為postgres
改變它,它應該工作。
默認情況下,postgres 圖像已經暴露給 5432,因此您可以在 yml 中刪除該部分。
然后,如果你想檢查 web 服務是否可以連接到你的 postgres 服務,你可以運行這個docker-compose exec web curl postgres:5432
然后它應該返回:
curl:(52)來自服務器的空回復
如果無法連接,它將返回:
curl:(6)無法解析主機:postgres或curl:(7)無法連接到postgres端口5432:連接被拒絕
更新:
我現在知道問題所在了。 這是因為您嘗試在 localhost 上連接,您應該連接到postgres
服務。
在使用Docker和Traefik在Ubuntu 20.04中處理Rails 6應用程序時,我遇到了同樣的問題。
在我的例子中,我試圖將在 docker 容器中運行的 Ruby on Rails 應用程序連接到在主機上運行的 PostgreSQL 數據庫。
所以每次我嘗試連接到數據庫時,我都會收到錯誤消息:
Unexpected error while processing request: 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?
這是我修復它的方法:
因此,首先在容器中運行的應用程序通過Traefik暴露給主機,該主機映射到端口80
上的主機。
首先,我必須修改我的 PostgreSQL 數據庫配置文件以接受來自其他 IP 地址的遠程連接。 這個 Stack Overflow 答案可以幫助解決這個問題 - PostgreSQL: FATAL - Peer authentication failed for user (PG::ConnectionBad)
其次,我必須創建一個 docker網絡,Traefik 和其他將通過它代理的應用程序將使用該網絡:
docker network create traefik_default
第三,我在docker-compose.yml
文件中設置應用程序以使用我剛剛創建的同一網絡:
version: "3"
services:
app:
build:
context: .
dockerfile: Dockerfile
env_file:
- .env
environment:
RAILS_ENV: ${RAILS_ENV}
RACK_ENV: ${RACK_ENV}
POSTGRES_USER: ${DATABASE_USER}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
POSTGRES_DB: ${DATABASE_NAME}
POSTGRES_HOST_AUTH_METHOD: ${DATABASE_HOST}
POSTGRES_PORT: ${DATABASE_PORT}
expose:
- ${RAILS_PORT}
networks:
- traefik_default
labels:
- traefik.enable=true
- traefik.http.routers.my_app.rule=Host(`${RAILS_HOST}`)
- traefik.http.services.my_app.loadbalancer.server.port=${NGINX_PORT}
- traefik.docker.network=traefik_default
restart: always
volumes:
- .:/app
- gem-cache:/usr/local/bundle/gems
- node-modules:/app/node_modules
web-server:
build:
context: .
dockerfile: ./nginx/Dockerfile
depends_on:
- app
expose:
- ${NGINX_PORT}
restart: always
volumes:
- .:/app
networks:
traefik_default:
external: true
volumes:
gem-cache:
node-modules:
最后,在我的.env
文件中,我將主機的私有 IP 地址指定為DATABASE_HOST
作為環境變量:
DATABASE_NAME=my_app_development
DATABASE_USER=my-username
DATABASE_PASSWORD=passsword1
DATABASE_HOST=192.168.0.156
DATABASE_PORT=5432
RAILS_HOST=my_app.localhost
NGINX_PORT=80
RAILS_ENV=development
RACK_ENV=development
RAILS_MASTER_KEY=e879cbg21ff58a9c50933fe775a74d00
RAILS_PORT=3000
我剛才也有這個問題。 我的解決方案是
port: '5432:5432'
POSTGRES_HOST=cluetapapi_postgres_1
更改為POSTGRES_HOST=localhost
當您需要訪問數據庫時,只需使用示例sqlalchemy.url = postgresql+psycopg2://postgres:password@postgres/dbname
docker volume prune
並重建它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.