Run DB2Rest on Docker
This tutorial shows how you can get started with DB2Rest in less than 10 minutes on Docker.
Minimum System Requirements
- Java 21
- 4-8 GB RAM/Memory.
Prerequisite
- PostgreSQL/MySQL is installed and running.
employee
table is created.
The script for creating the employee table is listed below:
PostgreSQL
CREATE TABLE employee (
id serial4 NOT NULL,
first_name varchar(50) NOT NULL,
last_name varchar(50) NOT NULL,
email varchar(255) NOT NULL,
created_on timestamp NOT NULL,
CONSTRAINT employee_email_key UNIQUE (email),
CONSTRAINT employee_pkey PRIMARY KEY (id)
);
MySQL
CREATE TABLE `employee` (
`emp_id` int unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(100) NOT NULL,
`last_name` varchar(150) NOT NULL,
`create_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`is_active` tinyint(1) DEFAULT '1',
PRIMARY KEY (`emp_id`)
);
You can use your favorite SQL client to execute these scripts.
1. Install Docker
To download the Docker desktop browse to this link. This page then shows options to download Docker for Mac, Windows, and Linux. Please click on the link for your operating system (OS) to start the download. After the download is completed, follow the steps outlined here pertinent to your OS, to install and test the Docker desktop.
2. Run DB2Rest
Now launch a terminal window to run the DB2Rest docker image. DB2Rest docker image is available on Dockerhub. In the terminal run the following command:
$ docker run -p 8080:8080 -e DB_URL=[DB_URL] -e DB_USER=[DB_USER] -e DB_PASSWORD=[DB_PASSWORD] kdhrubo/db2rest:latest
DB2Rest internally starts on port 8080 by default, but is not exposed automatically on startup.
To expose the default service port 8080, use the -p
option on docker run, such as -p 8080:8080
or an alternative port such as -p 1234:8080
Sl# | Parameter Name | Description | Example |
---|---|---|---|
1. | DB_URL | JDBC URL conneciton string | - MySQL : jdbc:mysql://host.docker.internal:3306/sakila - PostgreSQL : jdbc:postgresql://host.docker.internal:5432/sakila?currentSchema=public |
2. | DB_USER | Database user | |
3. | DB_PASSWORD | Database password |
Once this command is executed, within a few seconds, DB2Rest is ready to service your data access requests.
Verify DB2Rest Installation
The actuator endpoint can be used to test the installation.
- cURL
- HTTPie
curl --request GET \
--url http://[IP_ADDRESS]:[PORT]/actuator/health \
--header 'User-Agent: insomnia/8.6.1'
http GET http://[IP_ADDRESS]:[PORT]/actuator/health \
User-Agent:insomnia/8.6.1
The actuator health check service in DB2Rest will return the following response:
HTTP/1.1 200
Content-Type: application/json
Transfer-Encoding: chunked
{
"status": "UP"
}
The status
value of UP
confirms that the service is up and running.
Test Drive DB2Rest
Insert Row
- cURL
- HTTPie
curl --request POST \
--url http://[IP_ADDRESS]:[PORT]/v1/rdbms/db/employee \
--header 'Content-Type: application/json' \
--header 'User-Agent: insomnia/8.6.1' \
--data '{
"first_name" : "Salman",
"last_name" : "Khan",
"email" : "sk@skfilms.com",
"created_on" : "2015-04-14T11:07:36.639Z"
}'
echo '{
"first_name" : "Salman",
"last_name" : "Khan",
"email" : "sk@skfilms.com",
"created_on" : "2015-04-14T11:07:36.639Z"
}' | \
http POST http://[IP_ADDRESS]:[PORT]/v1/rdbms/db/employee \
Content-Type:application/json \
User-Agent:insomnia/8.6.1
HTTP Response
{
"row": 1,
"keys": {
"id": 1
}
}
Read Row
- cURL
- HTTPie
curl --request GET \
--url http://[IP_ADDRESS]:[PORT]/v1/rdbms/db/employee \
--header 'User-Agent: insomnia/8.6.1'
http GET http://[IP_ADDRESS]:[PORT]/v1/rdbms/db/employee \
User-Agent:insomnia/8.6.1
HTTP Response
[
{
"id": 1,
"first_name": "Salman",
"last_name": "Khan",
"email": "sk@skfilms.com",
"created_on": "2015-04-14T11:07:36.639+00:00"
}
]
Finally, refer to the DB2Rest documentation for further learning and exploring API features.
For help, visit us on Discord or our GitHub Discussions