数日前に Docker 化した Rails 6 アプリケーションがあります (現時点では開発環境用)。イメージにいくつか問題がありましたが、なんとか修正できました。今ではアプリケーション全体がスムーズに動作し、Docker ですべてが正常に動作します。
唯一の問題は、Webpacker のコンパイルに時間がかかりすぎることです。 Docker を使用する前はすでに 30 秒ほどかかっていましたが、現在では最大 5 分、最短でも 3 分かかることがあります。
ボリュームを利用して、packs フォルダーをキャッシュしてみました。 - ./public/packs:/app_name/public/packs:cached しかし、何も変わっていなかったようです。
他の解決策はありますか?
@β.εηοιτ.βε が述べているように、開発にはローカルに (ファイル システム上に直接) マウントされたボリュームを使用できます。
以下に、マルチステージを活用するために docker compose と Dockerfile を考慮したアプローチの組み合わせを追加した例を示します (私が提案しているのは一般的なソリューションであることに留意してください)。
docker-compose.yml
version: '3.9'
services:
  api: # or whatever name you use
    build:
      dockerfile: Dockerfile
      context: .
      # use just the development stage
      target: development
    command: <command to start in dev mode>
    expose:
      - ${PORT}
    ports:
      - ${PORT}:${PORT}
    env_file:
      - .env
    volumes:
      # use the local folder mapped to `/app` in container volume
      - .:/app
      - <directory_you_want_to_include_in_volume>:/app/<target_in_volume>
volumes:
  <directory_you_want_to_include_in_volume>:
    driver: local
Dockerfile
FROM <your_image> as development
# Create app directory
WORKDIR /app
# Copy source files
COPY . .
# Install with options as needed
RUN bundle install 
# Build API
RUN <any command to build/bundle your app>
# here the development build will stop, 
# and the start command of the docker compose file will take care.
FROM <your_image> as production
# Create app directory
WORKDIR /app
# Copy necessary files to run the app
COPY --from=development /app/<whatever_needed> ./
# Install only production dependencies
RUN <specific_production_bundler>
EXPOSE 80
CMD ["bundle", "exec", "rails"]