Skip to content

Instantly share code, notes, and snippets.

@mbenatti
Forked from nhu313/deploy_dokku.md
Last active May 24, 2021 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbenatti/6819ba001492dbbb008dc72195834c90 to your computer and use it in GitHub Desktop.
Save mbenatti/6819ba001492dbbb008dc72195834c90 to your computer and use it in GitHub Desktop.
Deploying phoenix on Dokku through Digital ocean

This write up was based on Henrik's gist

Create server

  1. Set up a virtual machine on azure, aws, digital ocean or whatever
  2. Don't forget to Setup inbounding rule (in some cases) for azure: https://docs.microsoft.com/en-us/azure/virtual-machines/windows/nsg-quickstart-portal, for open a listening port
  3. install dokku http://dokku.viewdocs.io/dokku/

Setup your server

  1. ssh into your server (ex: ssh root@104.236.34.69)
  2. Run these commands in the terminal. 1. Change the app_name to your application name or something like prod or qa. 1. Change app_db to your database name(if you are using postgrex) 1. Change SECRET_KEY_BASE="" to actual key from your prod.secret.exs file 1. Optional: You can copy this into a setup.sh file and run it with ./setup.sh. (Remember to change the file permission chmod u+x ./setup.sh)
#commands to run on your server
dokku apps:create app_name
dokku config:set app_name BUILDPACK_URL=https://github.com/mbenatti/heroku-buildpack-multi.git
dokku config:set app_name LC_ALL=en_US.utf8

#For postgres
sudo dokku plugin:install https://github.com/dokku/dokku-postgres.git
dokku postgres:create app_db
dokku postgres:link app_db app_name
dokku config:set app_name SECRET_KEY_BASE="key"

#For mongodb
dokku config:set app_name MONGODB_URI="mongodb_uri"

#Port
dokku config:set app_name PORT=80

#Last but not least
dokku config:set app_name MIX_ENV=prod

#add a domain
dokku domains:add app_name sdev.yoursite.com.br

Setup your project

Create the following file in your project root folder

  1. Create a file named.buildpacks with the following content
https://github.com/HashNuke/heroku-buildpack-elixir.git
https://github.com/gjaldon/heroku-buildpack-phoenix-static.git
  1. Create a file named elixir_buildpack.config with the following content (change it to your setting)
erlang_version=19.3
elixir_version=1.4.2
always_rebuild=false
post_compile="pwd"
  1. Create a file named phoenix_static_buildpack.config with the following content (change it to your setting)
node_version=6.10.2
npm_version=3.10.10
#Phx 1.3 configs
phoenix_relative_path=apps/app_web_or_api
assets_path=assets
phoenix_ex=phx 
#optional
clean_cache=true
  1. Open up config/prod.exs
  2. Remove this line import_config "prod.secret.exs" (at the end of the file) to prevent it from importing prod.secret
  3. Add secret_key_base: System.get_env("SECRET_KEY_BASE") underneath the main config. Ex: (not required for stage or dev, search other way do to it skiping this step)
  4. Change 'url: [host: xxx]' to host: System.get_env("HOSTNAME") || "localhost" (you can pass the port: 80, if you wish)
```
config :App, App.Endpoint,
  http: [port: {:system, "PORT"}],
  url: [host: System.get_env("HOSTNAME") || "localhost"],
  cache_static_manifest: "priv/static/manifest.json",
  secret_key_base: System.get_env("SECRET_KEY_BASE")
```
  
Remember to add the **comma** before adding `secret_key_base`
  1. Optional: Add database setting to the end of the file, change blog to your app name and db_name to your database name 1. Note you must get url from DATABASE_URL because that's where Dokku set your database url
config :app, App.Repo,
  adapter: Ecto.Adapters.Postgres,
  database: "db_name",
  url: System.get_env("DATABASE_URL"),
  pool_size: 20
  
 #or mongodb
  config :app, App.Repo,
  adapter: Mongo.Ecto,
  url: System.get_env("MONGODB_URI"),
  pool_size: 20

Upload SSH For Dokku

  1. Create a custom RSA key: ssh-keygen -t rsa (or use your id_rsa.pub)
  2. Enter custom as the file name
  3. Just set an empty passphrase
  4. Add the custom.pub key to Dokku's VM by running this command: (refer to the settings of the VM)
$ cat ~/.ssh/id_rsa.pub | ssh mbenatti@13.66.51.208 "sudo sshcommand acl-add dokku my-ssh-key"

Deploy

  1. Add a git remote git remote add ocean dokku@ip_address:app_name
  2. Note you have to log in using dokku as the user
  3. Ex: `git remote add ocean dokku@104.236.34.69:app_name
  4. Deploy git push ocean master
  5. Optional: Setup your database
  6. ssh into your server
  7. Migrate database dokku run app_name mix ecto.migrate 4.Start aplication: dokku run app_name elixir --detached -S mix phoenix.server

For future deployment, all you have to do is git push dokku master

You can see all the dokku commands with dokku help.

Sources

  1. https://gist.github.com/col/a509ee4b73cecb347c97
  2. http://blog.pragtechnologies.com/deploying-phoenix-using-dokku-in-azure/
  3. https://gist.github.com/henrik/c70e32544e09c1a79841
  4. http://www.phoenixframework.org/docs/heroku
  5. https://www.digitalocean.com/community/tutorials/how-to-use-the-digitalocean-dokku-application
  6. http://dokku.viewdocs.io/dokku/application-deployment/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment