create docker image with bluemix dev ops

41
© 2014 IBM Corporation Create Docker Image with Bluemix DevOps Joseph Chang Senior IT Specialist IBM Cloud Group

Upload: joseph-chang

Post on 14-Apr-2017

1.671 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Create Docker Image with Bluemix DevOps

Joseph Chang

Senior IT Specialist

IBM Cloud Group

Page 2: Create docker image with bluemix dev ops

© 2014 IBM Corporation2

Take me to BluemixClick Here

If you are new to Bluemix Docker Container, please start with the links:

https://www.ng.bluemix.net/docs/containers/container_index.htmlhttps://www.ng.bluemix.net/docs/containers/container_optional.html

Page 3: Create docker image with bluemix dev ops

© 2014 IBM Corporation3

Lifecycle of DevOps with Bluemix Docker

Create Dockerfile

Commitchange

BuildDocker Image

Publish ToDocker Registry

RunDocker Container

Edit ApplicationSource code

Web Editor Web Editor Web Git Auto Build

Bluemix Catalog

Manual Deploy

Create Dockerfile

Commitchange

BuildDocker Image

Publish ToDocker Registry

RunDocker Container

Page 4: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Edit Application Source Code

In order to get a sample web application with source code in JazzHub, I decide to “borrow” the sample from Bluemix Node.js (Cloud Foundry) Runtime. So….

4

Let’s start from create a .js runtime.

Create a Node.js runtime.

Page 5: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Edit Application Source Code

We no longer this runtime . We just need the source code of this sample to build new Docker Image. Let’s go to the Web IDE to see the source code.

5

Create the GIT project.

Page 6: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Edit Application Source Code

Now we get the sample application in Git.

6

Page 7: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Edit Application Source Code

To move out of CF, the app.js need to be modified.

7

Line 13: remove itLine 22: replaced with hardcode port number, url

Page 8: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Edit Application Source Code

8

Here is the app.js after modified..

Page 9: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Create Dockerfile

9

Right click here to get popup menu. Click on the New->File, name it as Dockerfile

Page 10: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Create Dockerfile

10

You should see the Dockerfile in the list.Let’s start edit the content.

Page 11: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Create Dockerfile

11

Here is the sample docker file for running Node.js on port 80 in the container.

Line 2: We user the ibmnode image as base imageLine 6- Line 7: Move the package.json to docker image then run npm installLine 8- Line 9: Copy app.js and all files in /public to imageLine 10-Line 11: expose port 80Line 14: start app.js

Page 12: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Modify Build & Deploy Configuration

12

That’s all the code need to be modified.

Now we need to modify the Build & Deploy Configuration for build & run Docker image. Click Build and Deploy Button on up right side.

Page 13: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Modify Build Configuration

13

Click on the up right icon of Build Stage. Then click Configure Stage.

Click here to continue.

Page 14: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Modify Build Configuration

14

Let’s change the Build type..

Select IBM Container Service as Build Type.

Page 15: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Modify Build Configuration

15

Fill in Organization, Space, and Image name

Here is the Dockimage build script. Normally you don’t need to modify it.

Uncheck this

The image name must be in Lower Case

Re-Select the space.

Page 16: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Modify Build Configuration

16

Let’s take look at the build script.

#!/bin/bash# The following colors have been defined to help with presentation of logs: green, red, label_color, no_color. log_and_echo "$LABEL" "Starting build script"

# The IBM Container Service CLI (ice), Git client (git), IDS Inventory CLI (ids-inv) and Python 2.7.3 (python) have been installed.# Based on the organization and space selected in the Job credentials are in place for both IBM Container Service and IBM Bluemix ###################### Run unit tests ######################log_and_echo "$LABEL" "No unit tests cases have been checked in"

####################################### Build Container via Dockerfile #######################################

# REGISTRY_URL=${CCS_REGISTRY_HOST}/${NAMESPACE}# FULL_REPOSITORY_NAME=${REGISTRY_URL}/${IMAGE_NAME}:${APPLICATION_VERSION}# If you wish to receive slack notifications, set SLACK_WEBHOOK_PATH as a property on the stage.

