These instructions are not intended to show you how to create a DB2 container, but instead instructions for running a DB2 instance with a database in a docker container with pre-installed DB2 Express-C 10.5 Fix Pack 7.
The sample can be used as is to create a sample container without having to modify the Dockerfile. Just copy Dockerfile.sample to Dockerfile and run docker build. Before doing so, look at the instructions below as a few steps are required to have a running docker container.
The template contains a number of tags that has to be replaced before you can run docker build. I provided a script to assist with this. The tags that need to be replaced are:
- CONTAINER_DB - DB2 database name for the container
- CONTAINER_PORT - DB2 instance port for the container
- CONTAINER_PS - DB2 database pagesize (4096, 8192, 16384 or 32768)
This script will build a Dockerfile from Dockerfile.template based on the arguments you provide to the script. If no arguments are passed to the script, it will print a usage statement with an example.
$ ./build_dockerfile.sh
usage: ./build_dockerfile.sh [-d database_name] [-p port] [-s pagesize]
- pagesize is one of: 4096, 8192, 16384 or 32768
example: ./build_dockerfile.sh -d sample -p 50000 -s 8192
Ensure you made the script executable before running it.
$ chmod 755 build_dockerfile.sh
- Get your hands on Dockerfile.template, Dockerfile.sample (optional) and build_dockerfile.sh
- Place these files in a docker build location of your choice
- Your docker location should now consist of the following files:
- build_dockerfile.sh
- Dockerfile.template
- Dockerfile.sample (optional)
- Run build_dockerfile.sh to generate the Dockerfile to use for docker build
$ ./build_dockerfile.sh -d mydb -p 50000 -s 8192
- You should now have a Dockerfile to use for docker build
- Run docker build (-t, the tag for the container can be anything you want it to be)
$ docker build -t db2-express-demo .
- See docker_build.out for expected output from docker build
- Run docker images, you should see the new container
db2-express-demo latest eab65833d26f 5 minutes ago 2.395 GB
- Now do a docker run to do initial DB2 configuration and database creation
docker run --privileged=true -ti db2-express-demo /home/db2inst1/create_db.sh
- Expected ouput from the above docker run should be similar:
DB20000I The UPDATE DATABASE MANAGER CONFIGURATION command completed successfully.
DB20000I The UPDATE DATABASE MANAGER CONFIGURATION command completed successfully.
SQL1063N DB2START processing was successful.
DB20000I The CREATE DATABASE command completed successfully.
DB20000I The UPDATE DATABASE CONFIGURATION command completed successfully.
- Now run docker ps -a | grep db2-express-demo to determine new container id
- 8401f868ff39 db2-express-demo "/home/db2inst1/creat" 5 minutes ago Exited (0) 5 minutes ago evil_kilby
- Now run docker commit for the new container id
docker commit 8401f868ff39 db2-express-demo
- You are now ready to run the container
docker run --privileged=true -ti -d db2-express-demo su - db2inst1 -c "db2start;bash"
--privileged=true
is required otherwise DB2 will not start;bash
is required otherwise DB2 will start but the container will end immediately afterwords
- If you want to verify if DB2 is running, run docker exec as inidicated below
- Run
docker ps
to get the container id - Run
docker exec -ti 8721f88166cd ps -ef |grep db2
and you should see output similar to:
root 1 0 0 19:30 ? 00:00:00 su - db2inst1 -c db2start;bash
db2inst1 12 1 0 19:30 ? 00:00:00 -bash -c db2start;bash
root 125 1 0 19:30 ? 00:00:00 db2wdog
db2inst1 127 125 0 19:30 ? 00:00:00 db2sysc
root 133 125 0 19:30 ? 00:00:00 db2ckpwd
root 134 125 0 19:30 ? 00:00:00 db2ckpwd
root 135 125 0 19:30 ? 00:00:00 db2ckpwd
db2inst1 137 125 0 19:30 ? 00:00:00 db2vend (PD Vendor Process - 1)
db2inst1 145 125 0 19:30 ? 00:00:00 db2acd ,0,0,0,1,0,0,0,0002,1,0
db2inst1 146 12 0 19:30 ? 00:00:00 bash
- Run
docker exec -ti 8721f88166cd su - db2inst1 -c "db2 connect to mydb"
and you should see:
Database Connection Information
Database server = DB2/LINUXX8664 10.5.7
SQL authorization ID = DB2INST1
Local database alias = MYDB
- DB2 is now running in a docker container with an instance and a database