Rails Application Deployment with Kamal: A Modern DevOps Solution

Deploying Rails applications has traditionally involved complex server configurations, intricate deployment pipelines, or reliance on expensive Platform-as-a-Service (PaaS) providers. Kamal, introduced by 37signals (the creators of Ruby on Rails), changes this paradigm by offering a straightforward, Docker-based deployment solution that puts you back in control of your infrastructure without the operational overhead.

In this guide, we'll explore how Kamal simplifies Rails deployment, walk through the setup process, and examine why development teams are increasingly adopting this approach for production applications.

What is Kamal and Why Does It Matter?

Kamal is an open-source deployment tool that leverages Docker containers and SSH to deploy web applications to any server. Originally developed for deploying Hey.com and Basecamp, Kamal represents 37signals' philosophy of owning your infrastructure while maintaining simplicity.

Unlike traditional deployment tools, Kamal doesn't require you to install complex agents or maintain elaborate CI/CD pipelines. It works with any cloud provider or bare-metal server that you can SSH into, giving you the freedom to choose your hosting environment without vendor lock-in.

Key benefits include:

  • Zero-downtime deployments using a proven health-check system
  • Simple configuration through a single YAML file
  • Automatic SSL certificate management via Let's Encrypt
  • Built-in support for accessories like Redis, PostgreSQL, and other services
  • Significant cost savings compared to PaaS solutions
Prerequisites and Initial Setup

Before deploying with Kamal, ensure you have the following:

  • A Rails application ready for deployment
  • Docker installed on your local development machine
  • One or more servers with SSH access (Ubuntu 20.04+ or similar Linux distributions recommended)
  • A Docker Hub account or access to another container registry
  • Ruby 3.0+ installed locally
Install Kamal by adding it to your Gemfile:

gem 'kamal'

Or install it globally:

gem install kamal

Once installed, initialize Kamal in your Rails project:

kamal init

This command generates two essential files: config/deploy.yml for your deployment configuration and .kamal/secrets for sensitive credentials.

Configuring Your Deployment

The deploy.yml file is where all your deployment magic happens. Here's a practical configuration breakdown:

Start by defining your service name and Docker image location. Kamal builds your application into a Docker container and pushes it to a registry. Specify your application name, the registry username, and the servers where you want to deploy.

Configure your web service with the appropriate port mappings. Rails typically runs on port 3000 inside the container, which Kamal maps to port 80 (HTTP) or 443 (HTTPS) on your server.

Set up environment variables for your Rails application. These include your RAILS_MASTER_KEY or credentials for encrypted secrets, database connection strings, and any third-party API keys. Kamal securely injects these during deployment without exposing them in your repository.

Define accessories for services like PostgreSQL, Redis, or Sidekiq. Kamal manages these as separate Docker containers on your infrastructure, ensuring they're properly networked with your application.

Executing Your First Deployment

With configuration complete, deploying is remarkably straightforward. First, ensure your servers are prepared:

kamal server bootstrap

This command installs Docker and sets up the necessary directory structure on your target servers. You only need to run this once per server.

To deploy your application:

kamal deploy

Kamal performs several operations automatically:

  • Builds your Docker image locally
  • Pushes the image to your container registry
  • Pulls the image to your servers
  • Starts new containers with your updated code
  • Performs health checks to ensure the new version is responding
  • Routes traffic to the new containers
  • Removes old containers only after successful deployment
The entire process typically completes in minutes, and because Kamal uses a rolling deployment strategy with health checks, your application experiences zero downtime.

Managing SSL Certificates and Domains

Kamal includes built-in SSL certificate management through its Traefik integration. Traefik acts as a reverse proxy that automatically obtains and renews Let's Encrypt certificates for your domains.

Configure SSL in your deploy.yml by specifying your domain name and enabling the Traefik accessory. Kamal handles the certificate acquisition process during deployment, and Traefik automatically renews certificates before they expire.

For applications serving multiple domains or requiring custom SSL configurations, Kamal's Traefik integration provides extensive flexibility through labels and routing rules.

Ongoing Operations and Best Practices

After your initial deployment, Kamal provides several commands for day-to-day operations:

View application logs across all servers with kamal app logs. This aggregates logs from all running containers, making debugging straightforward.

Execute Rails console commands remotely using kamal app exec, allowing you to run migrations, open a console, or execute rake tasks without SSH-ing into servers.

Roll back to a previous version instantly with kamal rollback [version] if issues arise post-deployment.

Best practices for production Kamal deployments include:

  • Use separate configurations for staging and production environments
  • Store secrets securely using environment variables or secret management tools
  • Implement comprehensive health check endpoints in your Rails application
  • Monitor Docker resource usage on your servers
  • Set up automated backups for your database accessories
  • Use multiple servers with load balancing for high-availability applications
Conclusion: Simplify Your Rails Deployment Journey

Kamal represents a significant shift in how we approach Rails application deployment. By combining the portability of Docker with the simplicity of SSH-based orchestration, it delivers enterprise-grade deployment capabilities without enterprise-grade complexity or costs.

Whether you're running a small SaaS application or managing multiple production services, Kamal provides the reliability and control you need while eliminating unnecessary abstraction layers. The result is faster deployments, lower infrastructure costs, and a deployment process you actually understand.

Ready to modernize your Rails deployment strategy? Our team specializes in helping companies transition to modern DevOps practices, including Kamal implementations, infrastructure optimization, and deployment automation. Contact us today to discuss how we can streamline your deployment pipeline and reduce your infrastructure costs while maintaining the reliability your users expect.