Tuesday, January 06, 2015

Building docker container for GlassFish 4.1 via phusion/baseimage on CentOS 7

Building  docker container for  GlassFish 4.1 via image phusion/baseimage  allows to execute several scripts been placed in folder /etc/my_init.d. In particular,  not only run.sh coming from https://registry.hub.docker.com/u/bonelli/glassfish-4.1/ ,  but also script database.sh  starting up Derby Database have been placed in mentioned folder, what actually does completely functional load  GlassFish 4.1 docker container.  The core issue in ([1]) is attempt to extend JAVA:8 , what causes a problem when starting several daemons during docker container load, needed by GlassFish vs TomCat ( for instance ).

The problem here is that  phusion/baseimage per  https://github.com/phusion/baseimage-docker  should provide ssh access to container , however it doesn't.
On 01/21/2015 I had completely rewrite this blog post, reasons which brought 
to this step clearly come up from comparison of two `docker logs container-id`
as of 01/08/2015 and as of 01/21/2015. I kept original version untouched at
bderzhavets.wordpress.com.

*******************************************************************************
To   bring sshd back to life  create in building folder script  01_sshd_start.sh
*******************************************************************************
#!/bin/bash


if [[ ! -e /etc/ssh/ssh_host_rsa_key ]]; then
    echo "No SSH host key available. Generating one..."
    export LC_ALL=C
    export DEBIAN_FRONTEND=noninteractive
    dpkg-reconfigure openssh-server
    echo "SSH KEYS regenerated by Boris just in case !"
fi
/usr/sbin/sshd > log &
echo "SSHD started !"

and insert in Dockerfile:-

ADD 01_sshd_start.sh /etc/my_init.d/ 

 *******************************************************************
 Then what ? Finally it appears to be a case (01/21/2015):-
*******************************************************************

CONTAINER ID        IMAGE                               COMMAND             CREATED             STATUS              PORTS               NAMES

4ef00f2fa5b9        dbahack/docker-glassfish41:latest   "/sbin/my_init"     36 seconds ago      Up 34 seconds                           nova-246d094b-bcd1-49c3-a490-0f74a7609d9a

1ee743c3cf3c        dba57/docker-glassfish41:latest     "/sbin/my_init"     18 minutes ago      Up 18 minutes                           nova-0c8b4b9d-14e1-43b5-8a55-dff8aa50fca1  

[root@junoDocker01 docker-glassfish41(keystone_admin)]# docker logs 4ef00f2fa5b9

*** Running /etc/my_init.d/00_regen_ssh_host_keys.sh...
*** Running /etc/my_init.d/01_start-sshd.sh...
No SSH host key available. Generating one...
Creating SSH2 RSA key; this may take some time ...
Creating SSH2 DSA key; this may take some time ...
Creating SSH2 ECDSA key; this may take some time ...
Creating SSH2 ED25519 key; this may take some time ...
invoke-rc.d: policy-rc.d denied execution of restart.
SSH KEYS regenarated by Boris just in case !
SSHD started !






*********************************************************************************
 Following bellow is Dockerfile been used to build image for GlassFish 4.1 docker container extending  phusion/baseimage and starting three daemons
at a time
**********************************************************************************


FROM phusion/baseimage

MAINTAINER Boris Derzhavets

RUN apt-get update
RUN echo 'root:root' |chpasswd
RUN sed -ri 's/^PermitRootLogin\s+.*/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config
RUN apt-get update && apt-get install -y wget
RUN wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u25-b17/jdk-8u25-linux-x64.tar.gz
RUN cp  jdk-8u25-linux-x64.tar.gz /opt
RUN cd /opt; tar -zxvf jdk-8u25-linux-x64.tar.gz
ENV PATH /opt/jdk1.8.0_25/bin:$PATH


RUN apt-get update && \
    apt-get install -y wget unzip pwgen expect net-tools vim && \
    wget http://download.java.net/glassfish/4.1/release/glassfish-4.1.zip && \
    unzip glassfish-4.1.zip -d /opt && \
    rm glassfish-4.1.zip && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

ENV PATH /opt/glassfish4/bin:$PATH

