Skip to content

Commit 65abc59

Browse files
authored
Merge pull request #18456 from beyhan/add-understand-docker-application-lifecycle-tutorial
Add new tutorial about docker based CF apps
2 parents 0d8e541 + 0a72894 commit 65abc59

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
title: Understand the Cloud Foundry Application Lifecycle for Docker Applications
3+
description: Understand how Docker works with Cloud Foundry and how to push a Docker image
4+
auto_validation: true
5+
time: 15
6+
tags: [ tutorial>beginner, software-product>sap-btp--cloud-foundry-environment]
7+
primary_tag: software-product>sap-btp--cloud-foundry-environment
8+
author_name: Beyhan Veli
9+
author_profile: https://github.com/beyhan
10+
---
11+
12+
## Prerequisites
13+
- **Tutorials** [Understand the Cloud Foundry Application Lifecycle for Buildpack Applications](cp-cf-understand-application-lifecycle)
14+
- You have installed Docker.
15+
- You have [Docker Hub](https://hub.docker.com/) account.
16+
17+
18+
## Details
19+
### You will learn
20+
- The lifecycle of Docker based applications
21+
- How to push a Docker application
22+
- Considerations when re-pushing and re-staging Docker images
23+
24+
25+
---
26+
[ACCORDION-BEGIN [Step 1: ](Understand Docker based applications)]
27+
28+
When you push an application to Cloud Foundry (`cf push`), it uses a [buildpack](https://docs.cloudfoundry.org/buildpacks/) by default. There is, however, another option to use a Docker image instead of a buildpack with Cloud Foundry as well. You can see the full application lifecycle documented at [How Apps are Staged](https://docs.cloudfoundry.org/concepts/how-applications-are-staged.html), but for the sake of brevity, just know that the lifecycle is mostly the same when running either a Docker container or buildpack based application. There are some special considerations when it comes to Docker based applications:
29+
30+
1. When you `cf push` with a Docker image, the last step of the process is to schedule the app as a long running process, and at this point the Docker image is downloaded. That means that Cloud Foundry will require access to the container registry where the image is stored to start your application.
31+
32+
2. Access to the registry is also required when the application needs to be re-staged or restarted. If the registry is not accessible, these operations will fail. For this reason it is required to re-push your application if the credentials for a private container registry change.
33+
34+
3. Additionally, when you use a buildpack, your main concern is your application code. The buildpack takes care of the work of downloading dependencies, compiling code, and creating a droplet from which your application can run. However, when using a Docker image, you have to do all this yourself (set up the image, define dependencies, provide settings the image/app needs to start, etc.)
35+
36+
4. Related to the above, buildpacks are generally maintained and updated by the Cloud Foundry community, but this is not the case for referenced Docker base images. Here you will need to keep track of security vulnerabilities and update the base images. If a base image is not maintained any more you will need to maintain your own base images. Moral of the story: don't take on more than you absolutely have to, but be certain you trust the source of all your base images and buildpacks.
37+
38+
[VALIDATE_1]
39+
40+
[ACCORDION-END]
41+
42+
[ACCORDION-BEGIN [Step 2 ](Push a Docker based application)]
43+
44+
To see how Docker based applications work you will take the [cf-nodejs](https://github.com/SAP-archive/cf-sample-app-nodejs) application you deployed before and create a Docker image of it, which you will then deploy. To do this, the first thing you need to do is create a `Dockerfile` in the same directory as your application files. The path to the `Dockerfile` should be `<path-to-cf-sample-app-nodejs>/Dockerfile` Once the file is created, open the `Dockerfile` with your favorite editor, and add the following (lines beginning with `#` are comments explaining what the line below it does):
45+
46+
```
47+
# specify the node base image with your desired version node:<version>
48+
FROM node:18
49+
# create a directory within the image to house your application code
50+
WORKDIR /usr/src/app
51+
# copy package files
52+
COPY package*.json ./
53+
#install app dependencies
54+
RUN npm install
55+
#copy over the rest of the app files
56+
COPY . .
57+
#the start command to be run
58+
CMD [ "node", "server.js" ]
59+
```
60+
61+
The code above begins with the official Node image from Docker. We then proceed to add onto this base image, adding files, downloading dependencies, and providing the start command for the `cf-nodejs` app. Make sure to save the file once you have added the code above. Once that is done you then need to tell Docker to build your image. Run the following command (the `username` is your `Dockerhub` username and some commands may require you to enter your `Dockerhub` password):
62+
63+
```Bash
64+
cd <path-to-cf-sample-app-nodejs>
65+
docker build -t <username>/cf-nodejs-app .
66+
```
67+
68+
You should see output similar to the following:
69+
70+
!![Running docker build](docker-build.png)
71+
72+
Now that the image is built, you need to push it to a repository on `Dockerhub`. Running the following commands, a repository will be created for your user called `cf-nodejs-app` and your image will be uploaded to it:
73+
74+
```Bash
75+
docker login -u <docker-id>
76+
docker push <username>/cf-nodejs-app
77+
```
78+
79+
!![Pushing the Docker image](docker-push.png)
80+
81+
Finally, once the image has been uploaded, you can push the image to Cloud Foundry (you can replace the name `docker-nodejs` with whatever you want to name your application).
82+
83+
```Bash
84+
cf push docker-nodejs --docker-image <username>/cf-nodejs-app:latest --docker-username <username>
85+
```
86+
87+
> In this example `cf-nodejs-app:latest` is used, but in the next section you will see why using the 'latest' image may not always be what you want.
88+
89+
Finally, once the application has started you should be able to access the app through a browser and see the same output as when you ran the `cf-nodejs` app using a buildpack. You can also validate that your application is actually using the docker image by running:
90+
91+
```Bash
92+
cf app docker-nodejs
93+
```
94+
95+
You should then see the `docker image` field with the name your Docker image next to it, similar to the output below.
96+
97+
!![Link text e.g., Destination screen](docker-image.png)
98+
99+
After running through this example, you can see that using a Docker image requires you to specify and configure things that you didn't have to be concerned with when using a buildpack. A buildpack handles most of this in the background for you.
100+
101+
[VALIDATE_2]
102+
103+
[ACCORDION-END]
104+
105+
[ACCORDION-BEGIN [Step 3 ](Why Docker image tags are relevant for your application)]
106+
107+
There are some special considerations when it comes to re-pushing, re-staging and restarting Docker applications. These operations will trigger a Docker pull when Cloud Foundry starts the application and depending on your Docker image configuration following can happen:
108+
109+
- If you specify a specific `tag` for the image, Cloud Foundry will pull the latest image state for that `tag` whenever the app start is scheduled.
110+
- However, if you don't specify a `tag`, the `latest` tag will be used which could be very dangerous for productive apps.
111+
112+
> Always take care which image tags do you use for your productive apps and don't push image changes to those image tags because this will cause unexpected updates of your productive apps.
113+
114+
[DONE]
115+
[ACCORDION-END]
Loading
Loading
Loading

0 commit comments

Comments
 (0)