gpt4 book ai didi

ruby-on-rails - Rails docker postgres 连接被拒绝

转载 作者:行者123 更新时间:2023-12-05 06:55:49 26 4
gpt4 key购买 nike

我正在尝试对我的 Rails 应用程序进行 docker 化,但是当我想使用 docker-compose 运行它时出现此错误:

! Unable to load application: PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host "postgres" (127.0.0.1) and accepting
TCP/IP connections on port 5432?

我的 docker-compose 文件是:

version: '2'
services:
web:
build: .
image: cda_app
container_name: "cda_app_web"
ports:
- '127.0.0.1:${WEB_PORT}:3000'
env_file: .env
volumes:
- gems:/gems
- ./:/var/www/app
logging:
driver: 'json-file'
options:
max-size: '100m'
max-file: '5'
links:
- redis
- postgres
entrypoint: 'bundle exec puma -C config/puma.rb'
redis:
image: 'redis'
volumes:
- redis:/data
- redis_log:/var/log/redis
postgres:
image: 'postgres:12.4'
volumes:
- postgres:/var/lib/postgresql/data
- postgres_log:/var/log/postgresql
volumes:
gems:
redis:
redis_log:
postgres:
postgres_log:

docker 文件:

ARG BASE_IMAGE=ruby:2.7.1
FROM $BASE_IMAGE

RUN apt-get update && \
apt-get install -y \
apt-utils \
build-essential \
cmake \
curl \
ghostscript \
libmagic-dev \
libpq-dev \
openssh-client \
rename \
&& rm -rf /var/lib/apt/lists/*

RUN curl -sL https://deb.nodesource.com/setup_12.x | bash && apt-get install -y nodejs
RUN npm install -g yarn

WORKDIR /var/www/app

RUN mkdir /gems
ENV BUNDLE_PATH=/gems
RUN gem install bundler

ARG BUNDLE_WITHOUT=development:test
COPY Gemfile Gemfile.lock ./
RUN bundle install --jobs $(nproc) --with BUNDLE_WITHOUT

COPY package.json yarn.lock ./
RUN yarn install --check-files --ignore-optional

COPY . .

RUN rename -f -v 's/\.sample//' config/*sample.yml

EXPOSE 3000

CMD ./bin/puma -b tcp://0.0.0.0:3000

我的数据库.yml:

default: &default
pool: <%= ENV["DB_POOL"] %>
template: 'template0'
adapter: 'postgresql'
database: <%= ENV["DB_NAME"] %>
username: <%= ENV["DB_USER"] %>
host: <%= ENV["DB_HOST"] %>
port: <%= ENV["DB_PORT"] %>
password: <%= ENV["DB_PASSWORD"] %>
timeout: 5000
encoding: 'utf8'
min_messages: WARNING

development:
<<: *default
database: <%= ENV["DB_NAME"] %>_development

test:
<<: *default
database: <%= ENV["DB_NAME"] %>_test<%= ENV['TEST_ENV_NUMBER'] %>

production:
<<: *default
database: <%= ENV["DB_NAME"] %>_production

.env 文件:


DB_NAME=cda_database
DB_HOST=postgres
DB_USER=postgres
DB_PASSWORD=postgres
DB_PORT=5432
DB_POOL=5

我不明白我做错了什么?!似乎我做的一切都是正确的,但我收到了错误。我正在使用 Digitalocean。此外,数据库在 docker 之外工作,但无法在 docker 中访问。

最佳答案

您可以在 docker-compose 文件中尝试。我希望它有效

postgres:
image: 'postgres:12.4'
volumes:
- postgres:/var/lib/postgresql/data
- postgres_log:/var/log/postgresql
environment:
- POSTGRES_USER= postgres
- POSTGRES_PASSWORD= postgres

关于ruby-on-rails - Rails docker postgres 连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65265730/

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