After successfully Dockerizing your Ruby on Rails application, the next step is to deploy it on a cloud hosting provider. This follow-up blog post is targeted at software developers who have no experience deploying Docker containers on the cloud. I'll walk you through selecting an appropriate cloud hosting provider, recommend various providers based on different needs, and guide you through the process of setting up the host and taking your project to production, including setting the domain DNS to point to the live application.
Selecting a Cloud Hosting Provider
Choosing the right cloud hosting provider for your Dockerized Rails application depends on various factors such as cost, ease of use, scalability, and support for Docker. Some popular cloud hosting providers that support Docker include:
- Amazon Web Services (AWS)
- Google Cloud Platform (GCP)
- Microsoft Azure
- DigitalOcean
- Heroku
While AWS, GCP, and Azure offer comprehensive cloud services with extensive scalability options, they can be more complex and expensive. DigitalOcean and Heroku, on the other hand, are more developer-friendly and cost-effective, making them ideal for smaller projects or startups.
For this tutorial, I'll demonstrate deploying your Dockerized Rails application on DigitalOcean, as it offers a straightforward setup process and reasonable pricing.
Deploying Your Dockerized Rails Application on DigitalOcean
- Sign up for a DigitalOcean account if you haven't already: https://www.digitalocean.com/
- Create a new project on DigitalOcean and select the "Docker" One-Click App.
- Choose the size of the Droplet (virtual server) based on your application's requirements and your budget.
- Select the datacenter region that is closest to your target audience to minimize latency.
- Add your SSH keys to the Droplet for secure access.
- Create the Droplet and wait for it to be provisioned.
- Once the Droplet is provisioned, you will receive an IP address. Use an SSH client to connect to your Droplet using the provided IP address and your SSH keys.
- Clone your Rails application repository onto the Droplet:
$ git clone <your_repository_url> my_docker_app $ cd my_docker_app
9. Ensure that your docker-compose.yml file is configured for production. Set the RAILS_ENV environment variable to production and update any other necessary settings.
To set the RAILS_ENV environment variable to production in your docker-compose.yml file, add an environment section to the web service, like this:
version: '3' services: db: image: postgres volumes: - ./tmp/db:/var/lib/postgresql/data web: build: . command: bundle exec rails s -p 3000 -b '0.0.0.0' volumes: - .:/my_docker_app ports: - "3000:3000" depends_on: - db environment: - RAILS_ENV=production - RAILS_SERVE_STATIC_FILES=true - RAILS_LOG_TO_STDOUT=true
This sets the RAILS_ENV variable to production for the web service, and also enables serving static files and logging to STDOUT, which are common settings for production environments.
10. Build and run the Docker containers:
$ docker-compose build $ docker-compose up -d
11. Set up your domain name by creating an A record that points to the IP address of your DigitalOcean Droplet. This process varies depending on your domain registrar, so refer to their documentation for instructions.
12. Configure your Rails application to use your domain name. Update the config/application.rb file to include the following:
config.action_controller.default_url_options = { host: 'yourdomain.com' } config.action_mailer.default_url_options = { host: 'yourdomain.com' }
13. Restart your Docker containers:
$ docker-compose restart
Your Dockerized Rails application should now be live and accessible at your domain name.
Conclusion
In this blog post, I've covered how to select an appropriate cloud hosting provider, recommended various providers based on different needs, and guided you through deploying your Dockerized Rails application on DigitalOcean. By following these steps, you'll have a solid foundation for deploying your Docker container on a cloud hosting provider and scaling your application as needed.