Hoc blog est pessimus

Install Dokku and deploy your Rails project with git

September 30, 2024

This time we’ll do a speedrun of installing Dokku on a remote server, deploying a Rails project with Postgresql and configuring a domain name. You should already have a remote server with Ubuntu 20.04/22.04/24.04 or Debian 11+ x64 for this.

Let’s go!

Installing Dokku

Ssh to your remote server and install Dokku using the Dokku install script:

    
      ssh root@YOUR-SERVER-IP
      wget -NP . https://dokku.com/install/v0.35.4/bootstrap.sh
      sudo DOKKU_TAG=v0.35.4 bash bootstrap.sh
    
  

Once the installation is complete, you should configure an ssh key and set your global domain:

    
      cat ~/.ssh/authorized_keys | dokku ssh-keys:add admin
      dokku domains:set-global YOUR-SERVER-IP
    
  

Deploying your Rails project

Still on your Dokku host, create a new Dokku app:

dokku apps:create YOUR-PROJECT-NAME

Install the Dokku Postgresql plugin, create a database and link it to your project:

    
      sudo dokku plugin:install https://github.com/dokku/dokku-postgres.git
      dokku postgres:create railsdatabase
      dokku postgres:link railsdatabase YOUR-PROJECT-NAME
    
  

Then, on your local development machine, go to your Rails project folder, set up the Git remote and push your code to your new Dokku instance:

    
      git remote add dokku dokku@YOUR-SERVER-IP:YOUR-PROJECT-NAME
      git push dokku main
    
  

Configuring your DNS settings for your domain

If you’ve just bought a domain name, you should delete the existing DNS records for the domain. What you’ll need, is a record with the following configuration:

    
      Type: A
      Host: The domain name you’ve bought, or a subdomain you might want to use, i.e. YOUR-DOMAIN-OR-SUBDOMAIN-NAME
      Answer: YOUR-SERVER-IP
      TTL: Doesn’t really matter, can be 5 min / 300 seconds
    
  

Then go back to your server’s ssh session and run:

dokku domains:add YOUR-PROJECT-NAME YOUR-DOMAIN-OR-SUBDOMAIN-NAME

And the last thing is to remember to setup your Rails database and its migrations:

dokku run rails db:setup

Maintaining your Dokku app

With this setup, you'll have to remember to push your code updates to Dokku, when you want to update your running app.

		
			git push dokku
		
	

Should you need to debug your application, you can read the logs by logging in to your server and running the following:

		
			dokku run YOUR-PROJECT-NAME logs -t
		
	

That is all! Hope it worked!