While developing Hibernate, I need to test the code base against a plethora of relational database systems: Oracle, SQL Server, PostgreSQL, MySQL, MariaDB, Informix, and of course DB2.
However, having all these databases installed on my system is far from ideal, so I rely a lot on Docker for this task. In this article, I’m going to show how easily you can install DB2 on Docker and set up the JDBC connection so that you can run Hibernate tests on DB2.
Installing the DB2 Docker container
Nowadays, all major RDBMS providers offer official Docker images on DockerHub, and IBM is no different. The Express-C edition can be fetched automatically during Docker container set up.
To create a new DB2 Docker container on Windows, you need to run the following command:
docker run ^ --name db2 ^ -p 50000:50000 ^ -e DB2INST1_PASSWORD=db2inst1-pwd ^ -e LICENSE=accept ^ -d ibmcom/db2express-c db2start
On Linux, you need to use the
\ multiline separator:
docker run \ --name db2 \ -p 50000:50000 \ -e DB2INST1_PASSWORD=db2inst1-pwd \ -e LICENSE=accept \ -d ibmcom/db2express-c db2start
Now, you can see the newly installed Docker container by running
docker ps -as
> docker ps -as CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE 731eee777310 ibmcom/db2express-c "/entrypoint.sh db..." 2 minutes ago Up 2 minutes 22/tcp, 0.0.0.0:50000->50000/tcp db2 3.64 MB (virtual 1.71 GB)
When you want to stop DB2, you can run:
> docker stop db2
To start it again, you have to run the following command:
> docker start db2
Setting up the DB2 Docker container
To set up the DB2 Docker container, you need to open a bash terminal inside the DB2 container:
docker exec -i -t db2 /bin/bash
Now, you need to switch to the
db2inst1 Linux user which is set up to run DB2:
su - db2inst1
We are going to create a new database called
hiber8. Strangely, DB2 limits the database name length to 8 bytes only.
db2 create db hibern8
Setting up the JDBC conection
Ironically, it takes more time to get the JDBC Driver than to install the DB2 Docker image. To get the JDBC Driver, you need to have an IBM account and download it from IBM Support.
Now, you can install the
db2jcc4.jar in your local Maven repsoitory using the following command:
mvn install:install-file -Dfile=db2jcc4.jar -DgroupId=com.ibm.db2 -DartifactId=db2jcc -Dversion=10.5 -Dpackaging=jar -DgeneratePom=true
To connect to the newly installed DB2 Docker container, you need the following connection properties:
db2 : [ 'db.dialect' : 'org.hibernate.dialect.DB2Dialect', 'jdbc.driver': 'com.ibm.db2.jcc.DB2Driver', 'jdbc.user' : 'db2inst1', 'jdbc.pass' : 'db2inst1-pwd', 'jdbc.url' : 'jdbc:db2://127.0.0.1:50000/hibern8' ]
If you enjoyed this article, I bet you are going to love my book as well.
Docker makes database testing a breeze. You can have as many database systems you need set up in Docker without having to bloat your currently running operating system.
Another great advantage of having your testing databases on Docker is that you can simply copy the Docker machine file from one system to another, which is ideal when you switch to a new notebook or when you have to share the default database containers with the rest of your team.
If you liked this article, you might want to subscribe to my newsletter too.