Skip to Content
Technical Articles

Getting started on SAP Cloud Platform with a docker image

Introduction

One of the requirements customers have had is to be able to migrate any existing business application consuming one or more backing services (database/messaging etc) to cloud. One way to achieve this is to run it on a VM or create a container image and deploy on a cloud environment where you could run the container.  Containers are a better choice when your biggest priority is maximising the number of applications running on a minimal number of servers and your applications do not really need to access the OS level resources or functionalities. SAP Cloud Platform Cloud Foundry environment which has Diego (container-management system for Cloud Foundry) enabled by default, offers this possibility of deploying Docker images as Cloud Foundry applications.

Ensure the docker image is be compatible with the memory and storage quota requirements.  For instance, the total size of the Docker image file system layers must not exceed the disk quota for the app.

Official Documentation about running a docker image on Cloud Foundry is available: https://docs.cloudfoundry.org/devguide/deploy-apps/push-docker.html

This blog documents the steps involved in running a dockerized application that has dependencies on certain on-premise core services like database or messaging service on SAP Cloud Platform. Further on how to consume the core services offered on SAP Cloud Platform. 

Assumption

The primary assumption is that the legacy application is modular and all the necessary backing service constructs/parameters (viz., database credentials, URL, port etc.,) are not hard-coded in the application. Instead,  these parameters are made available to the application via environment variables. Example: If the application has to connect to MongoDB, the necessary mongo DB URI, credentials are set in the environment variables at runtime than being hard coded in the application properties file.

Scenario

Consider a Spring Boot Java web application that uses MongoDB database to be migrated to the SAP Cloud Platform Cloud Foundry environment with just the docker image of the Spring Boot application. The end goal is to run the same web application with MongoDB service consumed from SAP Cloud Platform.

Technology Stack

  1. Spring Boot Java Web Application
  2. MongoDB Database
  3. Docker
  4. SAP Cloud Platform Cloud Foundry

Pre-requisites:

You have:

  1. Created an account in  the SAP Cloud Platform Cloud Foundry environment.
  2. Downloaded Cloud Foundry command line interface (CLI). <https://tools.hana.ondemand.com/#cloud>
  3. Configured the CLI to the SAP Cloud Platform endpoint. For more information about configuring the endpoints, refer documentation.
  4. Pushed the docker image of your application to a docker registry. (Docker Hub is used in this blog as shown in the screenshot below). For this scenario, the docker image of the Spring Boot application was pushed to Docker Hub.

  1. Establish connectivity to MongoDB. This is achieved in the Spring Boot Java application using the connection parameters and the database name required to create the required Mongo database collection for the operations. The parameter values are obtained from the environment variables “mango.uri” and “mongo.dbname”. The code snippet below shows one way of accessing the values in the application:
@Configuration
public class AppConfig {

	@Value("${mongo.uri}")
	private String URI;
	
	@Value("${mongo.dbname}")
	private String dbname;

	public @Bean MongoClient mongoClient() throws UnknownHostException {
		MongoClientURI MONGO_URI = new MongoClientURI(URI);
		return new MongoClient(MONGO_URI);
	}

	public @Bean MongoTemplate mongoTemplate() throws UnknownHostException {
		return new MongoTemplate(mongoClient(),dbname);
	}
}

 

Step 1:

Push the docker image via “cf push” CLI command. Use the no-start flag since the application would fail to start without the required MongoDB services bound.

Syntax:

cf push <App Name> --docker-image <Docker Image Repository:TagName> --docker- username <docker username> --no-start

Enter the  docker password  to continue with the creation of a new application on SAP Cloud Platform with the requested state “Stopped”.

Step 2:

Use the sub-steps below to bind a mongodb service to the above application and obtain the database connectivity parameters.

  1. In your SAP Cloud Platform Cockpit, choose the Space.
  2. From the navigation menu, choose Services > Service Marketplace.
  3. Choose the MongoDB service.

  1. From the navigation menu, choose Instances.
  2. Choose New Instance.
  3. The instance creation wizard appears.
  4. Choose a service plan based on your subscription.

  1. Proceed with the wizard.
  2. Skip the step to provide parameters.
  3. Assign the application created in step 1 to this service instance.
  4. Choose “Next”.
  5. Provide a service instance name.
  6. Choose Finish.

A new service instance is now created, click on the service instance.

The parameters of the service instance are displayed. For now, we need the “uri” and “dbname” values for the application. The application uses these details to connect to the MongoDB service instance running on SAP Cloud Platform.

 

Step 3:

Use the values of URI and DB name obtained in step 3 and set the env variables for our application deployed in step 1.

Syntax:

cf set-env <app name> mongo.uri <mongo_uri>
cf set-env <app name> mongo.dbname <mongo_dbname>​

 

Step 4:

Start the web application.

Syntax:

cf start <app name>

Result

The application with the status “running” appears with mongo db being accessed from the platform:

 

 

Summary

Any application (backed by supported CF buildpack ) can be easily migrated to run on SAP Cloud Platform Cloud Foundry environment with just the docker image while consuming the platform services without any application code changes.

Best,

Suhas Narasimhan

 

Reference & useful links:

  1. https://blogs.sap.com/2017/11/04/deploy-a-container-to-sap-cloud-platform-cf-using-docker-hub-and-github/
  2. https://blogs.sap.com/2017/09/08/push-docker-image-to-sap-cloud-platform-cloud-foundry-landscape/
  3. https://docs.cloudfoundry.org/adminguide/docker.html
  4. https://docs.cloudfoundry.org/devguide/deploy-apps/push-docker.html
1 Comment
You must be Logged on to comment or reply to a post.
  • Hi, it is great sharing. I was wondering have you able tried to connect two docker containers so they can talk as an internal application using SAP Cloud Foundry? I have tried to use C2C network but later found this feature isn’t available in the SAP CF.

    Would appreciate your insight. Thanks