gpt4 book ai didi

ruby-on-rails - Docker:服务器是否在主机 “localhost”(::1)上运行并在端口5432上接受TCP/IP连接?

转载 作者:行者123 更新时间:2023-12-04 05:47:30 26 4
gpt4 key购买 nike

我在使用Ruby on Rail的本地系统上设置和运行docker实例时遇到问题。请查看我的docker配置文件:-

Docker文件

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

docker-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命中它时,thotht 内部服务器错误且错误是
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。您为PostgreSQL执行5432:5432的端口映射将5432映射到主机的localhost。

现在,您的web容器代码正在容器内运行。它的本地主机上没有任何内容:5432。

因此,您需要在配置中更改连接详细信息以连接到postgres:5432,这是因为您将postgres DB服务命名为postgres
更改它,它应该可以工作。

关于ruby-on-rails - Docker:服务器是否在主机 “localhost”(::1)上运行并在端口5432上接受TCP/IP连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45637206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com