ホスト名 "db" をアドレスに変換できませんでした: nodename または servname が提供されたか、または不明です
解決したいこと
rails db:migrateでマイグレーションしようとしたら、下記のエラーになりました。
rails db:migrate:statusでマイグレーションファイルの状態を確認しようとしても、同じエラーになります。
dockerを起動しています。
DBはPostgreSQLを使用しています。
発生している問題・エラー
$rails db:migrate
rails aborted!
ActiveRecord::ConnectionNotEstablished: could not translate host name "db" to address: nodename nor servname provided, or not known
Caused by:
PG::ConnectionBad: could not translate host name "db" to address: nodename nor servname provided, or not known
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
rails db:migrate:status
rails aborted!
ActiveRecord::ConnectionNotEstablished: could not translate host name "db" to address: nodename nor servname provided, or not known
Caused by:
PG::ConnectionBad: could not translate host name "db" to address: nodename nor servname provided, or not known
Tasks: TOP => db:migrate:status
(See full trace by running task with --trace)
該当するソースコード
compose.yml
services:
db:
image: postgres:14.4
container_name:
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_PORT=5432
- TZ=Asia/Tokyo
ports:
- "5432:5432"
api:
build:
context: .
image:
container_name:
volumes:
- .:/api
depends_on:
- db
ports:
- "3000:3000"
command: rails s -b 0.0.0.0
tty: true
dockerfile.
FROM ruby:3.1.2-alpine3.16
ENV RUNTIME_PACKAGES="tzdata postgresql-client libpq-dev vips" \
DEV_PACKAGES="build-base git" \
HOME=/api \
LANG=C.UTF-8 \
TZ=Azia/Tokyo
WORKDIR ${HOME}
COPY . ${WORKDIR}
RUN apk update && \
apk upgrade && \
apk add --no-cache ${RUNTIME_PACKAGES} && \
apk add --virtual build-dependencies --no-cache ${DEV_PACKAGES} && \
bundle install -j4 && \
apk del build-dependencies && \
rm -f ${HOME}/tmp/pids/server.pid
RUN apk add --no-cache gcompat
EXPOSE 3000
database.yml
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 } %>
host: db
password: postgres
username: postgres
development:
<<: *default
database: ***_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: ***
# 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: ***_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
database: ***_production
password: <%= Rails.application.credentials.dig(:db, :password) %>
host: <%= Rails.application.credentials.dig(:db, :host) %>
※セキュリティの問題上、DB名を***で表現しています。
自分で試したこと
下記のコマンドを入力しましたが、変わりませんでした。
$bin/rails db:migrate RAILS_ENV=development
rails aborted!
ActiveRecord::ConnectionNotEstablished: could not translate host name "db" to address: nodename nor servname provided, or not known
Caused by:
PG::ConnectionBad: could not translate host name "db" to address: nodename nor servname provided, or not known
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
1