我正在嘗試對我的后端 API(Ruby on Rails 和 Postgres)進行 docker 化,我被困在 postgres 設定中,檢查 docker log 我收到了這個錯誤:

對于我的環境的完整概述,下面我想列出下一個檔案的源代碼:
所以,我的 Dockerfile 看起來像這樣:
FROM ruby:2.7.0-alpine
ENV BUNDLER_VERSION=2.1.2
RUN apk add --update --no-cache \
binutils-gold \
build-base \
curl \
file \
g \
gcc \
git \
less \
libstdc \
libffi-dev \
libc-dev \
linux-headers \
libxml2-dev \
libxslt-dev \
libgcrypt-dev \
make \
netcat-openbsd \
nodejs \
openssl \
pkgconfig \
postgresql-dev \
python \
tzdata \
yarn
RUN gem install bundler -v 2.1.2
WORKDIR /app
COPY Gemfile Gemfile.lock ./
RUN bundle config build.nokogiri --use-system-libraries
RUN bundle check || bundle install
#COPY package.json yarn.lock ./
RUN yarn install --check-files
COPY . ./
ENTRYPOINT ["./entrypoints/docker-entrypoint.sh"]
我的 docker-compose.yml 是這樣的:
version: '3.4'
services:
app:
build:
context: .
dockerfile: Dockerfile
depends_on:
- database
ports:
- "3000:3000"
volumes:
- .:/app
- gem_cache:/usr/local/bundle/gems
- node_modules:/app/node_modules
env_file: .env
environment:
RAILS_ENV: production
database:
image: postgres:13.7
env_file:
- ./.env
volumes:
- db_data:/var/lib/postgresql/data
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
volumes:
gem_cache:
db_data:
node_modules:
我的 database.yml 是這樣的:
# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
# gem install pg
# On macOS with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On macOS with MacPorts:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: rorletsorder_development
# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user running Rails.
#username: rorletsorder
# The password associated with the postgres role (username).
#password:
# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost
# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
#port: 5432
# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public
# Minimum log levels, in increasing order:
# debug5, debug4, debug3, debug2, debug1,
# log, notice, warning, error, fatal, and panic
# Defaults to warning.
#min_messages: notice
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: rorletsorder_test
# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
# production:
# url: <%= ENV['MY_APP_DATABASE_URL'] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
<<: *default
POSTGRES_USER: <%= ENV['RORLETSORDER_ADMIN_USER'] %>
POSTGRES_PASSWORD: <%= ENV['RORLETSORDER_ADMIN_PASSWORD'] %>
database: <%= ENV['RORLETSORDER_DATABASE_NAME'] %> #rorletsorder_production
username: <%= ENV['RORLETSORDER_DATABASE_USERNAME'] %> #rorletsorder
password: <%= ENV['RORLETSORDER_DATABASE_PASSWORD'] %>
port: <%= ENV['RORLETSORDER_DATABASE_PORT'] || '5432' %>
host: <%= ENV['RORLETSORDER_DATABASE_HOST'] %>
init.sql 是這樣的:
CREATE USER hftamayo;
ALTER USER hftamayo WITH SUPERUSER;
CREATE USER rorletsorder;
ALTER USER rorletsorder WITH SUPERUSER;
我的 .env 檔案是這樣的:
RORLETSORDER_ADMIN_USER=hftamayo
RORLETSORDER_ADMIN_PASSWORD=XXX
RORLETSORDER_DATABASE_NAME=rorletsorder_production
RORLETSORDER_DATABASE_USERNAME=rorletsorder
RORLETSORDER_DATABASE_PASSWORD=XXX
RORLETSORDER_DATABASE_HOST= rorletsorderdatabase
所以,我的問題是:我缺少什么來正確設定 postgres 管理員用戶和密碼?
非常感謝
uj5u.com熱心網友回復:
您需要在 postgres 容器的環境中POSTGRES_PASSWORD設定環境變數。這意味著(a)在您的檔案中,或(b)在服務部分中。你現在什么都不做。.envenvironment
有關詳細資訊,請參閱該影像的檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/490319.html
標籤:PostgreSQL 码头工人 码头工人撰写
