Deploy an ASP.NET Core application in the IBM Cloud Code Engine
By: Deepak Rai
Tags: Ibm Cloud, Aspnetcore, Code Engine, Containers, Serverless

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. Because these workloads are all hosted within the same Kubernetes infrastructure, all of them can seamlessly work together. 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.

While there are many use cases to explore, on this blog we are going to explore how can you deploy a dot net core application from scratch into IBM Cloud code engine. I would also suggest looking into this article for understanding when to use application or a job https://cloud.ibm.com/docs/codeengine?topic=codeengine-plan-codeengine.

What you’ll learn in this tutorial

Upon completion of this tutorial, you will know how to:

Dockerize a simple ASP.NET Core app

Create your Code Engine project with an application

Deploy your ASP.NET Core app to Code Engine

Before you begin

You’ll need following installed on your machine.

IBM Cloud account

IBM Cloud CLI

Git

.NET Core SDK ( I have used 5.0.23)

Create and Run dotnet application locally

This step is to verify whether your app is running successfully locally before deployment. You can start by verifying the version of dotnet as follows in your terminal:

dotnet --version

Create a sample webapp using the below command. This will create a new application under the directory myWebApp and flag — no-https flag specifies not to enable HTTPS.

dotnet new webApp -o myWebApp --no-https

In your terminal, run the following command:

dotnet watch run

The dotnet watch run command will build and start the app, and then rebuild and restart the app whenever you make code changes. You can stop the app at any time by selecting ctrl+c

Wait for the app to display that it’s listening on http://localhost:5000 and for the browser to launch at that address.

Publish the ASP.NET Core app

Publish the app to get a self-contained DLL using the dotnet publish command.

dotnet publish -c Release

Running publish displays some messages with a successfully published DLL at the end of the process.

Dockerize the ASP.NET Core app

Once the application is ready, we can make an image of it and put it inside a container. We need a file that contains step-by-step instructions to deploy the image inside the container to run our application anywhere. This Dockerfile is a basic file and you may only require a few lines to get started with your own image.

Go to the app folder (here myWebApp) and create a Dockerfile to define the Docker image.

Cd myWebApp

Create a docker file

vi Dockerfile FROM mcr.microsoft.com/dotnet/aspnet:5.0

WORKDIR /app1

COPY ./bin/Release/net5.0/publish .

ENTRYPOINT ["dotnet", "myWebApp.dll"]

EXPOSE 8080

ENV ASPNETCORE_URLS=http://*:8080

Run the app on Docker (Optional)

You can test your dockerized app by following the steps below. This section is optional for this tutorial, though.

First, build an image.

docker build -t mywebapp .

Note: You can choose any name for your app.

Running the build command displays the following message in the end.

...Successfully tagged mywebapp:latest

The following command will run an app.

docker run -d -p 8080:80 --name app mywebapp

Navigate to http://localhost:8080 to access your app in a web browser.

Clean up with the following commands.

docker stop /app docker rm /app

These above commands stop and remove the Docker container of your app, respectively. You can use them to remove your container if you no longer need it.

Create a Container Registry on IBM Cloud

Make sure you are logged into your IBM Cloud account by using:

ibmcloud login

or

ibmcloud login --sso

Log in to the Container Registry Service to store the Docker image that we created with Docker.

ibmcloud cr login

Find your container registry namespace by running the following command.

ibmcloud cr namespaces

If you don’t have any, create one by using following command.

ibmcloud cr namespace-add <name>

For Example:

ibmcloud cr namespace-add codeengine

Identify your Container Registry by running the following command.

ibmcloud cr info

Build and tag ( -t ) the Docker image by running the command below, replacing REGISTRY and NAMESPACE with the appropriate values.

docker build . -t <REGISTRY>/<NAMESPACE>/myapp:v1.0.0

For example :

docker build . -t jp.icr.io/codeengine/myapp:v1.0.0

It will display the following message in the end.

naming to jp.icr.io/codeengine/myapp:v1.0.0

Push the Docker image to your Container Registry on IBM Cloud.

docker push <REGISTRY>/<NAMESPACE>/myapp:v1.0.0

For example:

docker push jp.icr.io/codeengine/myapp:v1.0.0

Verify that the image was pushed successfully by running the following command.

ibmcloud cr image-list

You set up a namespace in the IBM Cloud Container Registry and pushed a Docker image to your namespace.

Authorizing access to Container Registry from the console

Now before we jump into creating a Code Engine application, we should ensure it has all the right access to the container registry

In order to pull or push images from or to IBM Cloud Container Registry, you must create a service ID, create an access policy for the service ID, and then create an API key to store the credentials.

Login to IBM Cloud in the browser using https://cloud.ibm.com .

In the IBM cloud console go to Manage->Access (IAM)

Select Service IDs .

. select Create , enter a name and description, and click Create (name and description can be anything) .

, enter a name and description, and click . From the Service ID page, select Access policies and then Assign access.

From the Assign service ID additional access section,

section, Select IAM services .

. Select Container Registry for type of access.

for type of access. You can select based on how you need access to the registry , you can select read only access incase you need to limit access

In this demo, ill select all the access in the All resources since we are not really looking into IAM right now

Click Add once you select all check boxes

Now click on Account Management under Assign service ID additional access section

section Select IAM Identity Service

Again not going into detail of it here, will select all resources and check all boxes

Click Add

Click Assign next

From the Service ID page, select API Keys , click create

Enter name and descriptiona and click Create (name and description can be anything) .

. Copy the API key or click download to save it.

Note : You won’t be able to see this API key again, so be sure to record it in a safe place.

Now the access is taken care , lets start creating a code engine service

Deploy your containerized application

In your IBM Cloud page, click on Catalog

You can search for code engine , alternative you can select services-> containers->Code Engine

Select Projects

Click Create, Select Location in my demo as you have seen I am using Tokyo

Enter any name , Select resource group ( I have used my default resource group)

You can enter any tags if needed

Click Create

Once the project is created ,From the project page, click Registry access.

Click Create.

Enter a name for your registry access.

Enter a server name for your registry access. For Container Registry, the server name is <region>.icr.io. For example, jp.icr.io

Enter a name. For IBM Cloud Container Registry, it is iamapikey

Enter the password. For IBM Cloud Container Registry, the password is your API key.

You can ignore the email

Click Add

From the project page , click on Applications

Click create

Enter name

Under Choose the code to run

Select Container image

Click Configure Image

On the Registry server, Select IBM Registry Tokyo

Registry access , select the registry access name we created previously

Namespace -> in our case its codeengine, you can select the one you created

Repository (image name) -> myapp

Tag-> v1.0.0

Click done

Select create.

That’s all folks, if everything is done perfectly your dotnet application will run and you can check using the Open application URL option

Incase you face issues you can enable logging in the dashboard and discover the issues. Click on enabling logging will show the logs, if you already have a log Analysis service running, else you need to create one in Tokyo region.