# syntax = docker/dockerfile:1 FROM node:slim AS base RUN apt-get update &&\ apt-get install --yes ruby FROM base as build RUN apt-get install --yes ruby-dev build-essential git RUN gem install rails RUN rails new demo --skip-active-record --javascript esbuild WORKDIR demo RUN yarn add react react-dom FROM base WORKDIR demo COPY --from=build /demo /demo COPY --from=build /var/lib/gems /var/lib/gems RUN bin/rails generate controller Time index RUN cat <<-"EOF" >> app/javascript/application.js import "./components/counter" EOF RUN mkdir app/javascript/components COPY <<-"EOF" app/javascript/components/counter.jsx import React, { useState, useEffect, useRef } from 'react'; import { createRoot } from 'react-dom/client'; const Counter = ({ arg }) => { const [count, setCount] = useState(0); const countRef = useRef(count); countRef.current = count; useEffect(() => { const interval = setInterval(() => { setCount(countRef.current + 1); }, 1000); return () => clearInterval(interval); }, []); return
{`${arg} - counter = ${count}!`}
; }; document.addEventListener("DOMContentLoaded", () => { const container = document.getElementById("root"); const root = createRoot(container); root.render(); }); EOF COPY <<-"EOF" app/views/time/index.html.erb
React Logo
ruby=<%= RUBY_VERSION %> rails=<%= Rails::VERSION::STRING %>>
EOF COPY <<-"EOF" config/routes.rb Rails.application.routes.draw { root "time#index" } EOF ENV RAILS_ENV=production RUN bin/rails assets:precompile EXPOSE 3000 CMD bin/rails server