if [ -f Dockerfile ]; then log_and_echo "$LABEL" "Building ${FULL_REPOSITORY_NAME}" ${EXT_DIR}/utilities/sendMessage.sh -l info -m "New container build requested for ${FULL_REPOSITORY_NAME}" # build image BUILD_COMMAND="" if [ "${USE_CACHED_LAYERS}" == "true" ]; then BUILD_COMMAND="build --pull --tag ${FULL_REPOSITORY_NAME} ${WORKSPACE}" ice_retry ${BUILD_COMMAND} RESULT=$? else BUILD_COMMAND="build --no-cache --tag ${FULL_REPOSITORY_NAME} ${WORKSPACE}" ice_retry ${BUILD_COMMAND} RESULT=$? fi

Page 17: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Modify Build Configuration

17

Let’s take look at the build script. (Continue)

if [ $RESULT -ne 0 ]; then log_and_echo "$ERROR" "Error building image" ice info ice images ${EXT_DIR}/print_help.sh ${EXT_DIR}/utilities/sendMessage.sh -l bad -m "Container build of ${FULL_REPOSITORY_NAME} failed. $(get_error_info)" exit 1 else log_and_echo "$SUCCESSFUL" "Container build of ${FULL_REPOSITORY_NAME} was successful" ${EXT_DIR}/utilities/sendMessage.sh -l good -m "Container build of ${FULL_REPOSITORY_NAME} was successful" fi else log_and_echo "$ERROR" "Dockerfile not found in project" ${EXT_DIR}/utilities/sendMessage.sh -l bad -m "Failed to get Dockerfile. $(get_error_info)" exit 1fi

####################################################################################### Copy any artifacts that will be needed for deployment and testing to $WORKSPACE #######################################################################################echo "IMAGE_NAME=${FULL_REPOSITORY_NAME}" >> $ARCHIVE_DIR/build.properties

Page 18: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Modify Build Configuration

18

Click Save to Continue

Page 19: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Modify Deploy Configuration

19

Click here to modify the Configuration of Deploy Stage.

Page 20: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Modify Deploy Configuration

20

Select IBM Containers on Bluemix as Deployer Type

Page 21: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Modify Deploy Configuration

21

Select the Organization, Space….

Page 22: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Modify Deploy Configuration

22

Let’s take look at the deploy script.

#!/bin/bash# The following are some example deployment scripts. Use these as is or fork them and include your updates here:echo -e "${label_color}Starting deployment script${no_color}"

# To view/fork this script goto: https://github.com/Osthanes/deployscripts# git_retry will retry git calls to prevent pipeline failure on temporary github problems# the code can be found in git_util.sh at https://github.com/Osthanes/container_deployergit_retry clone https://github.com/Osthanes/deployscripts.git deployscripts

# You can deploy your Image as either a single Container or as a Container # Group. A Container Group deploys a number of containers to enhance# scalability or reliability. By default we will deploy as a single # container. To switch to a group deploy, comment out the line below# containing deploycontainer.sh and uncomment the line for deploygroup.sh# Deploy with containers:# Optional environment properties (can be set directly in this script, or defined as environment properties):# NAME Value Description# ============= ========= ==============# BIND_TO String Specify a Bluemix application name that whose bound services you wish to make available to the container. By default this is not set.# CONTAINER_SIZE String Specify container size: pico (64), nano (128), micro (256), tiny (512), small (1024), medium (2048),# large (4096), x-large (8192), 2x-large (16384).# Default is micro (256).# CONCURRENT_VERSIONS Number Number of versions of this container to leave active. # Default is 1#/bin/bash deployscripts/deploycontainer.sh

Page 23: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Modify Deploy Configuration

23

