LoginSignup
2
3

More than 1 year has passed since last update.

【Docker】docker-compose upした時にA server is already running. Check /product-register/tmp/pids/server.pid. エラー

Last updated at Posted at 2023-01-09

1. 問題

docker-compose upし時にA server is already running. Check /product-register/tmp/pids/server.pid. Exiting エラーが出て、立ち上がらなかった。

console
/usr/local/lib/ruby/2.7.0/net/protocol.rb:66: warning: already initialized constant Net::ProtocRetryError
/usr/local/bundle/gems/net-protocol-0.2.1/lib/net/protocol.rb:68: warning: previous definition of ProtocRetryError was here
/usr/local/lib/ruby/2.7.0/net/protocol.rb:206: warning: already initialized constant Net::BufferedIO::BUFSIZE
/usr/local/bundle/gems/net-protocol-0.2.1/lib/net/protocol.rb:214: warning: previous definition of BUFSIZE was here
/usr/local/lib/ruby/2.7.0/net/protocol.rb:503: warning: already initialized constant Net::NetPrivate::Socket
/usr/local/bundle/gems/net-protocol-0.2.1/lib/net/protocol.rb:541: warning: previous definition of Socket was here
=> Booting Puma
=> Rails 6.0.6 application starting in development
=> Run `rails server --help` for more startup options
A server is already running. Check /product-register/tmp/pids/server.pid.
Exiting

2. 原因・解決法

原因

エラーCheck /product-register/tmp/pids/server.pid.に出ている通り、前回コンテナを立ち上げた時のpidファイルが残ってしまっていることが原因。

解決法

pidファイルを削除する

console
yellyell@MacBook-Pro product-register % rm tmp/pids/server.pid

pidファイルを消したので、もう一度コンテナを立ち上げてみる

console
yellyell@MacBook-Pro product-register % docker-compose up -d
Starting product-register_web_1 ... done

...

/usr/local/lib/ruby/2.7.0/net/protocol.rb:66: warning: already initialized constant Net::ProtocRetryError
/usr/local/bundle/gems/net-protocol-0.2.1/lib/net/protocol.rb:68: warning: previous definition of ProtocRetryError was here
/usr/local/lib/ruby/2.7.0/net/protocol.rb:206: warning: already initialized constant Net::BufferedIO::BUFSIZE
/usr/local/bundle/gems/net-protocol-0.2.1/lib/net/protocol.rb:214: warning: previous definition of BUFSIZE was here
/usr/local/lib/ruby/2.7.0/net/protocol.rb:503: warning: already initialized constant Net::NetPrivate::Socket
/usr/local/bundle/gems/net-protocol-0.2.1/lib/net/protocol.rb:541: warning: previous definition of Socket was here
=> Booting Puma
=> Rails 6.0.6 application starting in development
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 4.3.12 (ruby 2.7.4-p191), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

立ち上がった!!!

3. 毎回pidファイルの削除がめんどくさいの解決法

上記の解決法で一時的には解決しても、毎回pidファイルを手動で削除するのは現実的ではない。そこで下記のようにdocker-compose.ymlを設定しておく

解決法

docker-compose upした時に、pidファイルがあったら削除するコマンドを実行するようdocker-compose.ymlファイルに設定しておく

command: /bin/sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"

docker-compose.yml
version: '3'

services:
  web:
    build: .
    # commandに追加
    command: /bin/sh -c "rm -f tmp/pids/server.pid  && bundle exec rails s -p 3000 -b '0.0.0.0'"
    ports:
      - '3000:3000'
    volumes:
      - '.:/product-register'
    tty: true
    stdin_open: true

追加コードの解説
シェル/bin/sh -cで、pidファイルを削除rm -f tmp/pids/server.pidし、port番号3000でrailsサーバー立ち上げるbundle exec rails s -p 3000 -b '0.0.0.0'コマンドを実行する

こうすることで、毎回手動でpidファイルを削除しなくてもよくなる

参考

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