Deploy Application using Docker Container on SAP Cloud Foundry with XSUAA Authentication : 2022
This blog is to demonstrate the deployment of an application to SAP Cloud Foundry with XSUAA authentication using docker. I was working on a project where we wanted to deploy an application using Docker Container on SAP CF with multiple services along with XSUAA authentication. When I searched for it, there were some basic blogs for docker deployment but it doesn’t solve my requirement. I have to go through multiple documents to complete the task. So I thought of sharing it to make your life easier.
So, before we start, I am assuming a few things, you can take them as initial requirements.
- Docker Desktop is Installed, up and running.
- SAP CF CLI installed
- You have App ready for deployment
What will we do?
- Create a XSUAA service in CF
- Build a docker image
- Push the docker image to docker hub
- Deploy docker image to SAP Cloud Foundry
Create a XSUAA Service in CF
Step 1: Log in to CF account and choose the space.
step 2: To create a XSUAA service you need to have a xs-security.json file. If you don’t have one, you can use my sample application.
You can find the following files
XSUAA service is created now.
Build a docker image
Step 1: Before we start, let’s check whether docker is up and running.
Step 2: If you check the same directory, you can find a Dockerfile. Docker image can be created using Dockerfile where we give all the instructions in that.
#Pull the base machine FROM node:latest #setting a work directory for our application WORKDIR /usr/src/app #create necessary folder and copy all the required files RUN mkdir -p ./resources COPY resources ./resources COPY app.js . COPY package.json . COPY xs-app.json . #Install all the required packages RUN npm install #Expose the port. This should be similiar to the application port. EXPOSE 8080 #Start command for the application. CMD ["npm","start"]
Now in cmd, run the following command
docker build -t <reponame>/<applicationname> .
In place of reponame, you have to give your docker hub repository username. and for applicationname, you can give your application name. If you have not created a docker hub account already, you can create it here for free. https://hub.docker.com/
Note 1: Application name should be in lowercase.
Note 2: Don’t miss the “.” at the end of the command. By using “.”, it will point directly to the Dockerfile in the directory.
Push the docker image to the docker hub
Step 1: Hope you have already created the account. Now login to your docker account in cmd.
Step 2: We have already created a docker image and it’s in your local. Now we can push it to the docker hub. Run the following command for that.
docker push <reponame>/<applicationname>
Replace the reponame and applicationname with your details.
Now your docker image is pushed to the docker hub. If you check in your docker hub account, you can find the docker image.
Deploy the docker image to SAP Cloud Foundry
To deploy the docker image with XSUAA services to SAP Cloud Foundry, you need a manifest.yml file.
--- applications: - name: nodeappfordocker docker: image: <repo>/<appname>:<tag> username: <username> services: - my-xsuaa
In the above, manifest.yml, application name should be a unique name in lower case. You can replace the repo and appname under docker. In the tag, you should mention the version of your docker image. In our case, it is latest.
Under services, we will specify our XSUAA service name.
Once the manifest.yml file is ready, you can run the following command.
After running the above command, manifest.yml will be applied and your docker image will be uploaded from docker hub to the SAP Cloud Foundry.
Note: Usually while using manifest.yml for deployment, we use path and buildpack in the manifest.yml. But if you’re deploying with docker, you can’t use path and buildpack.
Once the deployment is done, you can run the application and check.
Congratulation! You have successfully deployed the application using docker. Using the same approach, you can deploy the angular app and react app as well. I hope this blog helped you to deploy your application easily.