Let’s take look at the deploy script. (Continue)# Deploy Container Group:# Optional environment properties (can be set directly in this script, or defined as environment properties):# NAME Value Description# ============= ========= ==============# ROUTE_HOSTNAME String Specify the Hostname for the Cloud Foundry Route you wish to assign to this container group. By default this is not set.# ROUTE_DOMAIN String Specify domain name for the Cloud Foundry Route you wish to assign to this container group. By default this is not set.# BIND_TO String Specify a Bluemix application name that whose bound services you wish to make available to the container. By default this is not set.# DESIRED_INSTANCES: Number Specify the number of instances in the group. Default value is 1.# AUTO_RECOVERY: Boolean Set auto-recovery to true/false. Default value is false.# Default is false.# CONTAINER_SIZE String Specify container size: pico (64), nano (128), micro (256), tiny (512), small (1024), medium (2048),# large (4096), x-large (8192), 2x-large (16384).# Default is micro (256).# CONCURRENT_VERSIONS Number Number of versions of this group to leave active.# Default is 1# IF YOU WANT CONTAINER GROUPS .. uncomment the next line, and comment out the previous deployment line (/bin/bash deployscripts/deploygroup.sh)#/bin/bash deployscripts/deploygroup.shRESULT=$?# source the deploy property fileif [ -f "${DEPLOY_PROPERTY_FILE}" ]; then source "$DEPLOY_PROPERTY_FILE"fi########################## Environment DETAILS ########################### The environment has been setup.# The Cloud Foundry CLI (cf), IBM Container Service CLI (ice), Git client (git), IDS Inventory CLI (ids-inv) and Python 2.7.3 (python) have been installed.# Based on the organization and space selected in the Job credentials are in place for both IBM Container Service and IBM Bluemix# The following colors have been defined to help with presentation of logs: green, red, label_color, no_color.if [ $RESULT -ne 0 ]; then echo -e "${red}Executed failed or had warnings ${no_color}" ${EXT_DIR}/print_help.sh exit $RESULTfiecho -e "${green}Execution complete${no_label}"

If you want to deploy Container Group, you need to uncomment this line.

Page 24: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Modify Deploy Configuration

24

Fill in the Name of the instance then click Save.

Page 25: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Commit Change

25

Go back to EDIT CODE

page then click GIT respository

Let’s back to EDIT CODE perspective. EDIT CODE

Page 26: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Commit Change

26

1. Select all of the changes.

2. Give a comment.

3. Click Confirm .4. Push the

changes to build pipeline

Page 27: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Build Docker Image

27

Switch to EDIT CODE perspective to see the result. Build & Deploy

The docker image have been created.

Page 28: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Build Docker Image

28

Let’s have a look at what happens during the build. Started by user joseph Building remotely on jenkins-build-slave-b9d10a965120 (.*Build) in workspace /home/jenkins/workspace/e6059a87-3337-a67b-64ea-

4bf1b5df961f/7dc9942f-7e3b-47bb-9cdd-665e50e37d55 Cloning the remote Git repository Cloning repository https://hub.jazz.net/git/joseph/helloWebApp Fetching upstream changes from https://hub.jazz.net/git/joseph/helloWebApp using .gitcredentials to set credentials Checking out Revision 918917ba36eb1bff5eee958e86cb14d6c1eee850 (detached) [7dc9942f-7e3b-47bb-9cdd-665e50e37d55] $ /bin/bash /tmp/hudson1082149903417757598.sh + cat /etc/lsb-release /etc/os-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=14.04 DISTRIB_CODENAME=trusty DISTRIB_DESCRIPTION="Ubuntu 14.04.2 LTS" NAME="Ubuntu" VERSION="14.04.2 LTS, Trusty Tahr" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 14.04.2 LTS" VERSION_ID="14.04" HOME_URL="http://www.ubuntu.com/" SUPPORT_URL="http://help.ubuntu.com/" BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/" + source _script.sh

Page 29: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Build Docker Image

29

++ set +x git clone https://github.com/Osthanes/utilities.git utilities Cloning into 'utilities'... APPLICATION_VERSION: 6 Archiving to /home/jenkins/workspace/e6059a87-3337-a67b-64ea-4bf1b5df961f/7dc9942f-7e3b-47bb-9cdd-665e50e37d55/.cs_archive Installing IBM Container Service CLI Installing Python 2.7 Python 2.7.6 Successfully installed IBM Container Service CLI Installing Cloud Foundry CLI Successfully installed Cloud Foundry CLI Bluemix host is 'api.ng.bluemix.net' Bluemix target is 'prod' Date/Time : 2015-08-14 03:41:20.476922 Debug mode : False CCS host/url : https://containers-api.ng.bluemix.net/v3/containers Registry host : registry.ng.bluemix.net Bluemix api host/url : api.ng.bluemix.net Bluemix Org : [email protected] (1f6d3957-c6c6-4804-a8c2-5c7b5d382387) Bluemix Space : 0-sandbox (3fb3e792-dfb1-4fc2-83ab-62435218d59a) ICE CLI Version : 2.0 646 2015-07-20T18:51:33 CCS API Version : 3.0 1306 2015-08-11T15:41:15 Repository namespace : jjregistry Containers limit : Unlimited

