CF is command line tool to interact with Cloud Foundry.IBM Bluemix uses Cloud Foundry.So you can use CF commands to interact with IBM Bluemix.Cloud Foundry is one of the develop and deployment tools available.For other development and deployment tool you can read this post.
After creating Cloud Foundry app on Bluemix you can use CF command line tools to push your updated app on Bluemix.In this post I will list out useful CF commands.These are the commands most useful for development and deployment of your app as well as interacting with Bluemix.
If you have not yet downloaded Cloud Foundry then do so from this link.It is open source.It is available for most operating systems like Fedora,Debian,MacOS and Windows.For all platforms installers are present so installing it is simple task.
IBM Bluemix Log in and Log out
After installing Cloud Foundry command line interface run below command.Below command will connect you to Bluemix.Before log into Bluemix you have to connect with Bluemix using below command.
cf api https://api.ng.bluemix.net
After connecting with Bluemix now you can login to your account.Log in command may seem confusing if you see IBM documentation for CF command line for Bluemix. I will take that command here and will explain it.So that when you encounter that command reading the manuals you do not get confused.
cf login -u your-ibm-id -o your-organization-name -s your-space-name
Above is the commands for login as per IBM documentation.This command is made up of three CF sub commands.Note I have used place holders in above command.Replace the texts your-ibm-id,your-organization-name,your-space-name with appropriate values.
- cf login – Command used for login to Bluemix
- cf -u – Mention your user name which in this case is your ibm id
- cf -o – Mention your organization name.Bluemix creates a organization name for each user id>By default it is same as your IBM id.But you can change it using CF commands or from Bluemix Dashboard.It is recommended to change it.
- cf -s – Mention space name.
Each app or container or virtual machine you create is hold with in a space.If you mention it then you are logged into that space name.You can ignore this option from above login command.But if you want to push your application to Bluemix you will have to select a space.You can either select it while login or separately.If you have not mentioned space name while login then you can run below command to select an space
cf -s space-name
To log out from Bluemix run below command
If you close the terminal then also your Bluemix session gets over.But it is always recommended to log out of your session instead of closing the terminal to close the session.
Pushing your app to IBM Bluemix
For a developer this is the task you will be doing most often.It is simple then login.Login is the most complex part of Cloud Foundry Bluemix interaction part.Once you got that rest of the part is easy.To push your app to Bluemix you must first change your current working directory to app directory.
You can use commands as per your operating system (cd command).After you have changed your present working directory to app directory you want to push run below command
cf push your-app-name
If you have followed Cloud Foundry Web App tutorial then you need to replace your-app-name with BlueMixText.After running this command your app is pushed to Bluemix.If there is no coding syntax error then the app gets staged successfully.You can see it live on app url.
But in case there is any error then it will not be staged.If you access the app URL you will get error like this does not exist.Bluemix stops the app if there is any error.In next point we will see how to find out problematic code using CF command.
Debug your app Bluemix App
Many a times while developing sample Bluemix app I had to use this command.This command is not mentioned in introductory IBM Bluemix manual.I faced problems debugging an app.As mentioned above if there is any syntax error then app will not run.But how to get the exact place of issue.
To solve exactly this problem you can use below command and trick.It is very helpful and provides good amount of details.It also points you to line you have made mistake.
cf logs --recent your-app-name >> error.txt
The above command writes recent log messages to error.txt file.You can then open error.txt file in any text editor.CF prints recent call trace and provides the line causing the issue.You can use the information to correct your code.The amount of information displayed is sufficient in most cases.
If you ignore –recent from above command then entire log is printed in the file.Since there is too much information I would suggest to always use –recent option with logs command.
Getting list of all CF commands
Cloud Foundry command line interface can be used to perform below tasks
- Basic Tasks like Login,Logout and changing user password
- App Management like Start,Stop,Delete,Rename,restart,restage and get list of all apps.
- Services Management like create,delete,update,list all services of app.Bind a service to your app Org Management like list all orgs,get information about an org,delete,create and rename an org
- Space management like creating,deleting,rename space and get list of all spaces
- Domain management like Create,delete domains.
Apart from above tasks you can perform administrative tasks like User Administration,Org Administration,Space administration and Service administration.You can also use it for advanced tasks like creating your own buildpacks.I will not touch upon all these commands in this post.
You can perform these tasks from Bluemix dashboard.I would recommend using Bluemix dashboard for these tasks and use Cloud Foundry command line interface for basic App develop deploy cycle.You can get list of all CF commands using below command. I have used below command to get list of all CF commands
cf --help >> cf-commands.txt
This will give you list of all Cloud Foundry commands.All the commands are with one liner detail.So you can easily figure out the use case of each command.Above command also prints environment variables.You can use these environment variables to customize your CF command line interface.
Bluemix is created on top of Cloud Foundry.Out of all develop and deploy options Cloud Foundry is the most powerful one.It can be used to perform all types of tasks without using Bluemix dashboard.
Are you using Cloud Foundry command line interface to interact with IBM Bluemix? Also feel free to share your queries and suggestions.