-
Notifications
You must be signed in to change notification settings - Fork 806
Add new tutorial about docker based CF apps #18456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jmmargo
merged 2 commits into
sap-tutorials:master
from
beyhan:add-understand-docker-application-lifecycle-tutorial
May 25, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
...d-docker-application-lifecycle/cp-cf-understand-docker-application-lifecycle.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
--- | ||
title: Understand the Cloud Foundry Application Lifecycle for Docker Applications | ||
description: Understand how Docker works with Cloud Foundry and how to push a Docker image | ||
auto_validation: true | ||
time: 15 | ||
tags: [ tutorial>beginner, software-product>sap-btp--cloud-foundry-environment] | ||
primary_tag: software-product>sap-btp--cloud-foundry-environment | ||
author_name: Beyhan Veli | ||
author_profile: https://github.com/beyhan | ||
--- | ||
|
||
## Prerequisites | ||
- **Tutorials** [Understand the Cloud Foundry Application Lifecycle for Buildpack Applications](cp-cf-understand-application-lifecycle) | ||
- You have installed Docker. | ||
- You have [Docker Hub](https://hub.docker.com/) account. | ||
|
||
|
||
## Details | ||
### You will learn | ||
- The lifecycle of Docker based applications | ||
- How to push a Docker application | ||
- Considerations when re-pushing and re-staging Docker images | ||
|
||
|
||
--- | ||
[ACCORDION-BEGIN [Step 1: ](Understand Docker based applications)] | ||
|
||
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: | ||
|
||
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. | ||
|
||
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. | ||
|
||
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.) | ||
|
||
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. | ||
|
||
[VALIDATE_1] | ||
|
||
[ACCORDION-END] | ||
|
||
[ACCORDION-BEGIN [Step 2 ](Push a Docker based application)] | ||
|
||
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): | ||
|
||
``` | ||
# specify the node base image with your desired version node:<version> | ||
FROM node:18 | ||
# create a directory within the image to house your application code | ||
WORKDIR /usr/src/app | ||
# copy package files | ||
COPY package*.json ./ | ||
#install app dependencies | ||
RUN npm install | ||
#copy over the rest of the app files | ||
COPY . . | ||
#the start command to be run | ||
CMD [ "node", "server.js" ] | ||
``` | ||
|
||
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): | ||
|
||
```Bash | ||
cd <path-to-cf-sample-app-nodejs> | ||
docker build -t <username>/cf-nodejs-app . | ||
``` | ||
|
||
You should see output similar to the following: | ||
|
||
! | ||
|
||
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: | ||
|
||
```Bash | ||
docker login -u <docker-id> | ||
docker push <username>/cf-nodejs-app | ||
``` | ||
|
||
! | ||
|
||
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). | ||
|
||
```Bash | ||
cf push docker-nodejs --docker-image <username>/cf-nodejs-app:latest --docker-username <username> | ||
``` | ||
|
||
> 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. | ||
|
||
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: | ||
|
||
```Bash | ||
cf app docker-nodejs | ||
``` | ||
|
||
You should then see the `docker image` field with the name your Docker image next to it, similar to the output below. | ||
|
||
! | ||
|
||
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. | ||
|
||
[VALIDATE_2] | ||
|
||
[ACCORDION-END] | ||
|
||
[ACCORDION-BEGIN [Step 3 ](Why Docker image tags are relevant for your application)] | ||
|
||
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: | ||
|
||
- 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. | ||
- However, if you don't specify a `tag`, the `latest` tag will be used which could be very dangerous for productive apps. | ||
|
||
> 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. | ||
|
||
[DONE] | ||
[ACCORDION-END] |
Binary file added
BIN
+453 KB
tutorials/cp-cf-understand-docker-application-lifecycle/docker-build.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+165 KB
tutorials/cp-cf-understand-docker-application-lifecycle/docker-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+70.3 KB
tutorials/cp-cf-understand-docker-application-lifecycle/docker-push.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docker is spelled with an upper case D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done