質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
docker-compose

docker-composeとは、複数のコンテナで構成されるサービスを提供する手順を自動的し管理を簡単にするツール。composeファイルを使用しコマンド1回で設定した全サービスを作成・起動することが可能です。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

Q&A

解決済

1回答

3228閲覧

【PostgreSQL】docker-composeを使って「rails db:create」をした時に「fe_sendauth: no password supplied」というエラーが出る

ikore908

総合スコア17

docker-compose

docker-composeとは、複数のコンテナで構成されるサービスを提供する手順を自動的し管理を簡単にするツール。composeファイルを使用しコマンド1回で設定した全サービスを作成・起動することが可能です。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

0グッド

0クリップ

投稿2022/01/22 22:50

はじめに

最初に結論を言うと、Docker環境でPostgresQLの最新版「14.1」を指定して
rails db:create
を実行すると、
fe_sendauth: no password supplied
というエラーが出ます。

流れ

流れとしては、参考サイト(こちら)を元に

  • docker
  • rails
  • postgresql

を使ってWEBアプリを作ろうとしており、まずrailsのウェルカムページ(Yay! You’re on Rails!)を表示させたいと思っています。

参考サイトと同じバージョン(ruby、postgresql、docker-compose)ではウェルカムページが表示できたのですが、
それぞれバージョンをアップさせて同じくウェルカムページを表示させようとした段階でつまづきました。

発生している問題・エラーメッセージ

具体的には、

  • ruby:2.6.5-alpine3.11 → 3.0.3-alpine3.15
  • docker-compose.yml version '3' → version '3.8'

のバージョンアップはうまくウェルカムページの表示ができたのですが、

  • postgres:12.1-alpine → 14.1-alpine

↑のようにpostgresのバージョンアップを行うと
docker-compose run --rm web rails db:create
を実行した段階で

terminal

1Creating sample-app_db_1 ... done 2Creating sample-app_web_run ... done 3Running via Spring preloader in process 20 4could not translate host name "db" to address: Name does not resolve 5Couldn't create 'app_development' database. Please check your configuration. 6rake aborted! 7ActiveRecord::ConnectionNotEstablished: could not translate host name "db" to address: Name does not resolve

というエラーが起きました。
そのためこちらの記事をもとに環境変数POSTGRES_PASSWORDを設定しました。

しかし、その後再び
docker-compose run --rm web rails db:create
を実行すると次は

terminal

1Creating network "sample-app_default" with the default driver 2Creating sample-app_db_1 ... done 3Creating sample-app_web_run ... done 4Running via Spring preloader in process 20 5connection to server at "db" (172.23.0.2), port 5432 failed: fe_sendauth: no password supplied 6Couldn't create 'app_development' database. Please check your configuration. 7rake aborted! 8ActiveRecord::ConnectionNotEstablished: connection to server at "db" (172.23.0.2), port 5432 failed: fe_sendauth: no password supplied 9- 一部省略 - 10Caused by: 11PG::ConnectionBad: connection to server at "db" (172.23.0.2), port 5432 failed: fe_sendauth: no password supplied 12/usr/local/bundle/gems/pg-1.2.3/lib/pg.rb:58:in `initialize' 13- 一部省略 - 14<internal:/usr/local/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require' 15-e:1:in `<main>' 16Tasks: TOP => db:create 17(See full trace by running task with --trace) 18ERROR: 1

「パスワードが設定されていない」というようなエラーが出てabortするようになり、自力で解決できておらず質問いたしました。

各ファイル

Dockerfile

Dockerfile

1FROM ruby:3.0.3-alpine3.15 2 3ENV HOME="/app" 4ENV LANG=C.UTF-8 5ENV TZ=Asia/Tokyo 6 7WORKDIR $HOME 8 9RUN apk update && \ 10 apk upgrade && \ 11 apk add --no-cache \ 12 gcc \ 13 g++ \ 14 less \ 15 libc-dev \ 16 libxml2-dev \ 17 linux-headers \ 18 make \ 19 nodejs \ 20 postgresql \ 21 postgresql-dev \ 22 tzdata \ 23 yarn && \ 24 apk add --virtual build-packs --no-cache \ 25 build-base \ 26 curl-dev 27 28COPY Gemfile $HOME 29COPY Gemfile.lock $HOME 30 31RUN bundle install && \ 32 apk del build-packs 33 34COPY . $HOME 35EXPOSE 3000 36CMD ["rails", "server", "-b", "0.0.0.0"]
docker-compose.yml

docker

1version: '3.8' 2 3services: 4 db: 5 image: postgres:14.1-alpine 6 environment: 7 POSTGRES_PASSWORD: password 8 9 volumes: 10 - ./tmp/db:/var/lib/postgresql/data 11 12 web: 13 build: . 14 volumes: 15 - .:/app 16 ports: 17 - 3000:3000 18 depends_on: 19 - db
database.yml

database.yml