Page 30: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Build Docker Image

30

Containers usage : 1 Containers running : 1 CPU limit (cores) : Unlimited CPU usage (cores) : 4 Memory limit (MB) : 2048 Memory usage (MB) : 256 Floating IPs limit : 2 Floating IPs allocated : 1 Floating IPs bound : 1

Image Id Created Image Name

97513e56aaa70b9daf1627734f18efcd7023 Aug 14 02:03:35 2015 registry.ng.bluemix.net/ibm-mobilefirst-starter:latest 97513e56aaa70b9daf1627734f18efcd7023 Aug 14 02:02:14 2015 registry.ng.bluemix.net/ibm-mobilefirst-starter:7.1.0 68e01b620447d7468f6b098ea0e6023257f6 Aug 6 03:31:53 2015 registry.ng.bluemix.net/jjregistry/demo-nodejs:10 777c0f64f1215d45272c9c301721e19689ee Aug 4 17:19:45 2015 registry.ng.bluemix.net/jjregistry/demo-nodejs:9 c0ace5c9ec7cd7f7c18a85fa841af99a79f9 Aug 1 16:31:19 2015 registry.ng.bluemix.net/jjregistry/demo-nodejs:8 de3ab2d47694b8482d1ca10d68e802d5ccd5 Aug 1 13:11:06 2015 registry.ng.bluemix.net/jjregistry/demo-nodejs:7 9a00bc0f-fbbf-4b48-9831-6271db8b8603 Aug 1 12:52:14 2015 registry.ng.bluemix.net/jjregistry/demo-nodejs:6 2209a9732f35a906491005f87c130bb73e26 Jul 23 15:08:49 2015 registry.ng.bluemix.net/ibmliberty:latest 8f962f6afc9a30b646b9347ecb7f458bf75b Jul 23 15:05:40 2015 registry.ng.bluemix.net/ibmnode:latest 17cd72f2667163c517253c0127425677d20d Jul 9 03:54:37 2015 registry.ng.bluemix.net/jjregistry/etherpad_bluemix:latest

Successfully accessed into IBM Container Service using credentials passed from IBM DevOps Services

Page 31: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Build Docker Image

31

Successfully accessed into IBM Containers Service Date/Time : 2015-08-14 03:41:26.812804 Debug mode : False CCS host/url : https://containers-api.ng.bluemix.net/v3/containers Registry host : registry.ng.bluemix.net Bluemix api host/url : api.ng.bluemix.net Bluemix Org : [email protected] (1f6d3957-c6c6-4804-a8c2-5c7b5d382387) Bluemix Space : 0-sandbox (3fb3e792-dfb1-4fc2-83ab-62435218d59a) ICE CLI Version : 2.0 646 2015-07-20T18:51:33 CCS API Version : 3.0 1306 2015-08-11T15:41:15 Repository namespace : jjregistry Containers limit : Unlimited Containers usage : 1 Containers running : 1 CPU limit (cores) : Unlimited CPU usage (cores) : 4 Memory limit (MB) : 2048 Memory usage (MB) : 256 Floating IPs limit : 2 Floating IPs allocated : 1 Floating IPs bound : 1 LOGMET setup failed with return code 2 Users namespace is jjregistry The desired image repository name will be registry.ng.bluemix.net/jjregistry/tle_nodejs:6

Page 32: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Build Docker Image

32

