0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Rails】docker compose upで「A server is already running.」が出る場合の対処法

0
Posted at

現在バックエンド等勉強し直し中です。
学習中に発生した事項の覚書として残しておきます。

事象

  • dockerを使った開発環境を立ち上げる際、 docker compose upでrailsサーバーがうまく立ち上がらない

対応方法

  • dockerのログを見ると以下のようになっていました。
web-1  | => Booting Puma
web-1  | => Rails 8.1.3.1 application starting in development
web-1  | => Run `bin/rails server --help` for more startup options
web-1  | A server is already running (pid: 1, file: /rails/tmp/pids/server.pid).
web-1  | Exiting
web-1 exited with code 1

A server is already runningとあるのでサーバーが起動しているとあります。

指定されたtmp/pids/server.pidを削除すれば起動できるようになります。

ls tmp/pids
rm tmp/pids/server.pid

原因

Railsサーバーを起動すると、起動中のRailsサーバーのプロセスID(PID)を記録するtmp/pids/server.pidが作成されます。
Railsサーバーが正常に終了すればこのファイルも削除されますが、コンテナの強制終了(docker compose kill等)などによってファイルだけが残る場合があるようです。

その状態で再びRailsサーバーを起動すると、Railsがserver.pidを見つけて「すでにサーバーが起動している」と判断し、A server is already running が発生します。

Docker開発環境では tmp/pids/server.pid がホスト側に残ることがあるため、恒久的な対策として、compose.yml の web サービスでRailsを起動する前にPIDファイルを削除します。

version: '3'
services:
  db:
    ...
  web:
    build: .
-   command: bundle exec rails s -p 3000 -b '0.0.0.0'
+   command: /bin/sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" 
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    depends_on:
      - db

rm -fのコマンドは、tmp/pids/server.pidがない場合でもエラーを非表示にします。

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?