IBM Cloud Docs
Getting started with IBM Cloud Code Engine

Getting started with IBM Cloud Code Engine

IBM Cloud® Code Engine is a fully managed, serverless platform that runs your containerized workloads, including web apps, micro-services, event-driven functions, or batch jobs. Code Engine even builds container images for you from your source code. All these workloads can seamlessly work together because they are all hosted within the same Kubernetes infrastructure. The Code Engine experience is designed so that you can focus on writing code and not on the infrastructure that is needed to host it. 

First, learn about some key terms for Code Engine and then get started with one of the following options.

What are Code Engine projects, applications, jobs, and functions?

Before you get started, become familiar with some key terms for Code Engine. Afterward, you can test your knowledge and take a quiz!

Table 1. Code Engine Terms
Term Description
Project A project is a grouping of Code Engine entities such as applications, jobs, and builds. Projects are used to manage resources and provide access to its entities.
Application An application, or app, runs your code to serve HTTP requests. An application has a URL for incoming requests. The number of running instances of an application are automatically scaled up or down (to zero) based on incoming workload.
Build A build, or image build, is a mechanism that you can use to create a container image from your source code. Code Engine supports building from a Dockerfile or Cloud Native Buildpacks.
Function A function is a stateless code snippet that performs tasks in response to an HTTP request.
Job A job runs one or more instances of your executable code in parallel. Unlike applications, which include an HTTP Server to handle incoming requests, jobs are designed to run one time and exit.

For more information about terms, see Code Engine terminology.

Not sure what to choose? See Planning for Code Engine.

Deploying your first Code Engine app

Create your first Code Engine app by using the icr.io/codeengine/helloworld image.

  1. Open the Code Engine console.
  2. Select Let's go.
  3. Select a project from the list of available projects. You can also create a new one. Note that you must have a selected project to deploy an app.
  4. Select Application.
  5. Enter a name for the application, for example, myapp. Use a name for your application that is unique within the project.
  6. Select to run a Container image and specify icr.io/codeengine/helloworld for the image reference. For this example, you do not need to modify the default values. For more information about the code that is used for this example, see helloworld.
  7. Click Create.
  8. After the application status changes to Ready, you can test the application. Click Test application and then click Send request in the Test application pane. To open the application in a web page, click Application URL.

Example output

Hello World from:
. ___  __  ____  ____
./ __)/  \(    \(  __)
( (__(  O )) D ( ) _)
.\___)\__/(____/(____)
.____  __ _   ___  __  __ _  ____
(  __)(  ( \ / __)(  )(  ( \(  __)
.) _) /    /( (_ \ )( /    / ) _)
(____)\_)__) \___/(__)\_)__)(____)

Some Env Vars:
--------------
CE_API_BASE_URL=https://api.us-south.codeengine.cloud.ibm.com
CE_APP=myapp
CE_DOMAIN=us-south.codeengine.appdomain.cloud
CE_PROJECT_ID=abcdefgh-abcd-abcd-abcd-1a2b3c4d5e6f
CE_REGION=us-south
CE_SUBDOMAIN=abcdabcdab
HOME=/root
HOSTNAME=myapp-00001-deployment-5b5895fdf7-abcd
K_REVISION=myapp-00001
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PORT=8080
PWD=/
SHLVL=1
z=Set env var 'SHOW' to see all variables

You deployed your first application to Code Engine and tested it out. Go to the Tutorial: Deploying applications or Working with apps in Code Engine to try out more options for applications.

Running your first Code Engine job

Create and run your first Code Engine job by using the icr.io/codeengine/helloworld image.

  1. Open the Code Engine console.
  2. Select Let's go.
  3. Select a project from the list of available projects. You can also create a new one. Note that you must have a selected project to create a job.
  4. Select Job.
  5. Enter a name for the job, for example, myjob. Use a name for your job that is unique within the project.
  6. Specify icr.io/codeengine/helloworld for the image reference. For this example, you do not need to modify the default values. For more information about the code that is used for this example, see helloworld.
  7. Click Create.
  8. From your job page, click Submit job to submit a job based on the current configuration.
  9. From the Submit job pane, accept all the default values, and click Submit job again to run your job.

When logging is enabled, the following example is displayed in the logs. To learn about running jobs with logging enabled, see Viewing logs.

Example output from logging instance

