Deploy an ASP.NET Core Application in the IBM Cloud Code Engine
Turn your ASP.NET application into a serverless engine.
Join the DZone community and get the full member experience.
Join For FreeWhile there are many use cases to explore, in this blog we are going to explore how can you deploy a dot net core application from scratch into the IBM Cloud code engine. I would also suggest looking into this article for understanding when to use an application or a job.
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 the 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 web app using the below command. This will create a new application under the directory myWebApp
and flag --no-https
flag specifies not to enable HTTPS.
xxxxxxxxxx
dotnet new webApp -o myWebApp --no-https
In your terminal, run the following command:
xxxxxxxxxx
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.
xxxxxxxxxx
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.
xxxxxxxxxx
Cd myWebApp
Create a docker file:
xxxxxxxxxx
vi Dockerfile
xxxxxxxxxx
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.
xxxxxxxxxx
docker build -t mywebapp .
Note: You can choose any name for your app.
Running the build command displays the following message in the end.
xxxxxxxxxx
...Successfully tagged mywebapp:latest
The following command will run an app.
xxxxxxxxxx
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.
xxxxxxxxxx
docker stop /app
xxxxxxxxxx
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 them.
Create a Container Registry on IBM Cloud
Make sure you are logged into your IBM Cloud account by using:
xxxxxxxxxx
ibmcloud login
or
xxxxxxxxxx
ibmcloud login --sso
Log in to the Container Registry Service to store the Docker image that we created with Docker.
xxxxxxxxxx
ibmcloud cr login
Find your container registry namespace by running the following command.
xxxxxxxxxx
ibmcloud cr namespaces
If you don’t have any, create one by using the following command.
xxxxxxxxxx
ibmcloud cr namespace-add <name>
For Example:
xxxxxxxxxx
ibmcloud cr namespace-add codeengine
Identify your Container Registry by running the following command.
xxxxxxxxxx
ibmcloud cr info
Build and tag (-t
) the Docker image by running the command below, replacing REGISTRY
and NAMESPACE
with the appropriate values.
xxxxxxxxxx
docker build . -t <REGISTRY>/<NAMESPACE>/myapp:v1.0.0
For example:
xxxxxxxxxx
docker build . -t jp.icr.io/codeengine/myapp:v1.0.0
It will display the following message at the end.
xxxxxxxxxx
naming to jp.icr.io/codeengine/myapp:v1.0.0
Push the Docker image to your Container Registry on IBM Cloud.
xxxxxxxxxx
docker push <REGISTRY>/<NAMESPACE>/myapp:v1.0.0
For example:
xxxxxxxxxx
docker push jp.icr.io/codeengine/myapp:v1.0.0
Verify that the image was pushed successfully by running the following command.
xxxxxxxxxx
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.
Log in 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).
- From the Service ID page, select Access policies and then Assign access.
- From the Assign service ID additional access section,
- Select IAM services.
- Select Container Registry for the type of access.
- You can select based on how you need access to the registry, you can select read-only access in case you need to limit access.
- In this demo, I'll select all the access in the All resources since we are not really looking into IAM right now.
- Click Add once you select all checkboxes.
- Now click on Account Management under Assign service ID additional access section.
- Select IAM Identity Service.
- Again not going into detail about 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 description 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 of, let's start creating a code engine service.
Deploy Your Containerized Application
On 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 it's
codeengine
, you can select the one you created. - Repository (image name) ->
myapp
. - Tag ->
v1.0.0
. - Click done.
- Select create.
Conclusion
That’s all folks; if everything is done perfectly, your Dotnet application will run and you can check using the Open application URL option.
In case you face issues you can enable logging in to 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 the Tokyo region.
Opinions expressed by DZone contributors are their own.
Comments