零からの

ゆるーく綴るブログ

Ruby on Jets デモプロジェクト作成

Ruby on Jets 環境構築に続き、プロジェクトを作成する。

プロジェクト作成

カレントディレクトリにプロジェクトを作成する。

docker-compose run --rm app bundle exec jets new .

rootでの実行とgitコマンドについてエラーメッセージが出るので、Dockerfileを修正する。

Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this application for all
non-root users on this machine.
Fetching https://github.com/tongueroo/webpacker.git
sh: git: not found

Retrying `git clone 'https://github.com/tongueroo/webpacker.git' "/usr/local/bundle/cache/bundler/git/webpacker-34bfa510f2df7739e222f010552da83abb125db1" --bare --no-hardlinks --quiet` due to error (2/4): Bundler::Source::Git::GitCommandError Git error: command `git clone 'https://github.com/tongueroo/webpacker.git' "/usr/local/bundle/cache/bundler/git/webpacker-34bfa510f2df7739e222f010552da83abb125db1" --bare --no-hardlinks --quiet` in directory /app has failed.sh: git: not found

Retrying `git clone 'https://github.com/tongueroo/webpacker.git' "/usr/local/bundle/cache/bundler/git/webpacker-34bfa510f2df7739e222f010552da83abb125db1" --bare --no-hardlinks --quiet` due to error (3/4): Bundler::Source::Git::GitCommandError Git error: command `git clone 'https://github.com/tongueroo/webpacker.git' "/usr/local/bundle/cache/bundler/git/webpacker-34bfa510f2df7739e222f010552da83abb125db1" --bare --no-hardlinks --quiet` in directory /app has failed.sh: git: not found

Retrying `git clone 'https://github.com/tongueroo/webpacker.git' "/usr/local/bundle/cache/bundler/git/webpacker-34bfa510f2df7739e222f010552da83abb125db1" --bare --no-hardlinks --quiet` due to error (4/4): Bundler::Source::Git::GitCommandError Git error: command `git clone 'https://github.com/tongueroo/webpacker.git' "/usr/local/bundle/cache/bundler/git/webpacker-34bfa510f2df7739e222f010552da83abb125db1" --bare --no-hardlinks --quiet` in directory /app has failed.sh: git: not found

Git error: command `git clone 'https://github.com/tongueroo/webpacker.git'
"/usr/local/bundle/cache/bundler/git/webpacker-34bfa510f2df7739e222f010552da83abb125db1" --bare --no-hardlinks --quiet` in directory /app has
failed.
FROM ruby:2.5-alpine

RUN apk update && \
    apk add --no-cache build-base libxml2-dev libxslt-dev git

RUN adduser -S jets && \
    echo "jets ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers && \
    echo 'jets:jets' | chpasswd

USER jets
ENV APP_HOME=/app
WORKDIR $APP_HOME

COPY ./Gemfile* $APP_HOME/
RUN bundle install

Dockerfileの修正後、再度ビルドを実行する。

docker-compose build
docker-compose run --rm app git --version
git version 2.24.1
docker-compose run --rm app whoami
jets

カレントディレクトリにプロジェクトを作成する。 README.md、Gemfileを既に作成しているため、forceオプションを指定する。

docker-compose run --rm app bundle exec jets new . --force

MySQLインストールエラー

MySQLに関するエラーが発生した。

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /usr/local/bundle/gems/mysql2-0.5.3/ext/mysql2
/usr/local/bin/ruby -I /usr/local/lib/ruby/site_ruby/2.5.0 -r ./siteconf20200119-7-19w4mew.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
checking for -lmysqlclient... no
-----
mysql client is missing. You may need to 'sudo apt-get install libmariadb-dev', 'sudo apt-get install libmysqlclient-dev' or 'sudo yum install
mysql-devel', and try again.
-----

ライブラリが足りていなかったので追加する。

FROM ruby:2.5-alpine

RUN apk update && \
    apk add --no-cache build-base libxml2-dev libxslt-dev git mariadb-dev

RUN adduser -S jets && \
    echo "jets ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers && \
    echo 'jets:jets' | chpasswd

USER jets
ENV APP_HOME=/app
WORKDIR $APP_HOME

COPY ./Gemfile* $APP_HOME/
RUN bundle install

Dockerfileの修正後、再度ビルドを実行する。

docker-compose build
docker-compose run --rm app bundle exec jets new . --force

Yarnインストール

Yarnのインストールを行う必要がある。

Yarn is not installed or has not been detected. Please double check that yarn has been installed.
To check:

    which yarn

If it is not installed, you can usually install it with:

    npm install -g yarn

Dockerfileを修正する。

FROM node:alpine as builder

FROM ruby:2.5-alpine

RUN apk update && \
    apk add --no-cache build-base libxml2-dev libxslt-dev git mariadb-dev

RUN adduser -S jets && \
    echo "jets ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers && \
    echo 'jets:jets' | chpasswd

ENV YARN_VERSION=1.21.1
COPY --from=builder /opt/yarn-v$YARN_VERSION /opt/yarn/
COPY --from=builder /usr/local/bin/node /usr/local/bin/
RUN ln -s /opt/yarn/bin/yarn /usr/local/bin/yarn && \
    ln -s /opt/yarn/bin/yarnpkg /usr/local/bin/yarnpkg

USER jets
ENV APP_HOME=/app
WORKDIR $APP_HOME

COPY ./Gemfile* $APP_HOME/
RUN bundle install

Dockerfileの修正後、再度ビルドを実行する。

docker-compose build
docker-compose run --rm app which yarn
docker-compose run --rm app yarn --version

Yarnのインストール確認後、再度プロジェクトを作成する。

docker-compose run --rm app bundle exec jets new . --force

以下のメッセージが表示されたら完了。

================================================================
Congrats 🎉 You have successfully created a Jets project.

Cd into the project directory:
  cd .

To start a server and test locally:
  jets server # localhost:8888 should have the Jets welcome page

Scaffold example:
  jets generate scaffold post title:string body:text published:boolean
  jets db:create db:migrate

To deploy to AWS Lambda, edit your .env.development.remote and add a DATABASE_URL endpoint.
Then run:

  jets deploy

Bundle用ボリューム作成

最後にdocker-compose.ymlを修正する。

  • 毎度bundle installが必要になるのでインストール先専用のボリュームを作成
  • Jets起動コマンドを追加
version: '3'
services:
  app:
    build:
      context: .
      dockerfile: ./docker/ruby/Dockerfile
    volumes:
      - .:/app
      - bundle-data:/usr/local/bundle
    ports:
      - "8888:8888"
    command: bundle exec jets server --port 8888 --host 0.0.0.0
volumes:
  bundle-data:
    driver: local

起動

Dockerを起動する。

docker-compose up

以下の画面が表示される。

f:id:kzp:20200122080450p:plain
Ruby on Jets Welcome