参考にさせていただいたページ
今日の環境
前提となる準備
端末自体には同じバージョンをインストールしなくても、Dockerのコンテナ内にはrubyがインストールされているので環境構築はできちゃうみたいだけど、一応インストールされている方が良いのだろうか...?
- rbenvのrubyのパスが通っているか
- Dockerfileのrubyバージョンと同じバージョンのrubyのパスが通っているか
rbenv versions
,ruby -v
- rbenvのrubyにbundlerが入っているか
gem list
,gem install bundler
- bundlerのパスがrbenvのrubyであるか
which bundler
failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount083584404/Dockerfile: no such file or directory
手順を実行していく途中で、以下のようなエラーが表示される場合はDockerfileが無いとか、DockerFileみたいにFが大文字になっていてファイル名が間違っているとかそういうことかもしれません。
failed to solve: rpc error: code = Unknown desc = failed to solve with frontend dockerfile.v0: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount083584404/Dockerfile: no such file or directory
参考ページについて
今回はとにかく公式の「Quickstart: Compose and Rails | Docker Documentation」に倣ってやっていきます。
まずは公式。
Rails5だと古いのですが、まずはそのままやってみます。
プロジェクトディレクトリを作成して移動
mkdir rails_sample
cd rails_sample
Dockerfileの作成とその記述内容
vim Dockerfile
「Quickstart: Compose and Rails | Docker Documentation」に倣って以下の通り作成してみます。
# syntax=docker/dockerfile:1 FROM ruby:2.5 RUN apt-get update -qq && apt-get install -y nodejs postgresql-client vim WORKDIR /myapp COPY Gemfile /myapp/Gemfile COPY Gemfile.lock /myapp/Gemfile.lock RUN bundle install # 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 # Configure the main process to run when running the image CMD ["rails", "server", "-b", "0.0.0.0"]
Gemfileの作成とその記述内容
vim Gemfile
参考ページに倣って以下の通り記述します。
source 'https://rubygems.org' gem 'rails', '~>5'
空のGemfile.lockを作成
dockerコンテナの中にコピーして使うので空ファイルを作成しておきます。
touch Gemfile.lock
entrypoint.shの作成とその記述内容
以下の説明の通りですが、 server.pid というファイルが削除されずにrailsサーバーが起動できなくなったりするので、entrypoint.sh というファイルを作成して、server.pidを削除するコマンドを記述します。
Next, provide an entrypoint script to fix a Rails-specific issue that prevents the server from restarting when a certain server.pid file pre-exists. This script will be executed every time the container gets started. entrypoint.sh consists of:
何でも良いですが、vimで。
vim entrypoint.sh
参考ページに倣って、以下の通り記述します。
#!/bin/bash set -e # Remove a potentially pre-existing server.pid for Rails. rm -f /myapp/tmp/pids/server.pid # Then exec the container's main process (what's set as CMD in the Dockerfile). exec "$@"
docker-compose.ymlの作成とその記述内容
version: "3.9" services: db: image: postgres volumes: - ./tmp/db:/var/lib/postgresql/data environment: POSTGRES_PASSWORD: password web: build: . command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" volumes: - .:/myapp ports: - "3000:3000" depends_on: - db
rails newする
参考ページに倣って、以下のコマンドを実行します。
docker-compose run --no-deps web rails new . --force --database=postgresql
rails newされて、railsのプロジェクトが生成されます。
このときGemfileやGemfile.lockが更新されます。
docker-compose buildする
参考ページに倣って以下のコマンドを実行します。
docker-compose build
データベースに接続する
参考ページに倣って、config/database.ymlを以下の通り編集します。
default: &default adapter: postgresql encoding: unicode host: db username: postgres password: password pool: 5 development: <<: *default database: myapp_development test: <<: *default database: myapp_test
たぶんdefaultの host, username, password の項目を追記するだけだと思います。
docker-compose upする
参考ページに倣って、以下のコマンドで、webとdbのサーバーを起動します。
docker-compose up
localhost:3000にアクセスしてみる
「http://localhost:3000/」にアクセスしたら、以下のようなエラーが出ました。
could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
railsの config/database.yml の変更を反映したいので、一度stopしてupしなおします。
docker-compose stopする
一度stopします。
db createする
参考ページに倣って、以下のコマンドでDBを作成します。
docker-compose run web rake db:create
docker-compose upし直す。
docker-compose up
localhost:3000にアクセスし直す
「http://localhost:3000/」にアクセスしたら、railsのスタートページが表示されました。
rubyとrailsのバージョンが古いので、新しいバージョンで後日もう一度挑戦しようと思います。
サーバーを止める
以下のコマンドで止められます。
docker-compose stop
サーバーを起動する
以下のコマンドで起動できます。
docker-compose start
不要なコンテナが残っていないか確認する
以下のコマンドでコンテナの一覧を表示できます。
docker ps -a
不要なコンテナを削除する
止まっているコンテナは、以下のコマンドで削除できます。
docker rm コンテナID