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.
Steps:
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.
https://docs.docker.com/docker-for-windows/install/
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:
docker login
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.
https://account.hanatrial.ondemand.com/
Step1: Let's login to your SAP Cloud Platform Cloud Foundry endpoint using CLI. Run:
cf login
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!
Next Article
Want to explore more with this Container. Check next Article