困っていること
docker-composeで、rails postgreSQLのコンテナを作成しました。
buildしてdocker compose up
で起動後、
rails db:create
でDB作成しようとするとエラーとなります。
実現したいこと
http://localhost:3000/
にアクセスしてrailsの初期ページが表示されること
発生している問題・エラーメッセージ
http://localhost:3000/
にアクセスした結果が下の画像です。
エラーメッセージ $ docker-compose run api rails db:create Creating test-backend_api_run ... done Calling `DidYouMean::SPELL_CHECKERS.merge!(error_name => spell_checker)' has been deprecated. Please call `DidYouMean.correct_error(error_name, spell_checker)' instead. fe_sendauth: no password supplied Couldn't create 'test_development' database. Please check your configuration. rails aborted! ActiveRecord::ConnectionNotEstablished: fe_sendauth: no password supplied Caused by: PG::ConnectionBad: fe_sendauth: no password supplied Tasks: TOP => db:create (See full trace by running task with --trace) ERROR: 1
該当のソースコード
関連しそうなコードです。
Gemfile
1source "https://rubygems.org" 2git_source(:github) { |repo| "https://github.com/#{repo}.git" } 3 4ruby "3.1.2" 5 6# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" 7gem "rails", "~> 7.0.3", ">= 7.0.3.1" 8 9# Use postgresql as the database for Active Record 10gem 'pg', '~> 0.18' 11 12# Use the Puma web server [https://github.com/puma/puma] 13gem "puma", "~> 5.0" 14 15# Build JSON APIs with ease [https://github.com/rails/jbuilder] 16# gem "jbuilder" 17 18# Use Redis adapter to run Action Cable in production 19# gem "redis", "~> 4.0" 20 21# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis] 22# gem "kredis" 23 24# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword] 25# gem "bcrypt", "~> 3.1.7" 26 27# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 28gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] 29 30# Reduces boot times through caching; required in config/boot.rb 31gem "bootsnap", require: false 32 33# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images] 34# gem "image_processing", "~> 1.2" 35 36# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible 37# gem "rack-cors" 38 39group :development, :test do 40 # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem 41 gem "debug", platforms: %i[ mri mingw x64_mingw ] 42end 43 44group :development do 45 # Speed up commands on slow machines / big apps [https://github.com/rails/spring] 46 # gem "spring" 47end 48 49
Dockerfile
1FROM ruby:3.1 2RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \ 3 && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \ 4 && apt-get update -qq \ 5 && apt-get install -y nodejs yarn \ 6 && mkdir /test 7WORKDIR /test 8COPY Gemfile /test/Gemfile 9COPY Gemfile.lock /test/Gemfile.lock 10RUN bundle install 11COPY . /test 12 13COPY entrypoint.sh /usr/bin/ 14RUN chmod +x /usr/bin/entrypoint.sh 15ENTRYPOINT ["entrypoint.sh"] 16EXPOSE 3000 17 18CMD ["rails", "server", "-b", "0.0.0.0"]
docker
1version: '3' 2services: 3 db: 4 image: postgres 5 environment: 6 POSTGRES_USER: postgres 7 POSTGRES_PASSWORD: "postgres_password" 8 ports: 9 - '5432:5432' 10 container_name: test-db 11 volumes: 12 - pg_data:/var/lib/postgresql/data 13 api: 14 build: ./ 15 command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" 16 container_name: test-rails 17 volumes: 18 - ./:/test/ 19 - gem_data:/usr/local/bundle 20 ports: 21 - "3000:3000" 22 depends_on: 23 - db 24 stdin_open: true 25 tty: true 26 27 api-nginx: 28 build: ./nginx/ 29 container_name: test-rails-nginx 30 volumes: 31 - ./:/test/ 32 ports: 33 - "8080:80" 34 depends_on: 35 - api 36 37volumes: 38 pg_data: 39 gem_data: 40 driver: local 41
databases.yml
1default: &default 2 adapter: postgresql 3 # encoding: utf8mb4 4 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 5 username: <%= ENV["POSTGRES_USER"] %> 6 password: <%= ENV["POSTGRES_PASSWORD"] %> 7 host: db 8 9development: 10 <<: *default 11 database: test_development 12 13test: 14 <<: *default 15 database: test_test 16 17production: 18 <<: *default 19 database: test_production 20 username: test 21 password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
試したこと
Gemfileのgem 'pg'
のバージョンを書きかえるなど

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/09/10 10:10