IBM WebSphere Liberty Profile and IBM DB2 and Docker - An approach
and had a query from a colleague last week.
So I did some of again ....
The context is that my colleague was looking to run a Java class, using the DB2 JDBC JCC4 drivers, where DB2 was running in a Docker container.
So I replicated this, using Liberty as my Java runtime ....
Here's the details: -
Create a directory on the host for DB2 to use for its datastore
mkdir /tmp/db2data
Start a DB2 container
docker run -itd --name mydb2 --privileged=true -p 50000:50000 -e LICENSE=accept -e DB2INST1_PASSWORD=p455w0rd -e DBNAME=testdb -v /tmp/db2data:/database ibmcom/db2
Check the container logs for the DB2 startup
docker logs mydb2 -f
Open a shell into the DB2 container
docker exec -it mydb2 /bin/bash
Switch to db2inst1 instance account
su - db2inst1
Create the DB2 sample database
db2sampl
Quit back to the host OS
exit
exit
Copy the DB2 JDBC JCC4 driver AND license file out of the DB2 container
docker cp mydb2:/opt/ibm/db2/V11.5/java/db2jcc4.jar .
docker cp mydb2:/opt/ibm/db2/V11.5/java/db2jcc_license_cu.jar .
Create a Dockerfile to spin up a Liberty container using the DB2 JAR files
vi Dockerfile
FROM websphere-liberty:latest
ENV LICENSE accept
COPY db2jcc4.jar /
COPY db2jcc_license_cu.jar /
COPY JdbcTestDB2.class /
CMD ["java","-cp","/:/db2jcc4.jar","JdbcTestDB2","carded1.fyre.ibm.com","50000","sample","db2inst1 ","p455w0rd"]
Start the Liberty container
docker run -dt --name mywlp wlp:latest
Check the Liberty logs
000010 CHRISTINE HAAS
000020 MICHAEL THOMPSON
000030 SALLY KWAN
000050 JOHN GEYER
000060 IRVING STERN
000070 EVA PULASKI
000090 EILEEN HENDERSON
000100 THEODORE SPENSER
000110 VINCENZO LUCCHESSI
000120 SEAN O'CONNELL
000130 DELORES QUINTANA
000140 HEATHER NICHOLLS
000150 BRUCE ADAMSON
000160 ELIZABETH PIANKA
000170 MASATOSHI YOSHIMURA
000180 MARILYN SCOUTTEN
000190 JAMES WALKER
000200 DAVID BROWN
000210 WILLIAM JONES
000220 JENNIFER LUTZ
000230 JAMES JEFFERSON
000240 SALVATORE MARINO
000250 DANIEL SMITH
000260 SYBIL JOHNSON
000270 MARIA PEREZ
000280 ETHEL SCHNEIDER
000290 JOHN PARKER
000300 PHILIP SMITH
000310 MAUDE SETRIGHT
000320 RAMLAL MEHTA
000330 WING LEE
000340 JASON GOUNOT
200010 DIAN HEMMINGER
200120 GREG ORLANDO
200140 KIM NATZ
200170 KIYOSHI YAMAMOTO
200220 REBA JOHN
200240 ROBERT MONTEVERDE
200280 EILEEN SCHWARTZ
200310 MICHELLE SPRINGER
200330 HELENA WONG
200340 ROY ALONZO
For the record, the Java class that I'm running is here: -
vi JdbcTestDB2.java
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.ResultSet ;
import java.sql.Statement ;
import java.sql.SQLException;
class JdbcTestDB2
{
public static void main (String args[])
{
try
{
Class.forName("com.ibm.db2.jcc.DB2Driver");
}
catch (ClassNotFoundException e)
{
System.err.println (e) ;
System.exit (-1) ;
}
String hostname = args[0];
String port = args[1];
String dbName = args[2];
String userName = args[3];
String password = args[4];
String sslConnection = "false";
java.util.Properties properties = new java.util.Properties();
properties.put("user",userName);
properties.put("password", password);
String url = "jdbc:db2://" + hostname + ":" + port + "/" + dbName;
try
{
Connection connection = DriverManager.getConnection(url,properties);
String query = "select EMPNO,FIRSTNME,LASTNAME from DB2INST1.EMPLOYEE" ;
Statement statement = connection.createStatement () ;
ResultSet rs = statement.executeQuery (query) ;
while ( rs.next () )
System.out.println (rs.getString (1) + " " + rs.getString(2) + " " + rs.getString(3)) ;
connection.close () ;
}
catch (java.sql.SQLException e)
{
System.err.println (e) ;
System.exit (-1) ;
}
}
}
and I can easily run the same class from the host OS: -
/opt/ibm/java/jre/bin/java -cp $(pwd):$(pwd)/db2jcc4.jar JdbcTestDB2 carded1.fyre.ibm.com 50000 sample db2inst1 p455w0rd
For what it's worth, I have the IBM Java 8 SDK: -
ibm-java-sdk-8.0-5.41-x86_64-archive.bin
installed on the host OS ( Ubuntu 18.04.3 LTS )
No comments:
Post a Comment