Deploying an App
In the previous module, you learned how to create your own application platform on Azure with Dokku. Congratulations! Now you are ready to create apps and push for deployments to this platform using a simple git push
.
Creating an Application
Dokku defaults to using Buildpack to deploy applications, unless a valid Dockerfile is detected at the root of your repository.
- Buildpacks: Using Heroku buildpacks to auto detect framework and runtime support for your application.
- Dockerfiles: Dockerfiles are used to define a portable execution environment built on a base OS of your choosing.
For the purpose of this training, we are going to deploy our application with builtin buildpacks. During git push
, Dokku will automatically detect the right Buildpack for your application, you will see -----> Node.js app detected
in the output.
SSH into the host we just provisioned using the private key of the key pair we created in the previous module for the purpose of ssh into the Dokku instance. For example, ~/.ssh/dokku, not the key pair for deploying apps.
If you get the following error: “Permissions 0664 for ‘dokku.pub’ are too open. It is required that your private key files are NOT accessible by others. This private key will be ignored. Load key “dokku.pub”: bad permissions Permission denied (publickey).” Set the correct permission for the file with
chmod 600 dokku.pub
. Then try SSH again into the VM.
Create a new Dokku application:
Adding MongoDB
Our Node.js application won’t work without a database. Previously we were test our app locally with a MongoDB instance running locally. Now we we need to create a MongoDB instance for our application data on Dokku and link it to the application we just created by using the Dokku Mongo plugin
:
Each official datastore offers a link
method to link a service to any application. We will link the new mongodb instance to our Node.js application. Note the environment variable MONGO_URL
has been set to the new instance. We will need this environment variable in a later step.
To verify that we have linked the database and the Node.js application successfully:
Add Dokku Remote Repo
From your local environment (not the Dokku VM), navigate to your project folder. Add a dokku
remote to your local git repository using the dokku
user name to push the app.
Add SSH Key
Before you can push anything to remote git, you have to first add the private key of the key pair you created for deploying apps to Dokku in the previous module. For example, ~/.ssh/dokkuapps, not the key pair created for ssh into the Dokku instance. > Note: If you forgot to create a second set of key pairs for deploying apps or happened to copy the incorrect key during setup, then you can follow the section below to update the value for the key.
Adding SSH Key from Windows
If you are using Windows, run the following commands from Git Bash to add the private key of the key pair you created for deploying apps to Dokku in the previous module. For example, ~\.ssh\dokkuapps
, not the key pair created for ssh into the Dokku instance.
> Note: If you forgot to create a second set of key pairs for deploying apps or happened to copy the incorrect key during setup, then you can follow the section below to update the value for the key.
Update Dokku Deployment Key
If you forgot to create a second set of key pairs for deploying apps or happened to copy the incorrect key during setup, then you can do the following to update the Dokku App Deployment key. From a terminal on Mac or a command prompt on Windows, type the following:
cat <path-to-dokku-app-deployment-public-key> | ssh -i <your-dokku-VM-ssh-private-key> <your-admin-user-name>@<DNSNAMEFORPUBLICIP>.<LOCATION>.cloudapp.azure.com "sudo sshcommand acl-add dokku dokkuappdeploy"
Update Environment File for Mongo
Before we push our app to Dokku, we need to update the .env.example
file to tell the app to use the MongoDB instance we just created.
Recall this is the current .env.example
configuration setting for Mongo:
MONGODB=mongodb://localhost:27017/test
We need to update this value to the $MONGO_URL
environment variable set in the previous step when we linked the Mongo service to this app.
MONGODB=$MONGO_URL
Save this file, then use git add
and git commit
to push the change to your local master branch so that the updates will be deployed to Dokku when we push the app.
Git Push to Deploy
Good job! We are almost there! Now we can finally push our application to Dokku.
Deploy the app with git push
and Dokku will create the application on the server.
Your output should look similar to below. Notice how Dokku automatically detects our app is a Node.js app.
The entire process builds a Docker image with all the dependencies our Node.js app needs. At the end, it will start the container with the recently built image deployed to the domain address we specified in the previous module.
To verify the application has been deployed successfully, browse to the URL at the end of the output (e.g.:http://hackathon-starter.<HOSTPUBLICIP>.xip.io
) and you will see the app running. Try creating a new account.