1# PostgreSQL. Versions 9.3 and up are supported. 2# 3# Install the pg driver: 4# gem install pg 5# On macOS with Homebrew: 6# gem install pg -- --with-pg-config=/usr/local/bin/pg_config 7# On macOS with MacPorts: 8# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config 9# On Windows: 10# gem install pg 11# Choose the win32 build. 12# Install PostgreSQL and put its /bin directory on your path. 13# 14# Configure Using Gemfile 15# gem 'pg' 16# 17default: &default 18 adapter: postgresql 19 encoding: unicode 20 host: db 21 username: postgres 22 password: <%= ENV["POSTGRES_PASSWORD"] %> 23 # For details on connection pooling, see Rails configuration guide 24 # https://guides.rubyonrails.org/configuring.html#database-pooling 25 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 26 27development: 28 <<: *default 29 database: app_development 30 31 # The specified database role being used to connect to postgres. 32 # To create additional roles in postgres see `$ createuser --help`. 33 # When left blank, postgres will use the default role. This is 34 # the same name as the operating system user running Rails. 35 #username: app 36 37 # The password associated with the postgres role (username). 38 #password: 39 40 # Connect on a TCP socket. Omitted by default since the client uses a 41 # domain socket that doesn't need configuration. Windows does not have 42 # domain sockets, so uncomment these lines. 43 #host: localhost 44 45 # The TCP port the server listens on. Defaults to 5432. 46 # If your server runs on a different port number, change accordingly. 47 #port: 5432 48 49 # Schema search path. The server defaults to $user,public 50 #schema_search_path: myapp,sharedapp,public 51 52 # Minimum log levels, in increasing order: 53 # debug5, debug4, debug3, debug2, debug1, 54 # log, notice, warning, error, fatal, and panic 55 # Defaults to warning. 56 #min_messages: notice 57 58# Warning: The database defined as "test" will be erased and 59# re-generated from your development database when you run "rake". 60# Do not set this db to the same as development or production. 61test: 62 <<: *default 63 database: app_test 64 65# As with config/credentials.yml, you never want to store sensitive information, 66# like your database password, in your source code. If your source code is 67# ever seen by anyone, they now have access to your database. 68# 69# Instead, provide the password or a full connection URL as an environment 70# variable when you boot the app. For example: 71# 72# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase" 73# 74# If the connection URL is provided in the special DATABASE_URL environment 75# variable, Rails will automatically merge its configuration values on top of 76# the values provided in this file. Alternatively, you can specify a connection 77# URL environment variable explicitly: 78# 79# production: 80# url: <%= ENV['MY_APP_DATABASE_URL'] %> 81# 82# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database 83# for a full overview on how database connection configuration can be specified. 84# 85production: 86 <<: *default 87 database: app_production 88 username: app 89 password: <%= ENV['APP_DATABASE_PASSWORD'] %> 90

自分で調べたことや試したこと

環境変数の記述方法の変更

こちらの質問こちらのページに記載されているように、環境変数の記述方法を変えてみました。

docker-compose.yml

docker

1 environment: 2 - POSTGRES_PASSWORD=password

docker

1 environment: 2 POSTGRES_PASSWORD: password
database.yml

database.yml

1default: &default 2 adapter: postgresql 3 encoding: unicode 4 host: db 5 username: postgres 6 password: password

database.yml

1default: &default 2 adapter: postgresql 3 encoding: unicode 4 host: db 5 username: postgres 6 password: <%= ENV["POSTGRES_PASSWORD"] %>

しかし結果は変わりませんでした。

pg_hba.confの編集

こちらの質問を元にpg_hba.confを編集してみましたが、こちらも結果変わらずでした。

pg_hba.conf

pg_hba.conf

1# "local" is for Unix domain socket connections only 2local all all trust

pg_hba.conf

1# "local" is for Unix domain socket connections only 2local all all peer

なぜ環境変数を指定しているのにエラーになるのか、解決策やヒント等でも構いませんので情報お待ちしております。

また可能であればpostgres12.1ではエラーとならないのに、14.1でエラーが出るのがなぜか知れたら嬉しいです。

使っているツールのバージョンなど補足情報

Docker:20.10.11
docker-compose.yml:3.8
Ruby:3.0.3
Ruby on Rails:6.1.4.4
PostgresQL:14.1

不足の情報等ありましたらご指摘ただければとおもいます。
ご回答お待ちしております。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2022/01/22 23:50

psqlコマンドでの直ログインは確認しましたか?
ikore908

2022/01/23 12:23

コメントありがとうございます。はい、 `docker-compose run web sh` にてコンテナ内に入り、psqlコマンドで入れるようです。 ``` /app # psql -h db -U postgres Password for user postgres: psql (14.1) Type "help" for help. postgres=# exit /app # ```
ikore908

2022/01/23 12:40

すみませんよくわかっていなかったのですがやはりうまくいっていないようです…。 下記コマンドにてテーブル一覧を表示させようとすると.s.PGSQL.5432が無いというエラーとなります。 /app # psql -l psql: error: connection to server on socket "/run/postgresql/.s.PGSQL.5432" failed: No such file or directory Is the server running locally and accepting connections on that socket? また psqlにて直ログイン後に\lコマンドでテーブル一覧を見ても、テーブルが作られていないようです。 postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+------------+------------+----------------------- postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres (3 rows)
guest

回答1

0

自己解決

こちらの記事を元に環境変数用のファイルを別で作成

variables.env

1POSTGRES_PASSWORD="password"

②docker-compose.ymlで読み込み↓

docker

1version: '3.8' 2 3services: 4 db: 5 image: postgres:14.1-alpine 6 7 volumes: 8 - ./tmp/db:/var/lib/postgresql/data 9 10 web: 11 env_file: ./variables.env #←ここで読み込んでます! 12 build: 13 context: . 14 # args: 15 # - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} ←参考記事にはありますがここ不要で良かったです 16 volumes: 17 - .:/app 18 ports: 19 - 3000:3000 20 depends_on: 21 - db

③database.ymlで読み込む

database.yml

1default: &default 2 adapter: postgresql 3 encoding: unicode 4 host: db 5 username: postgres 6 password: <%= ENV['POSTGRES_PASSWORD'] %>

とすることでエラーも出ずウェルカムページの表示までできました!

投稿2022/01/27 11:25

ikore908

総合スコア17

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問