2
1
記事投稿キャンペーン 「2024年!初アウトプットをしよう」

rails7×docker環境下でrails dbconsoleが立ち上がらない時の解決方法

Posted at

概要

タイトル通りではあるが、軽くアプリを作っていたときにrails dbconsoleが立ち上がらなくなった

エラー内容

コンテナ内で実行

# rails dbconsole
DEPRECATION WARNING: The config.web_console.whitelisted_ips is deprecated and will be ignored in future release of web_console. Please use config.web_console.allowed_ips instead. (called from <main> at /bauikis2/config/environment.rb:5)
Couldn't find database client: mysql, mysql5. Check your $PATH and try again.
  1. Couldn't find database client: mysql, mysql5. Check your $PATH and try again.というエラーが出ている
    • mysqlが見つからないからpathをチェックしてねというエラー
  2. DEPRECATION WARNING: The config.web_console.whitelisted_ips is deprecated and will be ignored in future release of web_console. Please use config.web_console.allowed_ips instead
    • こちらは今回の本題ではないため、別記事で調べる

原因

Dockerfile内でmariadb-clientというmysqlを使うためのクライアントアプリケーションを入れていなかったため

修正前

FROM --platform=linux/x86_64 ruby:3.1

RUN apt-get update
RUN apt-get install -y vim

RUN mkdir /bauikis2
WORKDIR /bauikis2
COPY Gemfile ./Gemfile
COPY Gemfile.lock ./Gemfile.lock

# Bundlerの不具合対策(1)
RUN gem update --system
RUN bundle update --bundler

RUN bundle install
COPY . /bauikis2

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

解決

修正後

FROM --platform=linux/x86_64 ruby:3.1

RUN apt-get update
RUN apt-get install -y vim mariadb-client # 追加

RUN mkdir /bauikis2
WORKDIR /bauikis2
COPY Gemfile ./Gemfile
COPY Gemfile.lock ./Gemfile.lock

# Bundlerの不具合対策(1)
RUN gem update --system
RUN bundle update --bundler

RUN bundle install
COPY . /bauikis2

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

dockerfileを書き換えたため、イメージを作り直すためにbuildし直す

docker-compose build --no-cache

コンテナを再度docker-compose upで立ち上げたあと、再度試したら成功

# rails dbconsole
DEPRECATION WARNING: The config.web_console.whitelisted_ips is deprecated and will be ignored in future release of web_console. Please use config.web_console.allowed_ips instead. (called from <main> at /bauikis2/config/environment.rb:5)
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [bauikis2_development]>

注意点

% docker-compose build --no-cache
[+] Building 38.5s (7/17)                                                                                                                      docker:desktop-linux
 => [web internal] load build definition from Dockerfile                                                                                                       0.0s
 => => transferring dockerfile: 591B                                                                                                                           0.0s
 => [web internal] load .dockerignore                                                                                                                          0.0s
 => => transferring context: 2B                                                                                                                                0.0s
 => [web internal] load metadata for docker.io/library/ruby:3.1                                                                                                7.1s
 => [web  1/13] FROM docker.io/library/ruby:3.1@sha256:f6c16efb0337941e6291ec9321357031879948fdcb08ae5c0d4a505fe8bfb14c                                       20.6s
 => => resolve docker.io/library/ruby:3.1@sha256:f6c16efb0337941e6291ec9321357031879948fdcb08ae5c0d4a505fe8bfb14c                                              0.0s
 => => sha256:f6c16efb0337941e6291ec9321357031879948fdcb08ae5c0d4a505fe8bfb14c 7.60kB / 7.60kB                                                                 0.0s
 => => sha256:ee4548ef50bf8af4f10596e7a12e311375ddff63bd92520964404280d6f65d0d 5.83kB / 5.83kB                                                                 0.0s
 => => sha256:30d85599795460b2d9d24c6b87c53ec60555b601705cc83bea31632240500980 64.14MB / 64.14MB                                                               8.0s
 => => sha256:5b46b99ce2a6a18d5267129272744776348218ac9f05e69a4ec99360b493ba3c 199B / 199B                                                                     2.2s
 => => sha256:202fe609f4dd2f8db44d7c44208f36b6c7eaeeb00c3e5bba2a8dd2b3a29027c6 2.00kB / 2.00kB                                                                 0.0s
 => => sha256:ad5739181616b815fae7edc6bba689496674acbcf44e48a57fc7cc13a379b3a2 211.10MB / 211.10MB                                                            15.4s
 => => sha256:39633c3ef1e54dd34248d3e06204e8dcaed0d27a0c5980a09d15c37fe7d8cde6 32.55MB / 32.55MB                                                               5.5s
 => => sha256:dd6e7c9735ed3ac5deaaa4d0f363498b78da43844aa65754e021ec0fbe882a1a 144B / 144B                                                                     5.8s
 => => extracting sha256:30d85599795460b2d9d24c6b87c53ec60555b601705cc83bea31632240500980                                                                      1.7s
 => => extracting sha256:ad5739181616b815fae7edc6bba689496674acbcf44e48a57fc7cc13a379b3a2                                                                      4.3s
 => => extracting sha256:5b46b99ce2a6a18d5267129272744776348218ac9f05e69a4ec99360b493ba3c                                                                      0.0s
 => => extracting sha256:39633c3ef1e54dd34248d3e06204e8dcaed0d27a0c5980a09d15c37fe7d8cde6                                                                      0.6s
 => => extracting sha256:dd6e7c9735ed3ac5deaaa4d0f363498b78da43844aa65754e021ec0fbe882a1a                                                                      0.0s
 => [web internal] load build context                                                                                                                          1.3s
 => => transferring context: 12.15MB                                                                                                                           1.3s
 => [web  2/13] RUN apt-get update                                                                                                                             8.0s
 => ERROR [web  3/13] RUN apt-get install -y vim mysql-client                                                                                                  2.7s
------
 > [web  3/13] RUN apt-get install -y vim mysql-client:
0.194 Reading package lists...
2.269 Building dependency tree...
2.688 Reading state information...
2.698 Package mysql-client is not available, but is referred to by another package.
2.698 This may mean that the package is missing, has been obsoleted, or
2.698 is only available from another source
2.698
2.701 E: Package 'mysql-client' has no installation candidate
------
failed to solve: process "/bin/sh -c apt-get install -y vim mysql-client" did not complete successfully: exit code: 100

パッケージがないと言われる

This may mean that the package is missing, has been obsoleted, or
2.698 is only available from another source

補足

2
1
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
2
1