0
0

dockerコンテナ内でrails dbconsoleが使えないエラーが出た時の対処法

Posted at

概要

DBコンソールを起動しようとDockerコンテナ内で以下コマンドを実行したが,エラーが発生。

# rails db
Couldn't find database client: psql. Check your $PATH and try again.

rails dbコマンドを実行したときにPostgreSQLのクライアントであるpsqlが見つからないときに表示されるとのこと。
Dockerfileを訂正し,PostgreSQLをインストールする必要がある。

解決策

Dockerfile

FROM ruby:3.1.4
RUN curl -sL https://deb.nodesource.com/setup_19.x | bash - \
&& wget --quiet -O - /tmp/pubkey.gpg https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
&& echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \
&& apt-get update -qq \
&& apt-get install -y build-essential libpq-dev nodejs yarn
RUN mkdir /app
(以下略)

接続用のクライアントアプリケーション記載がなかったため,postgresql-clientを追加記載する。

FROM ruby:3.1.4
RUN curl -sL https://deb.nodesource.com/setup_19.x | bash - \
&& wget --quiet -O - /tmp/pubkey.gpg https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
&& echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \
&& apt-get update -qq \
&& apt-get install -y build-essential libpq-dev nodejs yarn postgresql-client  ←追加
RUN mkdir /app

Dockerイメージを再ビルドする。

docker-compose buildコマンドを実行して、Dockerfileの変更を反映させる。
再度docker-compose run web rails dbconsoleを実行する。

参考にさせていただいた記事
https://qiita.com/ryosk7/items/077ea98a88ec3df289c8

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0