Deploy Python Application on SAP Cloud Platform using Docker Container
Docker is the containerization platform that packages your application and all its dependencies together in the form of containers, which make your application works seamlessly in any environment. Docker is an open-source technology that makes it easier to create, deploy, run applications by using containers.
Certainly, it’s a hot topic in cloud computing, and in this article get ready to build your first “Hello World” python application and run it in a container on SAP Cloud Platform.
Follow this 6 steps approach to run a docker container
- Create the python source code (helloworld.py)
- Create a Dockerfile file (Dockerfile)
- Build the docker image (hello-world-python)
- Run the image in a container
- Push the image to docker hub
- Push the docker image from docker hub to SAP Cloud Platform
helloworld.py ( Python Source Code )
from flask import Flask, request from flask_restful import Resource, Api app = Flask(__name__) api = Api(app) class Welcome (Resource): def get(self): return 'Hello World!' api.add_resource(Welcome, '/') if __name__ == '__main__': app.run('0.0.0.0','3333')
Dockerfile – set of instructions docker uses to build the image.
FROM python:3 ADD helloworld.py / RUN pip install flask RUN pip install flask_restful EXPOSE 3333 CMD [ "python", "./helloworld.py"]
Install Docker Desktop on Windows
Create your account on https://hub.docker.com/
Before proceeding further make sure Docker is installed on your system. Follow the instruction on this URL to install Docker on your windows machine.
After successful installation, Open the Docker for Desktop from Programs and you will see the docker icon on the right bottom side of your windows screen.
And in the docker desktop application you manage the settings/configuration, also manage local/remote repositories.
Now, if everything is set you can proceed further.
Create a folder and copy both the files helloworld.py and Dockerfile there. Open the Windows PowerShell and change the current working directory to your newly created directory.
Note: Make sure you run all the commands from that directory only.
Let’s Build And Run
To build the image, run
docker build -t hello-world-python .
To run the image, run
docker run -p 3333:3333 hello-world-python
Now, we can see the App is running & we can access the app via URL:
http://localhost:3333/ in any browser.
After successful testing, Let’s stop the instance.
To check container details on docker run :
docker container ls
To stop the instance run:
docker stop <CONTAINER ID>
Push Docker Image to Docker Hub
Step1: Create an account on Docker Hub: https://hub.docker.com/
Step2: Let’s check the Docker Hub account, go to the terminal. Run:
As the docker is running on your Desktop, it will automatically authenticate using existing credentials.
Step3: Tag your docker image. Run:
docker tag hello-world-python ravimittal/hello-world-python-image
Note: Here, ravimittal is my Docker Hub Username. You should use your username.
Step4: Let’s push our Docker image to Docker Hub. Run:
docker push ravimittal/hello-world-python-image
Step5: Go to https://hub.docker.com/ & Confirm your push.
Push Docker Image to SAP Cloud Platform
Now the image is ready in the docker hub, let’s push this docker image to SAP Cloud Platform.
Here I assume that you have your SAP Cloud Platform account ready and Cloud Foundry Command Line Interface (CLI) installed on your desktop.
Step1: Let’s login to your SAP Cloud Platform Cloud Foundry endpoint using CLI. Run:
From the console, you can validate the endpoint and other details.
Step2: Push the docker image to SAPcp Cloud Foundry.
cf push hello-world-cf-app --docker-image ravimittal/hello-world-python-image --docker-username ravimittal
It will ask your docker password, enter your password to proceed.
cf push <App Name> –docker-image <Docker Image Repository:TagName> –docker- username <docker username>
<App Name> – hello-world-cf-app
<Docker Image Repository:TagName> – ravimittal/hello-world-python-image
<docker username> – ravimittal
Step3: Now your image is deployed on SAPcp Cloud Foundry and running in a container. Get the URL fomr routes section.
Or login to your SAP Cloud Platform Cockpit and validate the same.
Step4: Open the application URL to validate that the application is up and running.
I hope you have all the knowledge and tools necessary to run your Python code in a docker container on SAP Cloud Platform. Try yourself, in case you face any issue or have questions, leave your comment below.
Thanks for reading!
Want to explore more with this Container. Check next Article
Can you suggest me if I want to run an algorithm written in python where my algo is reading csv file.. So in such case how I can deploy algo in HANA cloud Platform using docker.
There are two ways to provide files to the container.
In your scenario, as you need to deploy the container on SAP Cloud Platform, we need to provide the files at build time.
When you build the image, this file will be part of your image and can inspect the same in the Docker application.
All other steps remain the same, and your application will work fine.
Thanks for this blog.
It's well written, still I think the Docker solution is too cumbersome for many.
It would be great to have a Python trial as a service where you can just create an instance of like you do with the ABAP trial service!
Thanks, Kai for your inputs.
Yes, you are right it would be much easier that way. But I think as Python is not that heavy, can be made available anywhere with extensions and buildpacks, and developer can use it for development and to run applications almost anywhere. I think this might be the reason it is not available separately as a service.
Thanks for the blog post. So simple to get a dockerized app running in cloud foundry!
One question though - the Flask app is bound to port 3333. In the Dockerfile you expose 3333 and in the run command you map this as 3333 to the outside. But... when deployed on cloud foundry you access the app on port 80 and not on 3333 ?
Where/Why is the port redirected from 80 to 3333? What if I really want the app exposed on 3333?
Thanks, Jason, a very valid question.
Here we need to understand that the EXPOSE rule only as a hint to which ports will provide services. It is up to the operator of the container to specify further networking rules. It is just metadata that provides information to be used by another command or to inform choices made by the container operator.
Now come to the second part of your question that you want the app exposed only on 3333, which means instead of merely offering up a port, we can explicitly bind a port from container to host using the -p flag. But the problem is that there is no equivalent instruction allowed in the Dockerfile it is a runtime configuration only.
Does currently, on SCP Cloud Foundry, containers run inside a Diego cell, or as a "real" containers (POD) in the undelying platform (K8s)? So, Is Cloud Foundry on SCP based on kubernetes (is based on projects e.g. EIRINI/Quarks)?
I think it's important for me to understand, regarding the performances and overheads.
Thanks for your feedback!
Cloud Foundry uses the Diego system to manage app containers. Diego components assume app scheduling and management responsibility from the Cloud Controller.
SAP Cloud Platform provides four different PaaS environments Neo, ABAP, Cloud Foundry, and Kyma
Kyma environment is a fully managed Kubernetes runtime based on the open-source project "Kyma". So You should explore Kyma.
Hello Ravi Mittal ,
Is it mandatory to use docker to deploy python code on the SAP BTP?
Will it not work without the Docker container?
On your last step when pushing my "hello-world-cf-app" into HANA Trail on Cloud it failed. The error message is "Routes cannot be mapped to destinations in different spaces". What does that mean?
I can see the appl. in my HANA Trail on Cloud but the Application Routes says :No routes defined. Hence, I can't run the app.
How about cf push --random-route, I found this command from
Great article! I just finished reading it and found it very helpful. I especially appreciated the step-by-step guide on building an image using Docker. As someone who is new to Docker, this was really useful for me.
However, I did run into a problem when trying to push my app to BTP Cloud Foundry. After following the instructions, my app kept crashing with an "exec format error" message. After some research, I found that this was due to the fact that I was using an M1 MacBook, which uses the arm64 architecture by default.
To solve this issue, I had to use Docker buildx to build an amd64 architecture image, which allowed me to successfully push and run my app on Cloud Foundry. Just wanted to leave a note for others who may run into the same issue. Thanks for the great article!