Search This Blog

Thursday, 3 December 2015

IBM Containers running Spring Boot Applications with IBM Bluemix

There is now a new command line plugin for IBM containers on Bluemix so you can push and run docker images using CF CLI itself. The steps below show you how to set this up and I use a basic spring boot application as a docker image to test this out.

Steps

Take a note of the docker local host IP. In this example it was as follows, as I test my docker image on my laptop prior to pushing it to Bluemix.

-> docker is configured to use the default machine with IP 192.168.99.100

1. Install the latest CF command line, I used the following version.

pasapicella@pas-macbook-pro:~/bluemix_apps/CONTAINERS/ibm-containers/gs-rest-service/complete$ cf --version
cf version 6.14.0+2654a47-2015-11-18


https://github.com/cloudfoundry/cli

2. Install IBM Containers Cloud Foundry plug-in

pasapicella@pas-macbook-pro:~$ cf install-plugin https://static-ice.ng.bluemix.net/ibm-containers-mac

**Attention: Plugins are binaries written by potentially untrusted authors. Install and use plugins at your own risk.**

Do you want to install the plugin https://static-ice.ng.bluemix.net/ibm-containers-mac? (y or n)> y

Attempting to download binary file from internet address...
9314192 bytes downloaded...
Installing plugin /var/folders/rj/5r89y5nd6pd4c9hwkbvdp_1w0000gn/T/ibm-containers-mac...
OK
Plugin IBM-Containers v0.8.788 successfully installed.


Note: Default plugin directory as follows

$HOME/.cf/plugins


3. Login to IBM Containers

pasapicella@pas-macbook-pro:~/bluemix_apps/CONTAINERS$ cf ic login
Client certificates are being retrieved from IBM Containers...
Client certificates are being stored in /Users/pasapicella/.ice/certs/...
Client certificates are being stored in /Users/pasapicella/.ice/certs/containers-api.ng.bluemix.net/0bcbcada-bd11-4372-b416-955dff3078a1...
OK
Client certificates were retrieved.

Deleting old configuration file...
Checking local Docker configuration...
OK

Authenticating with registry at host name registry.ng.bluemix.net
OK
Your container was authenticated with the IBM Containers registry.
Your private Bluemix repository is URL: registry.ng.bluemix.net/apples

You can choose from two ways to use the Docker CLI with IBM Containers:

Option 1: This option allows you to use "cf ic" for managing containers on IBM Containers while still using the Docker CLI directly to manage your local Docker host.
    Use this Cloud Foundry IBM Containers plug-in without affecting the local Docker environment:

    Example Usage:
    cf ic ps
    cf ic images

Option 2: Use the Docker CLI directly. In this shell, override the local Docker environment to connect to IBM Containers by setting these variables. Copy and paste the following commands:
    Note: Only Docker commands followed by (Docker) are supported with this option.

     export DOCKER_HOST=tcp://containers-api.ng.bluemix.net:8443
     export DOCKER_CERT_PATH=/Users/pasapicella/.ice/certs/containers-api.ng.bluemix.net/0bcbcada-bd11-4372-b416-955dff3078a1
     export DOCKER_TLS_VERIFY=1

    Example Usage:
    docker ps
    docker images
4. View docker images

pasapicella@pas-macbook-pro:~/bluemix_apps/CONTAINERS$ cf ic images
REPOSITORY                                        TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
registry.ng.bluemix.net/ibm-mobilefirst-starter   latest              5996bb6e51a1        6 weeks ago         770.4 MB
registry.ng.bluemix.net/ibm-node-strong-pm        latest              ef21e9d1656c        8 weeks ago         528.7 MB
registry.ng.bluemix.net/ibmliberty                latest              2209a9732f35        8 weeks ago         492.8 MB
registry.ng.bluemix.net/ibmnode                   latest              8f962f6afc9a        8 weeks ago         429 MB
registry.ng.bluemix.net/apples/etherpad_bluemix   latest              131fd7a39dff        11 weeks ago        570 MB


5. Clone application to run as docker image

$ git clone https://github.com/spring-guides/gs-rest-service.git

6. Create a file called Dockerfile as follows in the "complete" directory

pasapicella@pas-macbook-pro:~/bluemix_apps/CONTAINERS/ibm-containers/gs-rest-service/complete$ cat Dockerfile
FROM java:8
VOLUME /tmp
ADD target/gs-rest-service-0.1.0.jar app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]


7. Package the demo

$ mvn package

8. Build docker image

