Home > Software design >  Docker: you must specify POSTGRES_PASSWORD
Docker: you must specify POSTGRES_PASSWORD

Time:06-13

I'm trying to dockerize my backend API (Ruby on Rails and Postgres), I'm stuck in the postgres setting up, checking docker log I'm getting this error:

postgres_error

For a complete overview of my environment, bellow I would like to list the source code of the next files:

So, My Dockerfile looks like this:

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"]

my docker-compose.yml is this:

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:

my database.yml is this:

# 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'] %>

The init.sql is like this:

CREATE USER hftamayo;
ALTER USER hftamayo WITH SUPERUSER;
CREATE USER rorletsorder;
ALTER USER rorletsorder WITH SUPERUSER;

and my .env file is this:

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

So, my question is: what am I missing to set up properly the postgres admin user and password?

Thanks a lot

CodePudding user response:

You need to set the POSTGRES_PASSWORD environment variable in the environment of the postgres container. That means either (a) in your .env file, or (b) in the environment section of the service. You're doing neither right now.

See the documentation for that image for more information.

  • Related