| copyright | lastupdated | keywords | subcollection | content-type | completion-time | ||
|---|---|---|---|---|---|---|---|
|
2021-09-16 |
app tutorial for code engine, application, apps, images, tutorial for code engine, deploying |
codeengine |
tutorial |
10m |
{:DomainName: data-hd-keyref="APPDomain"} {:DomainName: data-hd-keyref="DomainName"} {:android: data-hd-operatingsystem="android"} {:api: .ph data-hd-interface='api'} {:apikey: data-credential-placeholder='apikey'} {:app_key: data-hd-keyref="app_key"} {:app_name: data-hd-keyref="app_name"} {:app_secret: data-hd-keyref="app_secret"} {:app_url: data-hd-keyref="app_url"} {:audio: .audio} {:authenticated-content: .authenticated-content} {:beta: .beta} {:c#: .ph data-hd-programlang='c#'} {:c#: data-hd-programlang="c#"} {:cli: .ph data-hd-interface='cli'} {:codeblock: .codeblock} {:curl: #curl .ph data-hd-programlang='curl'} {:curl: .ph data-hd-programlang='curl'} {:deprecated: .deprecated} {:dotnet-standard: .ph data-hd-programlang='dotnet-standard'} {:download: .download} {:external: .external target="_blank"} {:external: target="_blank" .external} {:faq: data-hd-content-type='faq'} {:fuzzybunny: .ph data-hd-programlang='fuzzybunny'} {:generic: data-hd-operatingsystem="generic"} {:generic: data-hd-programlang="generic"} {:gif: data-image-type='gif'} {:go: .ph data-hd-programlang='go'} {:help: data-hd-content-type='help'} {:hide-dashboard: .hide-dashboard} {:hide-in-docs: .hide-in-docs} {:important: .important} {:ios: data-hd-operatingsystem="ios"} {:java: #java .ph data-hd-programlang='java'} {:java: .ph data-hd-programlang='java'} {:java: data-hd-programlang="java"} {:javascript: .ph data-hd-programlang='javascript'} {:javascript: data-hd-programlang="javascript"} {:middle: .ph data-hd-position='middle'} {:navgroup: .navgroup} {:new_window: target="_blank"} {:node: .ph data-hd-programlang='node'} {:note: .note} {:objectc: .ph data-hd-programlang='Objective C'} {:objectc: data-hd-programlang="objectc"} {:org_name: data-hd-keyref="org_name"} {:php: .ph data-hd-programlang='PHP'} {:php: data-hd-programlang="php"} {:pre: .pre} {:preview: .preview} {:python: .ph data-hd-programlang='python'} {:python: data-hd-programlang="python"} {:release-note: data-hd-content-type='release-note'} {:right: .ph data-hd-position='right'} {:route: data-hd-keyref="route"} {:row-headers: .row-headers} {:ruby: .ph data-hd-programlang='ruby'} {:ruby: data-hd-programlang="ruby"} {:runtime: architecture="runtime"} {:runtimeIcon: .runtimeIcon} {:runtimeIconList: .runtimeIconList} {:runtimeLink: .runtimeLink} {:runtimeTitle: .runtimeTitle} {:screen: .screen} {:script: data-hd-video='script'} {:service: architecture="service"} {:service_instance_name: data-hd-keyref="service_instance_name"} {:service_name: data-hd-keyref="service_name"} {:shortdesc: .shortdesc} {:space_name: data-hd-keyref="space_name"} {:step: data-tutorial-type='step'} {:step: data-tutorial-type='step'} {:subsection: outputclass="subsection"} {:support: data-reuse='support'} {:swift: #swift .ph data-hd-programlang='swift'} {:swift: .ph data-hd-programlang='swift'} {:swift: data-hd-programlang="swift"} {:table: .aria-labeledby="caption"} {:term: .term} {:terraform: .ph data-hd-interface='terraform'} {:tip: .tip} {:tooling-url: data-tooling-url-placeholder='tooling-url'} {:topicgroup: .topicgroup} {:troubleshoot: data-hd-content-type='troubleshoot'} {:tsCauses: .tsCauses} {:tsResolve: .tsResolve} {:tsSymptoms: .tsSymptoms} {:tutorial: data-hd-content-type='tutorial'} {:ui: .ph data-hd-interface='ui'} {:unity: .ph data-hd-programlang='unity'} {:url: data-credential-placeholder='url'} {:user_ID: data-hd-keyref="user_ID"} {:vbnet: .ph data-hd-programlang='vb.net'} {:video: .video}
{: #deploy-app-tutorial} {: toc-content-type="tutorial"} {: toc-completion-time="10m"}
With this tutorial, deploy an application with the {{site.data.keyword.codeengineshort}} CLI. The application scales to zero when not in use. {: shortdesc}
An application, or app, runs your code to serve HTTP requests. In addition to traditional HTTP requests, {{site.data.keyword.codeenginefull}} also supports applications that use WebSockets as their communications protocol. The number of running instances of an app are automatically scaled up or down (to zero) based on incoming workloads and your configuration settings. An app contains one or more revisions. A revision represents an immutable version of the configuration properties of the app. Each update of an app configuration property creates a new revision of the app.
Before you begin
Tutorials might incur costs. Use the Cost Estimator to generate a cost estimate based on your projected usage. {: note}
{: #deploy-app-image-file} {: step}
This tutorial uses a sample image, ibmcom/hello, which is a simple Hello World program. The program includes an environment variable TARGET, and prints Hello ${TARGET}. If the environment variable is empty, Hello World is returned.
If you have a container image that you want to use, you can replace the image reference in the next step with your own Docker repository, image name, and version.
You can review the code that is used for this example at ibmcom/hello.
{: #app-creating-deploying} {: step}
-
Create your application by using the
ibmcloud ce application createcommand. In the following example, usemyappas the name of the application and specifyibmcom/helloas the image to reference.ibmcloud ce application create --name myapp --image ibmcom/hello{: pre}
Example output
Creating application 'myapp'... [...] Run 'C:\Program Files\IBM\Cloud\bin\ibmcloud.exe ce application get -n myapp' to check the application status. OK https://myapp.4svg40kna19.us-south.codeengine.appdomain.cloud{: screen}
-
Run the
ibmcloud ce application getcommand to display details about the application, including the URL for themyappapplication.ibmcloud ce application get -n myapp{: pre}
Example output
Run 'ibmcloud ce application events -n myapp' to get the system events of the application instances. Run 'ibmcloud ce application logs -f -n myapp' to follow the logs of the application instances. OK Name: myapp ID: abcdefgh-abcd-abcd-abcd-1a2b3c4d5e6f Project Name: myproject Project ID: 01234567-abcd-abcd-abcd-abcdabcd1111 Age: 3m6s Created: 2021-07-11T06:39:41-05:00 URL: https://myapp.4svg40kna19.us-south.codeengine.appdomain.cloud Cluster Local URL: http://myapp.4svg40kna19.svc.cluster.local Console URL: https://cloud.ibm.com/codeengine/project/us-south/01234567-abcd-abcd-abcd-abcdabcd1111/application/myapp/configuration Status Summary: Application deployed successfully Image: ibmcom/hello Resource Allocation: CPU: 1 Ephemeral Storage: 500Mi Memory: 4G Revisions: myapp-huv70-1: Age: 3m6s Traffic: 100% Image: ibmcom/hello (pinned to f0dc03) Running Instances: 1 Runtime: Concurrency: 100 Maximum Scale: 10 Minimum Scale: 0 Timeout: 300 Conditions: Type OK Age Reason ConfigurationsReady true 52s Ready true 26s RoutesReady true 26s Events: Type Reason Age Source Messages Normal Created 3m9s service-controller Created Configuration "myapp" Normal Created 3m9s service-controller Created Route "myapp" Instances: Name Revision Running Status Restarts Age myapp-huv70-1-deployment-6656cfc796-7gl27 myapp-huv70-1 1/2 Terminating 0 3m9s{: screen}
-
Obtain the URL of the application from running the
application getcommand as described in the previous step. To retrieve the URL of the application directly, you can use the--outputoption and specify the URL format on theapplication getcommand. Additionally, you can run theibmcloud ce application listcommand to get the application URL.ibmcloud ce application get -n myapp -output url{: pre}
Example output
https://myapp.4svg40kna19.us-south.codeengine.appdomain.cloud{: screen}
-
Copy the URL from the previous output and call the application with
curl.curl https://myapp.4svg40kna19.us-south.codeengine.appdomain.cloud{: pre}
Example output
Hello World{: screen}
You successfully deployed and started a {{site.data.keyword.codeengineshort}} application!
{: #app-updating} {: step}
-
Update your newly created application by adding an environment variable to return
Hello Strangerwith theibmcloud ce application updatecommand.ibmcloud ce application update --name myapp --env TARGET=Stranger{: pre}
Example output
Updating application 'myapp' to latest revision. [...] Run 'ibmcloud ce application get -n myapp' to check the application status. OK https://myapp.4svg40kna19.us-south.codeengine.appdomain.cloud{: screen}
-
Use the
application getcommand to display details about your app, including the latest revision information.ibmcloud ce application get --name myapp{: pre}
Example output
Run 'ibmcloud ce application events -n myapp' to get the system events of the application instances. Run 'ibmcloud ce application logs -f -n myapp' to follow the logs of the application instances. OK Name: myapp ID: abcdefgh-abcd-abcd-abcd-1a2b3c4d5e6f Project Name: myproject Project ID: 01234567-abcd-abcd-abcd-abcdabcd1111 Age: 3m6s Created: 2021-07-11T06:39:41-05:00 URL: https://myapp.4svg40kna19.us-south.codeengine.appdomain.cloud Cluster Local URL: http://myapp.4svg40kna19.svc.cluster.local Console URL: https://cloud.ibm.com/codeengine/project/us-south/01234567-abcd-abcd-abcd-abcdabcd1111/application/myapp/configuration Status Summary: Application deployed successfully Environment Variables: Type Name Value Literal TARGET Stranger Image: ibmcom/hello Status Summary: Application deployed successfully Environment Variables: Type Name Value Literal TARGET Stranger Image: ibmcom/hello Resource Allocation: CPU: 1 Ephemeral Storage: 500Mi Memory: 4G Revisions: myapp-00002: Age: 54m Traffic: 100% Image: ibmcom/hello (pinned to f0dc03) Running Instances: 1 Runtime: Concurrency: 100 Maximum Scale: 10 Minimum Scale: 0 Timeout: 300 Conditions: Type OK Age Reason ConfigurationsReady true 25s Ready true 11s RoutesReady true 11s Events: Type Reason Age Source Messages Normal Created 4m17s service-controller Created Configuration "myapp" Normal Created 4m17s service-controller Created Route "myapp" Instances: Name Revision Running Status Restarts Age myapp-00001-deployment-bf7559548-mxgvq myapp-00001 2/3 Terminating 0 4m55s myapp-00002-deployment-8495f8ccb9-kmc57 myapp-00002 3/3 Running 0 88s{: screen}
From the output in the Revisions section, you can see the latest application revision of the
myappservice. Also, notice that 100% of the traffic to the application is running the latest revision of the app. -
Call the application.
curl https://myapp.4svg40kna19.us-south.codeengine.appdomain.cloud{: pre}
Example output
Hello Stranger{: screen}
From the output of this command, you can see the updated app now returns Hello Stranger.
{: #app-scaling} {: step}
The number of running instances of an application are automatically scaled up or down (to zero) based on incoming workload.
The following example illustrates how to scale your application with the CLI. You can control the maximum and minimum number of running instances of your app by changing the values of the --min-scale and --max-scale options with the application create or application update command.
-
Call the application.
curl https://myapp.4svg40kna19.us-south.codeengine.appdomain.cloud{: pre}
-
Run the
application getcommand to display the status of your application. Notice the value forRunning instances. In this example, the app has1running instance.ibmcloud ce application get -name myapp{: pre}
Example output
[...] Name: myapp [...] URL: https://myapp.4svg40kna19.us-south.codeengine.appdomain.cloud Cluster Local URL: http://myapp.4svg40kna19.svc.cluster.local Console URL: https://cloud.ibm.com/codeengine/project/us-south/01234567-abcd-abcd-abcd-abcdabcd1111/application/myapp/configuration Status Summary: Application deployed successfully Environment Variables: Type Name Value Literal TARGET Stranger Image: ibmcom/hello Resource Allocation: CPU: 1 Ephemeral Storage: 500Mi Memory: 4G Revisions: myapp-00002: Age: 58m Traffic: 100% Image: ibmcom/hello (pinned to f0dc03) Running Instances: 1 Runtime: Concurrency: 100 Maximum Scale: 10 Minimum Scale: 0 Timeout: 300 Conditions: Type OK Age Reason ConfigurationsReady true 57m Ready true 57m RoutesReady true 57m Events: Type Reason Age Source Messages Normal Created 4m17s service-controller Created Configuration "myapp" Normal Created 4m17s service-controller Created Route "myapp" Instances: Name Revision Running Status Restarts Age myapp-00002-deployment-8495f8ccb9-kmc57 myapp-00002 3/3 Running 0 16m{: screen}
-
Run the
application getcommand again and notice that the value forRunning instancesscaled to zero. When the application is finished running, the number of running instances automatically scales to zero, if the--min-scaleoption is set to0, which is the default value.ibmcloud ce application get -n myapp{: pre}
Example output
[...] URL: https://myapp.4svg40kna19.us-south.codeengine.appdomain.cloud Cluster Local URL: http://myapp.4svg40kna19.svc.cluster.local Console URL: https://cloud.ibm.com/codeengine/project/us-south/01234567-abcd-abcd-abcd-abcdabcd1111/application/myapp/configuration Status Summary: Application deployed successfully Environment Variables: Type Name Value Literal TARGET Stranger Image: ibmcom/hello Resource Allocation: CPU: 1 Ephemeral Storage: 500Mi Memory: 4G Revisions: myapp-huv70-2 Age: 13m Traffic: 100% Image: ibmcom/hello (pinned to 548d5c) Running Instances: 0 Runtime: Concurrency: 100 Maximum Scale: 10 Minimum Scale: 0 Timeout: 300 Conditions: Type OK Age Reason ConfigurationsReady true 13m Ready true 13m RoutesReady true 13m Events: Type Reason Age Source Messages Normal Created 4m17s service-controller Created Configuration "myapp" Normal Created 4m17s service-controller Created Route "myapp" Instances: Name Revision Running Status Restarts Age myapp-00002-deployment-8495f8ccb9-kmc57 myapp-00002 3/3 Running 0 16m{: screen}
Your application scaled down to zero.
-
Call the application again to scale from zero.
-
Run the
application getcommand again and notice that the value forRunning instancesscaled from zero and information about the instance is displayed.ibmcloud ce application get -n myapp{: pre}
Example output
Name: myapp [...] URL: https://myapp.4svg40kna19.us-south.codeengine.appdomain.cloud Cluster Local URL: http://myapp.4svg40kna19.svc.cluster.local Console URL: https://cloud.ibm.com/codeengine/project/us-south/01234567-abcd-abcd-abcd-abcdabcd1111/application/myapp/configuration Status Summary: Application deployed successfully Environment Variables: Type Name Value Literal TARGET Stranger Image: ibmcom/hello Resource Allocation: CPU: 1 Ephemeral Storage: 500Mi Memory: 4G Revisions: myapp-huv70-2: Age: 3h11m Traffic: 100% Image: ibmcom/hello (pinned to f0dc03) Running Instances: 1 Runtime: Concurrency: 100 Maximum Scale: 10 Minimum Scale: 0 Timeout: 300 Conditions: Type OK Age Reason ConfigurationsReady true 3h10m Ready true 3h10m RoutesReady true 3h10m Events: Type Reason Age Source Messages Normal Created 4m17s service-controller Created Configuration "myapp" Normal Created 4m17s service-controller Created Route "myapp" Instances: Name Revision Running Status Restarts Age myapp-00002-deployment-8495f8ccb9-kmc57 myapp-00002 3/3 Running 0 16m{: screen}
Your application scales back up.
For more information about scaling your app, see Configuring application scaling.
{: #nextsteps-deployapptut}
For more information, see Deploying applications.
Looking for more code examples? Check out the Samples for {{site.data.keyword.codeenginefull_notm}} GitHub repo{: external}. {: tip}