- A GitHub account for source code and container registry
- A bunny.net account with Magic Containers enabled
- Ruby 3.2+ installed locally
Create the Rails app
- Full-stack
- API only
Create a new Rails application with views and assets:Generate a controller with a view:Add the route in Update the view in
config/routes.rb:config/routes.rb
app/views/home/index.html.erb:app/views/home/index.html.erb
Run locally
Start the Rails development server:Prepare for production
Rails 7.1+ automatically generates a production-readyDockerfile in your project root.
Before building, prepare the app:
If you’re using an older Rails version, you can generate a Dockerfile with:
bash bin/rails generate dockerfile Review the Dockerfile
Rails 7.1+ generates a production-ready, multi-stage Dockerfile that includes:- jemalloc for reduced memory usage
- Bootsnap precompilation for faster boot times
- Non-root user for security
- Asset precompilation (full-stack apps only)
- Thruster HTTP/2 proxy (full-stack apps only)
Build and push to GitHub Container Registry
- GitHub Actions
- Docker CLI
Create Push your code to trigger the workflow:
.github/workflows/build.yml to automatically build and push on every commit to main:.github/workflows/build.yml
If your package is private, set the visibility to Public in GitHub or
configure Magic Containers with registry
credentials.
Deploy to Magic Containers
1
Create a new app
In the bunny.net dashboard, go to Magic Containers and click Add
App. Enter a name and select your deployment option.
2
Add a container
Click Add Container, then configure:
| Field | Value |
|---|---|
| Registry | GitHub Container Registry |
| Image | YOUR_USERNAME/app-rails |
| Tag | latest |
3
Configure environment variables
Go to the Environment Variables tab and add:
| Variable | Value |
|---|---|
SECRET_KEY_BASE | Generate with bin/rails secret |
4
Add an endpoint
Go to the Endpoints tab, click Add New Endpoint, and set the
container port to
80.5
Deploy
Click Add Container, then Next Step, and Confirm and Create.
Test your app
Response
You can add a custom hostname from the Endpoints section in your app settings.
Add persistent storage
Magic Containers are ephemeral, so data stored locally is lost on restarts. For Rails apps that need to persist data (Active Storage uploads, etc.), attach a Persistent Volume.1
Add a volume
Go to your app’s Volumes tab and click Add Volume. Set the mount path to
/rails/storage and choose an initial size.2
Configure Rails storage
Update
config/environments/production.rb to use the volume for Active Storage:config/environments/production.rb
3
Configure SQLite (optional)
To persist your SQLite database, update
config/database.yml:config/database.yml