Initialization complete Number of images: 0 and Image limit: 5 The number of images are less than the image limit Init runtime of 0m 47s Starting build script No unit tests cases have been checked in Building registry.ng.bluemix.net/jjregistry/tle_nodejs:6 zipped tar size: 1221307 Posting 1221307 bytes... It may take a while... Step 0 : FROM registry.ng.bluemix.net/ibmnode ---> 8f962f6afc9a Step 1 : MAINTAINER Joseph Chang "[email protected]" ---> Running in de6d557e0124 ---> 3617945c3b59 Removing intermediate container de6d557e0124 Step 2 : ADD package.json /app/package.json ---> c4561d03bc9c Removing intermediate container 47dc78795a53 Step 3 : RUN cd /app && npm install ---> Running in 6c3d1d3313e6 npm WARN package.json [email protected] No README data [email protected] node_modules/cfenv

[email protected] node_modules/express

Page 33: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Build Docker Image

33

---> 260391a99b7c Removing intermediate container 6c3d1d3313e6 Step 4 : ADD app.js /app/app.js ---> e35785ccf97d Removing intermediate container 0dc0dbe21815 Step 5 : COPY public /app/public ---> fd9c9f0aa7f1 Removing intermediate container da63175d18d2 Step 6 : ENV WEB_PORT 80 ---> Running in 95c7d5e28cf8 ---> a99c74aedc13 Removing intermediate container 95c7d5e28cf8 Step 7 : EXPOSE 80 ---> Running in f8fe1b423788 ---> f68b44c65390 Removing intermediate container f8fe1b423788 Step 8 : CMD node /app/app.js ---> Running in 2990e0f0b613 ---> e83417fad8ff Removing intermediate container 2990e0f0b613 Successfully built e83417fad8ff The push refers to a repository [registry.ng.bluemix.net/jjregistry/tle_nodejs] (len: 1) Sending image list Pushing repository registry.ng.bluemix.net/jjregistry/tle_nodejs (1 tags)

Page 34: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Build Docker Image

34

Image 83e4dde6b9cf already pushed, skipping Image b670fb0c7ecd already pushed, skipping Image 29460ac93442 already pushed, skipping Image d2a0ecffe6fa already pushed, skipping Image 770bd0523a4b already pushed, skipping Image bfad8ba883e4 already pushed, skipping Image 5ed9589ea02f already pushed, skipping Image 416009f52495 already pushed, skipping Image 21676a84c1cc already pushed, skipping Image 5c1479ef231c already pushed, skipping Image 85e53b6dde50 already pushed, skipping Image 5caa6af8b7bc already pushed, skipping Image 4b9233194516 already pushed, skipping Image eda651c947e4 already pushed, skipping Image 8f962f6afc9a already pushed, skipping Container build of registry.ng.bluemix.net/jjregistry/tle_nodejs:6 was successful To send notifications, please set SLACK_WEBHOOK_PATH or HIP_CHAT_TOKEN in the environment Uploading artifacts ... UPLOAD SUCCESSFUL Total time: 1 seconds Finished: SUCCESS

Page 35: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Trouble shooting

35

If the build quickly finish, but the image is not created. And the “Deploy to” is no space…

Try ….Change the space, Uncheck the “Use cached layers”.

Page 36: Create docker image with bluemix dev ops

© 2014 IBM Corporation

View Docker Image in Bluemix Catagory

36

Login in to Bluemix and Switch to Catalog.

Surprise!!Your docker image is shown in the Catalog now.

Page 37: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Run Container

37

The container is successfully deployed.

Page 38: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Manually create container from image

38

By clicking the tle_nodejs icon in the catalog, you can launch this form to create a new container

Page 39: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Run Container

39

The application is running on Bluemix Docker Container now.

Page 40: Create docker image with bluemix dev ops

© 2014 IBM Corporation40

You can also build Docker Image from local then push it to Bluemix Catalog

Bluemix

Desktop

EditApplicationSource Code

Create Dockerfile

BuildDocker Image

RunDocker Container

vi app.js vi Dockerfile docker build docker run

Create Dockerfile

Commitchange

BuildDocker Image

Publish ToDocker Registry

RunDocker Container

EditApplicationSource Code

Web Editor Web Editor Web Git Auto Build

Bluemix Catalog

Manual Deploy

Create Dockerfile

Commitchange

BuildDocker Image

Publish ToDocker Registry

RunDocker Container

docker pushdocker pull

Page 41: Create docker image with bluemix dev ops

© 2014 IBM Corporation

Thank you

41

Take me to BluemixClick Here