[英]Rails docker postgres Connection refused
I'm trying to dockerize my Rails application, but I have this error when I want to run it with docker-compose:我正在尝试 dockerize 我的 Rails 应用程序,但是当我想使用 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?
my docker-compose file is:我的 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:
Dockerfile: Dockerfile:
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
my database.yml:我的数据库.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 file: .env 文件:
DB_NAME=cda_database
DB_HOST=postgres
DB_USER=postgres
DB_PASSWORD=postgres
DB_PORT=5432
DB_POOL=5
I can't understand what I'm doing wrong?, It seems I did everything correctly.我不明白我做错了什么?,看来我做的一切都是正确的。 but I'm getting the error.
但我得到了错误。 I'm using Digitalocean, Also, the database is working outside of docker.
我正在使用 Digitalocean,此外,数据库在 docker 之外工作。 but it can't be accessible in docker.
但它无法在 docker 中访问。
You can try it in docker-compose file.您可以在 docker-compose 文件中进行尝试。 I hope it working
我希望它工作
postgres:
image: 'postgres:12.4'
volumes:
- postgres:/var/lib/postgresql/data
- postgres_log:/var/log/postgresql
environment:
- POSTGRES_USER= postgres
- POSTGRES_PASSWORD= postgres
You can write the environment variables each one for postgresql in docker-compose.yml, example:您可以在docker-compose.yml中为postgresql分别编写环境变量,例如:
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
environment:
POSTGRES_PASSWORD: "${DB_PASSWORD}"
POSTGRES_USER: "${DB_USER}"
POSTGRES_DB: "${DB_NAME}"
volumes:
gems:
redis:
redis_log:
postgres:
postgres_log:
Be aware that the file.env must be in the same directory than your docker-compose.yml请注意,file.env 必须与 docker-compose.yml 位于同一目录中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.