Deploying your application code from a local source
Let's deploy a simple hello world
app to see how Code Engine works. Follow the steps to create a project, prepare your code, and deploy your app.
Review Cloud Foundry and Code Engine terms before starting this tutorial.
Objectives
- Learn the similarities between Code Engine and Cloud Foundry.
- Learn the general process of deploying apps in Code Engine.
- Deploy an application from code on your local system with Code Engine.
Prerequisites
Before you can get started with Code Engine, you need to set up your account and install the CLI.
-
All Code Engine users are required to have a Pay-as-you-Go account.
-
While you can use Code Engine through the console, the examples in this documentation focus on the command line. Therefore, you must install the Code Engine CLI.
ibmcloud plugin install code-engine
For more information, see setting up the Code Engine CLI. For more information about the CLI, see Code Engine CLI reference.
All Code Engine users are required to have a Pay-as-you-Go account. Tutorials might incur costs. Use the Cost Estimator to generate a cost estimate based on your projected usage. For more information, see Code Engine pricing.
Log in to IBM Cloud
Follow these steps to log in to your IBM Cloud account and target a resource group.
-
Log in to the IBM Cloud CLI.
ibmcloud login
-
Target a resource group by running the following command. To get a list of your resource groups, run
ibmcloud resource groups
.ibmcloud target -g <resource_group>
Example output
Targeted resource group default
Creating a project
A Code Engine "project" is similar to a Cloud Foundry "space" in that it groups related workloads into a logical collection that is meaningful to the developer.
You can group workloads into different projects based on whatever criteria that makes sense to you, for example, company organization structure, dependencies between the workloads, or development versus test versus production environments.
You can then configure access to these projects, similar to configuring access to your Cloud Foundry orgs
. Keep in mind that workloads within a single project share a private
network and are isolated within the security boundary of the project. All workloads within a project can talk to each other freely without concern of being seen by workloads outside of the cluster. If workloads in different projects want to
communicate with each other, then the communication must either use the internet or an internal IBM private network. For more information, see Options for visibility for a Code Engine application.
Create a project in Code Engine called sample
.
ibmcloud ce project create --name sample
Example output
Creating project 'sample'...
ID for project 'sample' is 'abcdabcd-abcd-abcd-abcd-abcd12e3456f7'.
Waiting for project 'sample' to be active...
Now selecting project 'sample'.
OK
Notice that your project is also selected for context, so all subsequent application-related commands are within the scope of this new sample
project.
Creating a directory and source code
-
Create a directory on your local workstation that is named
myapp
and navigate into it. In this directory, save all the files that are required to build the image and to run your app.mkdir myapp && cd myapp
-
Create a file called
server.js
and copy the following source code into it.const http = require('http'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end( "Hello world\n" ); }).listen(8080);
This example uses Node.js. You can substitute code from any supported runtime.
Deploying your application
Push your code to Code Engine by using the application create
command. You must provide a name for your application and the location of the source code. The following example creates an application called myapp
that uses the buildpack
strategy and provides the location for the source code in the current directory (.
).
ibmcloud ce app create --name myapp --build-source . --strategy buildpacks
Example output
Creating application 'myapp'...
Packaging files to upload from source path '.'...
Submitting build run 'myapp-run-220999-210706331'...
Creating image 'private.us.icr.io/ce--6ef04-khxrbwa0lci/app-myapp:220418-0207-askql'...
Waiting for build run to complete...
Build run status: 'Running'
Build run completed successfully.
Run 'ibmcloud ce buildrun get -n myapp-run-220000-210706331' to check the build run status.
Waiting for application 'myapp' to become ready.
Configuration 'myapp' is waiting for a Revision to become ready.
Ingress has not yet been reconciled.
Waiting for load balancer to be ready.
Run 'ibmcloud ce application get -n myapp' to check the application status.
OK
https://myapp.abcdbwa0lci.us-south.codeengine.appdomain.cloud
And that's it. You now have an internet-facing application. The code in the application itself is the same as what is used for a Cloud Foundry application, it's just the Code Engine commands that are slightly different.
Let's take a deeper look at the previous app create
command. Notice that the output of the app create
command provides information about the progression of the build run before the
app is created and deployed.
- Code Engine receives a request to create an application from source code (instead of pulling directly from an image).
- Code Engine checks for an IAM service ID and API key that is associated with the selected project. This service ID must be authorized to read and write to IBM Cloud Container Registry. If no service ID exists, Code Engine creates one for you. Note that this service ID is used for subsequent Code Engine build requests that are run from the same project.
- This example builds code from a local source (
--build-source .
). The source code is packed into an archive file and uploaded to a managed namespace within the IBM Cloud Container Registry instance in your account. Note that you can target only IBM Cloud Container Registry for your local builds. For more information about IBM Container Registry, including information about quota limits and access, see Getting started with IBM Cloud Container Registry. - Code Engine builds your source code into an image. The source image is created in the same namespace as your source archive file.
- After the build completes, your application is deployed. You can access your application from the provided URL.
With Code Engine, you automatically get many of the same features as Cloud Foundry, such as autoscaling and blue-green roll-out of updates, but you'll also enjoy the benefits of newer features such as scaling down-to-zero, ensuring that you are not charged if your application is not active.
Want to learn more about your options for building your source code? See the application create
and the job create
commands.
Want to learn more about applications and jobs? See Working with apps in Code Engine and Working with jobs and jobruns.
Clean up
After you finish this tutorial, you can clean up the resources that you created with the following commands.
Delete your application
ibmcloud ce app delete --name myapp
When you delete your app, the associated build files are also deleted.
Lastly, delete the images that the build created from IBM Cloud Container Registry.
- Navigate to Registry in the IBM Cloud console.
- Find the archive and the image that are associated with your application by searching for your application name.
- Select the archive and image and delete.
Next steps
- Just starting your migration? Check out Getting started.
- Compare Cloud Foundry terminology with Code Engine.
- Deploying your application code from a local source (current page)
- Does your application use service bindings? Check out Migrating your service bindings.
- Learn about scaling and traffic management.
- Find Code Engine equivalents to Cloud Foundry commands.
- Still have questions? Try the Migrating Cloud Foundry applications to Code Engine FAQ.
Other information
- Find out about Code Engine pricing.
- Try other Code Engine tutorials.
- Explore other Code Engine topics.