ADD 01_sshd_start.sh  /etc/my_init.d
ADD run.sh /etc/my_init.d/
ADD database.sh  /etc/my_init.d/
ADD change_admin_password.sh /change_admin_password.sh
ADD change_admin_password_func.sh /change_admin_password_func.sh
ADD enable_secure_admin.sh /enable_secure_admin.sh
RUN chmod +x /*.sh /etc/my_init.d/*.sh

# 4848 (administration), 8080 (HTTP listener), 8181 (HTTPS listener), 9009 (JPDA debug port)
EXPOSE 22  4848 8080 8181 9009

CMD ["/sbin/my_init"]



[root@junolxc boris-glassfish41]# cat database.sh
#!/bin/bash

set -e
asadmin start-database

*********************
Build image
*********************

[root@junolxc docker-glassfish41]# ls -l
total 44
-rw-r--r--. 1 root root   217 Jan  7 00:27 change_admin_password_func.sh
-rw-r--r--. 1 root root   833 Jan  7 00:27 change_admin_password.sh
-rw-r--r--. 1 root root   473 Jan  7 00:27 circle.yml
-rw-r--r--. 1 root root    44 Jan  7 00:27 database.sh
-rw-r--r--. 1 root root  1287 Jan  7 00:27 Dockerfile
-rw-r--r--. 1 root root   167 Jan  7 00:27 enable_secure_admin.sh
-rw-r--r--. 1 root root 11323 Jan  7 00:27 LICENSE
-rw-r--r--. 1 root root  2123 Jan  7 00:27 README.md
-rw-r--r--. 1 root root   354 Jan  7 00:27 run.sh

[root@junolxc docker-glassfish41]# docker build -t boris/docker-glassfish41 .



**********************
Start container
**********************
# docker run -d -p 49160:22 -p 49161:4848  -p 49162:8080 -p 49163:8181 boris/docker-glassfish41


*********************************************************************
Managing docker container ( keeping status Derby tables OK )
*********************************************************************
[root@junolxc ~]# docker ps -a
CONTAINER ID        IMAGE                             COMMAND             CREATED             STATUS              PORTS                                                                                                        NAMES
353f49aa26aa        boris/docker-glassfish41:latest   "/sbin/my_init"     5 minutes ago       Up 5 minutes        9009/tcp, 0.0.0.0:49160->22/tcp, 0.0.0.0:49161->4848/tcp, 0.0.0.0:49162->8080/tcp, 0.0.0.0:49163->8181/tcp   high_mcclintock    

[root@junolxc ~]# docker stop 353f49aa26aa
353f49aa26aa

[root@junolxc ~]# docker ps -a
CONTAINER ID        IMAGE                             COMMAND             CREATED             STATUS                     PORTS               NAMES
353f49aa26aa        boris/docker-glassfish41:latest   "/sbin/my_init"     5 minutes ago       Exited (2) 2 seconds ago                       high_mcclintock    

[root@junolxc ~]# docker start  353f49aa26aa
353f49aa26aa

[root@junolxc ~]# docker ps -a
CONTAINER ID        IMAGE                             COMMAND             CREATED             STATUS              PORTS                                                                                                        NAMES
353f49aa26aa        boris/docker-glassfish41:latest   "/sbin/my_init"     6 minutes ago       Up 10 seconds       9009/tcp, 0.0.0.0:49160->22/tcp, 0.0.0.0:49161->4848/tcp, 0.0.0.0:49162->8080/tcp, 0.0.0.0:49163->8181/tcp   high_mcclintock    

[root@junolxc ~]# docker ps -a
CONTAINER ID        IMAGE                             COMMAND             CREATED             STATUS              PORTS                                                                                                        NAMES
353f49aa26aa        boris/docker-glassfish41:latest   "/sbin/my_init"     6 minutes ago       Up 20 seconds       9009/tcp, 0.0.0.0:49160->22/tcp, 0.0.0.0:49161->4848/tcp, 0.0.0.0:49162->8080/tcp, 0.0.0.0:49163->8181/tcp   high_mcclintock    

**************
Log files
**************
[root@junolxc ~]# docker ps -a
CONTAINER ID        IMAGE                             COMMAND             CREATED             STATUS              PORTS                                                                                                        NAMES
bb310dbf8553        boris/docker-glassfish41:latest   "/sbin/my_init"     21 seconds ago      Up 20 seconds       9009/tcp, 0.0.0.0:49160->22/tcp, 0.0.0.0:49161->4848/tcp, 0.0.0.0:49162->8080/tcp, 0.0.0.0:49163->8181/tcp   distracted_davinci  

[root@junolxc ~]# docker logs bb310dbf8553
*** Running /etc/my_init.d/00_regen_ssh_host_keys.sh...
No SSH host key available. Generating one...
Creating SSH2 RSA key; this may take some time ...
Creating SSH2 DSA key; this may take some time ...
Creating SSH2 ECDSA key; this may take some time ...
Creating SSH2 ED25519 key; this may take some time ...
invoke-rc.d: policy-rc.d denied execution of restart.

*** Running /etc/my_init.d/database.sh...

Starting database in Network Server mode on host 0.0.0.0 and port 1527.
--------- Derby Network Server Information --------
Version: CSS10100/10.10.2.0 - (1582446)  Build: 1582446  DRDA Product Id: CSS10100
-- listing properties --
derby.drda.traceDirectory=/opt/glassfish4/glassfish/databases
derby.drda.maxThreads=0
derby.drda.sslMode=off
derby.drda.keepAlive=true
derby.drda.minThreads=0
derby.drda.portNumber=1527
derby.drda.logConnections=false
derby.drda.timeSlice=0
derby.drda.startNetworkServer=false
derby.drda.host=0.0.0.0
derby.drda.traceAll=false
------------------ Java Information ------------------
Java Version:    1.8.0_25
Java Vendor:     Oracle Corporation
Java home:       /opt/jdk1.8.0_25/jre
Java classpath:  /opt/glassfish4/glassfish/lib/asadmin/cli-optional.jar:/opt/glassfish4/javadb/lib/derby.jar:/opt/glassfish4/javadb/lib/derbytools.jar:/opt/glassfish4/javadb/lib/derbynet.jar:/opt/glassfish4/javadb/lib/derbyclient.jar
OS name:         Linux
OS architecture: amd64
OS version:      3.10.0-123.13.2.el7.x86_64
Java user name:  root
Java user home:  /root
Java user dir:   /
java.specification.name: Java Platform API Specification
java.specification.version: 1.8
java.runtime.version: 1.8.0_25-b17
--------- Derby Information --------
[/opt/glassfish4/javadb/lib/derby.jar] 10.10.2.0 - (1582446)
[/opt/glassfish4/javadb/lib/derbytools.jar] 10.10.2.0 - (1582446)
[/opt/glassfish4/javadb/lib/derbynet.jar] 10.10.2.0 - (1582446)
[/opt/glassfish4/javadb/lib/derbyclient.jar] 10.10.2.0 - (1582446)
------------------------------------------------------
----------------- Locale Information -----------------
Current Locale :  [English/United States [en_US]]
Found support for locale: [cs]
     version: 10.10.2.0 - (1582446)
Found support for locale: [de_DE]
     version: 10.10.2.0 - (1582446)
Found support for locale: [es]
     version: 10.10.2.0 - (1582446)
Found support for locale: [fr]
     version: 10.10.2.0 - (1582446)
Found support for locale: [hu]
     version: 10.10.2.0 - (1582446)
Found support for locale: [it]
     version: 10.10.2.0 - (1582446)
Found support for locale: [ja_JP]
     version: 10.10.2.0 - (1582446)
Found support for locale: [ko_KR]
     version: 10.10.2.0 - (1582446)
Found support for locale: [pl]
     version: 10.10.2.0 - (1582446)
Found support for locale: [pt_BR]
     version: 10.10.2.0 - (1582446)
Found support for locale: [ru]
     version: 10.10.2.0 - (1582446)
Found support for locale: [zh_CN]
     version: 10.10.2.0 - (1582446)
Found support for locale: [zh_TW]
     version: 10.10.2.0 - (1582446)
------------------------------------------------------
------------------------------------------------------

Starting database in the background.
Log redirected to /opt/glassfish4/glassfish/databases/derby.log.
Command start-database executed successfully.

*** Running /etc/my_init.d/run.sh...

Waiting for domain1 to start .......
Successfully started the domain : domain1
domain  Location: /opt/glassfish4/glassfish/domains/domain1
Log File: /opt/glassfish4/glassfish/domains/domain1/logs/server.log
Admin Port: 4848
Command start-domain executed successfully.
=> Modifying password of admin to random in Glassfish
spawn asadmin --user admin change-admin-password
Enter the admin password>
Enter the new admin password>
Enter the new admin password again>
Command change-admin-password executed successfully.
=> Enabling secure admin login
spawn asadmin enable-secure-admin
Enter admin user name>  admin
Enter admin password for user "admin">
You must restart all running servers for the change in secure admin to take effect.
Command enable-secure-admin executed successfully.
=> Done!
========================================================================
You can now connect to this Glassfish server using:

     admin:cDpn9CI5YMMz

Please remember to change the above password as soon as possible!
========================================================================
=> Restarting Glassfish server
Waiting for the domain to stop .
Command stop-domain executed successfully.
=> Starting and running Glassfish server
=> Debug mode is set to: false

   
*************************************
Start SSH session  via "exec" :
*************************************

[root@junolxc ~]# docker ps -a

da665944b757        boris/docker-glassfish41:latest   "/sbin/my_init"     21 hours ago        Up 3 minutes              9009/tcp, 0.0.0.0:49160->22/tcp, 0.0.0.0:49161->4848/tcp, 0.0.0.0:49162->8080/tcp, 0.0.0.0:49163->8181/tcp   clever_heisenberg
  

[root@junolxc ~]# docker exec -d da665944b757 /usr/sbin/sshd -D

[root@junolxc ~]# ssh root@localhost -p 49160

root@localhost's password:
Last login: Thu Jan  8 05:53:01 2015 from 172.17.42.1
root@da665944b757:~# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 05:50 ?        00:00:00 /usr/bin/python3 -u /sbin/my_init
root        39     1  0 05:50 ?        00:00:01 /opt/jdk1.8.0_25/bin/java -Djava.library.path=/op
root       114     1  0 05:50 ?        00:00:00 /bin/bash /etc/my_init.d/run.sh
root       344   114  0 05:51 ?        00:00:01 java -jar /opt/glassfish4/bin/../glassfish/lib/cl
root       358   344 15 05:51 ?        00:00:29 /opt/jdk1.8.0_25/bin/java -cp /opt/glassfish4/gla
root       455     0  0 05:51 ?        00:00:00 /usr/sbin/sshd -D
root       477   455  0 05:53 ?        00:00:00 sshd: root@pts/0   
root       479   477  0 05:53 pts/0    00:00:00 -bash
root       490   479  0 05:54 pts/0    00:00:00 ps -ef