# Windowsでノンストレスrails new ファイルとコマンドをコピペするだけでWindows&WSL環境で権限エラーが発生しないrailsのディレクトリを作成できます ※vendorにgemを入れている関係で素のrailsコマンドが使えないので`bin/rails`か`bundle exec rails`コマンドを使いましょう ## ディレクトリとファイルの準備 ``` mkdir deploy_sample cd deploy_sample touch Dockerfile.dev touch compose.yml ``` ## Dockerを使った開発環境の作成 ### PostgreSQL(Node.jsあり) Dockerfile.dev ```dockerfile FROM ruby:3.3.6 ARG UID=1000 ENV LANG C.UTF-8 ENV TZ Asia/Tokyo ENV BUNDLE_APP_CONFIG /myapp/.bundle RUN apt-get update -qq \ && apt-get install -y ca-certificates curl gnupg \ && mkdir -p /etc/apt/keyrings \ && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \ && NODE_MAJOR=20 \ && echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list \ && wget --quiet -O - /tmp/pubkey.gpg https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \ && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs yarn vim RUN useradd -m -u $UID rails RUN mkdir /myapp WORKDIR /myapp COPY . /myapp RUN chown rails:rails -R /myapp ``` compose.yml ```yml services: db: image: postgres restart: always environment: TZ: Asia/Tokyo POSTGRES_PASSWORD: password volumes: - postgresql_data:/var/lib/postgresql ports: - 5432:5432 healthcheck: test: ["CMD-SHELL", "pg_isready -d myapp_development -U postgres"] interval: 10s timeout: 5s retries: 5 web: build: context: . dockerfile: Dockerfile.dev command: bash -c "bundle install && bundle exec rails db:prepare && rm -f tmp/pids/server.pid && ./bin/dev" tty: true stdin_open: true volumes: - .:/myapp environment: TZ: Asia/Tokyo ports: - "3000:3000" user: "1000:1000" depends_on: db: condition: service_healthy volumes: postgresql_data: ``` Bootstrap ``` docker compose build docker compose run --rm web /bin/bash -c "bundle init && bundle add rails" docker compose run --rm web /bin/bash -c "bundle config set --local path vendor/bundle && bundle in stall" docker compose run --rm web bundle exec rails new . -d postgresql -j esbuild --css=bootstrap echo '/vendor/bundle' >> .gitignore ``` Tailwind ``` docker compose build docker compose run --rm web /bin/bash -c "bundle init && bundle add rails" docker compose run --rm web /bin/bash -c "bundle config set --local path vendor/bundle && bundle in stall" docker compose run --rm web bundle exec rails new . -d postgresql -j esbuild --css=tailwind echo '/vendor/bundle' >> .gitignore ``` config/database.ymlにhost・username・passwordを追加 ```yml default: &default adapter: postgresql encoding: unicode # For details on connection pooling, see Rails configuration guide # https://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> host: db username: postgres password: password ``` Procfile.devのwebに` -b 0.0.0.0 -p 3000`をつける ``` web: env RUBY_DEBUG_OPEN=true bin/rails server -b 0.0.0.0 -p 3000 js: yarn build --watch css: yarn watch:css ``` ``` docker compose up ``` `http://localhost:3000`