Hello World from:
. ___  __  ____  ____
./ __)/  \(    \(  __)
( (__(  O )) D ( ) _)
.\___)\__/(____/(____)
.____  __ _   ___  __  __ _  ____
(  __)(  ( \ / __)(  )(  ( \(  __)
.) _) /    /( (_ \ )( /    / ) _)
(____)\_)__) \___/(__)\_)__)(____)

Some Env Vars:
--------------
CE_DOMAIN=us-east.codeengine.appdomain.cloud
CE_JOB=myjob
CE_JOBRUN=myjob-jobrun-xgpmz
CE_SUBDOMAIN=abcdabcdab
HOME=/root
HOSTNAME=myjob-jobrun-abcde-0-0
JOB_INDEX=0
...
z=Set env var 'SHOW' to see all variables

You created and ran your job from the console. Go to the Tutorial: Running jobs or Running jobs in Code Engine to try out more options for jobs.

Running your first function

Create and run your first Code Engine function with sample function code.

  1. Open the Code Engine console.
  2. Select Let's go.
  3. Select a project from the list of available projects. You can also create a new one. Note that you must have a selected project to create a function.
  4. Select Function.
  5. Enter a name for the function. Use a name for your function that is unique within the project.
  6. Select Node.js 18
  7. Click Create. Your function is created with sample code. You can edit this code on the Function -> Configuration page.
  8. Click Test function and then click Send request in the Test function pane. To open the function in a web page, click Function URL.

Example output

{"args":{"__ce_headers":{"Accept-Encoding":"gzip, deflate, br","User-Agent":"got (https://github.com/sindresorhus/got)","X-Request-Id":"12340a7b-11c0-4de3-f16b-a6abc27f4146"},"__ce_method":"GET","__ce_path":"/"},"env":{"HOME":"/root","PATH":"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/lib/nodejs/bin","PWD":"/nodejsAction","SHLVL":"1","_":"/usr/local/lib/nodejs/bin/node","__OW_ALLOW_CONCURRENT":"true","container":"oci"}}

You deployed your first function to Code Engine and tested it out. Go to the Running a function from local source or Working with functions to try out more options for functions.

You can migrate your IBM Cloud Functions to Code Engine.

Building your first container image from source code

Create and run your first Code Engine build and then deploy the container image in an application.

Code Engine can automatically push images to a Container Registry namespace in your account. It can even create a namespace for you. To push images to a different Container Registry account or to a private Docker Hub account, see Adding access to a private container registry.

  1. Open the Code Engine console.
  2. Select Let's go.
  3. Select a project from the list of available projects. You can also create a new one. Note that you must have a selected project to deploy an app.
  4. Select Application.
  5. Enter a name for the application. Use a name for your application that is unique within the project.
  6. Select to run a Container image and specify icr.io/codeengine/helloworld for the image reference. For this example, you do not need to modify the default values. For more information about the code that is used for this example, see helloworld.
  7. Select Source code.
  8. Click Specify build details.
  9. Accept the default for each page, clicking Next and then Done.
  10. Click Create.

After your build run is submitted, the built container image is sent to Container Registry and then your application pulls the image and deploys for you. After the application status changes to Ready, you can try it out. Click Test application and then click Send request in the Test application pane. To open the application in a web page, click Application URL.

Example output

Hello World from:
     ___  __  ____  ____
    / __)/  \(    \(  __)
   ( (__(  O )) D ( ) _)
    \___)\__/(____/(____)
 ____  __ _   ___  __  __ _  ____
(  __)(  ( \ / __)(  )(  ( \(  __)
 ) _) /    /( (_ \ )( /    / ) _)
(____)\_)__) \___/(__)\_)__)(____)
Some Env Vars:
--------------
CE_API_BASE_URL=https://api.us-south.codeengine.cloud.ibm.com
CE_APP=myapp
CE_DOMAIN=us-south.codeengine.appdomain.cloud
CE_PROJECT_ID=abcdefgh-abcd-abcd-abcd-1a2b3c4d5e6f
CE_REGION=us-south
CE_SUBDOMAIN=abcdabcdab
HOME=/root
HOSTNAME=myapp-00001-deployment-6db6d89dc7-k6qc7
K_REVISION=myapp-00001
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PORT=8080
PWD=/
SHLVL=1

You submitted source code to Code Engine and created a container image that you then deployed in an application - all from one interface.

Go to Building a container image to explore and try out more options for builds.

Next steps for Code Engine

Learn more about performing these Code Engine tasks from the console or with the Code Engine CLI.

Looking for more code examples? Check out the Samples for IBM Cloud Code Engine GitHub repo.