pasapicella@pas-macbook-pro:~/bluemix_apps/CONTAINERS/ibm-containers/gs-rest-service/complete$ docker build -t gs-rest-service .
Sending build context to Docker daemon 13.44 MB
Step 1 : FROM java:8
8: Pulling from library/java
1565e86129b8: Pull complete
a604b236bcde: Pull complete
5822f840e16b: Pull complete
276ac25b516c: Pull complete
5d32526c1c0e: Pull complete
0d61f7a71c59: Pull complete
16952eac0a64: Pull complete
2fb3388c8597: Pull complete
ca603b247c8e: Pull complete
1785f2bc7c99: Pull complete
40e61a6ae215: Pull complete
32f541968fe6: Pull complete
Digest: sha256:52a1b487ed34f5a76f88a336a740cdd3e7b4486e264a3e69ece7b96e76d9f1dd
Status: Downloaded newer image for java:8
 ---> 32f541968fe6
Step 2 : VOLUME /tmp
 ---> Running in 030f739777ac
 ---> 22bf0f9356a1
Removing intermediate container 030f739777ac
Step 3 : ADD target/gs-rest-service-0.1.0.jar app.jar
 ---> ac590c46b73b
Removing intermediate container 9790c39eb1f7
Step 4 : RUN bash -c 'touch /app.jar'
 ---> Running in e9350ddebb75
 ---> 697d245c6afb
Removing intermediate container e9350ddebb75
Step 5 : ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar /app.jar
 ---> Running in 42fc22473930
 ---> df853abfea57
Removing intermediate container 42fc22473930
Successfully built df853abfea57


9. Run locally

pasapicella@pas-macbook-pro:~/bluemix_apps/CONTAINERS/ibm-containers/gs-rest-service/complete$ docker run --name gs-rest-service -p 80:8080 -d -t gs-rest-service
a392aa15da81fb4ca6c16a6307e0bd1c6b22f9a046228f1fc477d3fe12e15f16


10. Test as follows

pasapicella@pas-macbook-pro:~/bluemix_apps/CONTAINERS/ibm-containers$ curl http://192.168.99.100/greeting
{"id":1,"content":"Hello, World!"}


11. PUSH TO BLUEMIX AS follows

pasapicella@pas-macbook-pro:~/bluemix_apps/CONTAINERS/ibm-containers/gs-rest-service/complete$ docker tag gs-rest-service registry.ng.bluemix.net/apples/gs-rest-service
pasapicella@pas-macbook-pro:~/bluemix_apps/CONTAINERS/ibm-containers/gs-rest-service/complete$ docker push registry.ng.bluemix.net/apples/gs-rest-service
The push refers to a repository [registry.ng.bluemix.net/apples/gs-rest-service] (len: 1)
Sending image list
Pushing repository registry.ng.bluemix.net/apples/gs-rest-service (1 tags)
Image 5822f840e16b already pushed, skipping
Image 276ac25b516c already pushed, skipping
Image 5d32526c1c0e already pushed, skipping
Image a604b236bcde already pushed, skipping
Image 1565e86129b8 already pushed, skipping
Image 0d61f7a71c59 already pushed, skipping
Image 2fb3388c8597 already pushed, skipping
Image 16952eac0a64 already pushed, skipping
Image ca603b247c8e already pushed, skipping
Image 1785f2bc7c99 already pushed, skipping
Image 40e61a6ae215 already pushed, skipping
Image 32f541968fe6 already pushed, skipping
22bf0f9356a1: Image successfully pushed
ac590c46b73b: Image successfully pushed
697d245c6afb: Image successfully pushed
df853abfea57: Image successfully pushed
Pushing tag for rev [df853abfea57] on {https://registry.ng.bluemix.net/v1/repositories/apples/gs-rest-service/tags/latest}


12. List all allocated IP

pasapicella@pas-macbook-pro:~/bluemix_apps/CONTAINERS/ibm-containers/gs-rest-service/complete$ cf ic ip list
Number of allocated public IP addresses:  2

IpAddress        ContainerId
134.168.13.83
134.168.15.105


13. Create a container from the uploaded image

pasapicella@pas-macbook-pro:~/bluemix_apps/CONTAINERS/ibm-containers/gs-rest-service/complete$ cf ic run -p 8080 --memory 512 --name pas-sb-container registry.ng.bluemix.net/apples/gs-rest-service:latest
b1fe3159-0c19-4d54-b0f5-cdd938618deb


14. Assign IP to container

pasapicella@pas-macbook-pro:~/bluemix_apps/CONTAINERS/ibm-containers/gs-rest-service/complete$ cf ic ip bind 134.168.13.83 pas-sb-container
OK
The IP address was bound successfully.


15. Verify it's running

pasapicella@pas-macbook-pro:~/bluemix_apps/CONTAINERS/ibm-containers/gs-rest-service/complete$ cf ic ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                  PORTS                          NAMES
3794802b-b0c                  ""                  4 minutes ago       Running 3 minutes ago   134.168.13.83:8080->8080/tcp   pas-sb-container

16. Invoke as follows

$ curl http://134.168.13.83:8080/greeting


More Information

Plugin Reference ->

https://www.eu-gb.bluemix.net/docs/containers/container_cli_reference_cfic.html

Installing cf ci plugin ->

https://www.eu-gb.bluemix.net/docs/containers/doc/container_cli_cfic.html

No comments: