Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit a6a19e5

Browse files
author
vctls
committed
Initial commit
0 parents  commit a6a19e5

11 files changed

+219
-0
lines changed

.dockerignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.git/
2+
.idea/
3+
.env

.env.dist

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
COMPOSE_PROJECT_NAME=php_db2
2+
DB2_HOST=
3+
DB2_PORT=
4+
DB2_DB=
5+
DB2_SCHEMA=
6+
DB2_PROTOCOL=
7+
DB2_USER=
8+
DB2_PASSWORD=

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
php_apache/ibm_data_server_driver_package_linuxx64_v11.5.tar.gz filter=lfs diff=lfs merge=lfs -text

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env

Dockerfile

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
FROM php:7.2.2-apache-stretch AS php_apache_pdo_db2
2+
3+
ENV IBM_DB_HOME /opt/ibm/dsdriver
4+
ENV LD_LIBRARY_PATH $IBM_DB_HOME/lib
5+
6+
RUN apt-get update && apt-get install -y \
7+
apt-utils \
8+
# The driver installation script is written in Korn shell.
9+
ksh \
10+
# Needed by the driver installation script.
11+
unzip
12+
13+
# Install the driver.
14+
RUN mkdir /opt/ibm
15+
COPY php_apache/ibm_data_server_driver_package_linuxx64_v11.5.tar.gz /opt/ibm/
16+
RUN cd /opt/ibm && tar -xvf ibm_data_server_driver_package_linuxx64_v11.5.tar.gz
17+
RUN ksh /opt/ibm/dsdriver/installDSDriver
18+
19+
# Install the ibm_db2 PHP extensions.
20+
RUN echo $IBM_DB_HOME | pecl install ibm_db2-2.0.8
21+
RUN docker-php-ext-enable ibm_db2
22+
RUN export LD_LIBRARY_PATH=$IBM_DB_HOME/lib
23+
24+
# Install and configure xdebug.
25+
RUN pecl install xdebug &&\
26+
docker-php-ext-enable xdebug &&\
27+
echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
28+
echo "error_reporting = E_ALL" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
29+
echo "display_startup_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
30+
echo "display_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
31+
echo "xdebug.idekey=PHPSTORM" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
32+
echo "xdebug.remote_port=9000" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
33+
echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
34+
echo "xdebug.remote_autostart=0" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
35+
echo "xdebug.remote_host=host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
36+
echo "xdebug.profiler_enable_trigger=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
37+
echo "xdebug.profiler_enable_trigger_value=XDEBUG_PROFILE" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
38+
39+
# Clean up.
40+
RUN rm -r /tmp/*
41+
42+
# Update the default apache site.
43+
ADD php_apache/conf/apache-config.conf /etc/apache2/sites-enabled/000-default.conf
44+
45+
# Copy the index.php file, a.k.a. The «API».
46+
COPY src/index.php /var/www/html/
47+
48+
CMD ["apache2-foreground"]

README.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Docker PHP, Apache and PDO_Db2
2+
Inspired by https://github.com/keboola/db-extractor-db2
3+
4+
Base configuration files to generate a Docker container with PHP, Apache and IBM_DB2 support.
5+
6+
This is a proof of concept on how to install IBM_DB2 with the IBM Db2 driver.
7+
8+
It provides a very crude web API to which you can send SQL queries and retrieve the results in JSON format.
9+
10+
**WARNING: Queries are executed literally. This provides no security whatsoever.**
11+
12+
### Setup
13+
14+
You can build it yourself as follows.
15+
16+
1. Clone the project.
17+
18+
2. DS Driver is versioned through Git LFS, but if for some reason you need to donwload it manually, backup link is below. Put it into the `php_apache` directory:
19+
* The IBM Data Server Driver Package (DS Driver), from the IBM website (as of 2019-08-29):
20+
Tested with version 11.5 for Linux AMD64 and Intel EM64 architecture.
21+
https://www-01.ibm.com/marketing/iwm/iwm/web/pickUrxNew.do?source=swg-idsdpds
22+
(requires login. See below.)
23+
24+
3. Copy `.env.dist` into `.env` and set your connection settings in that file.
25+
26+
>Since the IBM site is a real PITA, has changed multiple times, requires an account, and we have no guarantee that these files won't disappear overnight, here's a direct link to a working version:
27+
https://mega.nz/#!JU5FUKhI!YM9Rn457Qd2hUUarZVjimdsMahC6XGX8FKjR41V06fg
28+
And here's a link to the ibm_db2 source, just in case:
29+
https://mega.nz/#!UMwFkY5K!tPB4sWpLvLaMlrYT8zDt97seSenkEj0aPVE12stsdsI
30+
31+
### Build...
32+
33+
4. Build the image:
34+
`docker build -t phpdb2 .`
35+
36+
5. When containers are to be created, note that you must set the Infomix server IP:
37+
`docker run -d -p 80:80 -v /var/www/html:/var/www/html`.
38+
39+
### Or,
40+
Run the project with docker-compose:\
41+
`docker-compose up`
42+
43+
Open `localhost` in your browser to see if it's working.
44+
45+
You should see something like this:
46+
```json
47+
[{"1":"2019-08-29"}]
48+
```
49+
50+
Declare the `db2.local` domain for clarity.
51+
52+
You can pass URL encoded SQL queries as get parameter like this:
53+
```
54+
http://db2.local/?query=SELECT%20current%20date%20FROM%20sysibm.sysdummy1;
55+
```

docker-compose-nginx-proxy.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
version: '3.6'
2+
# Use this docker-compose file with nginx-proxy.
3+
services:
4+
db2:
5+
container_name: db2
6+
build:
7+
context: ./
8+
volumes:
9+
- ./src:/var/www/html
10+
expose:
11+
- 80
12+
environment:
13+
- DB2_HOST=${DB2_HOST}
14+
- DB2_PORT=${DB2_PORT}
15+
- DB2_DB=${DB2_DB}
16+
- DB2_SCHEMA=${DB2_SCHEMA}
17+
- DB2_PROTOCOL=${DB2_PROTOCOL}
18+
- DB2_USER=${DB2_USER}
19+
- DB2_PASSWORD=${DB2_PASSWORD}
20+
- VIRTUAL_PORT=80
21+
- VIRTUAL_HOST=db2.local
22+
23+
networks:
24+
default:
25+
external:
26+
name: nginx-proxy

docker-compose.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: '3.6'
2+
3+
services:
4+
db2:
5+
container_name: db2
6+
build:
7+
context: ./
8+
volumes:
9+
- ./src:/var/www/html
10+
ports:
11+
- "80:80"
12+
environment:
13+
- DB2_HOST=${DB2_HOST}
14+
- DB2_PORT=${DB2_PORT}
15+
- DB2_DB=${DB2_DB}
16+
- DB2_PROTOCOL=${DB2_PROTOCOL}
17+
- DB2_USER=${DB2_USER}
18+
- DB2_PASSWORD=${DB2_PASSWORD}

php_apache/conf/apache-config.conf

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<VirtualHost *:80>
2+
ServerName db2.local
3+
ServerAlias db2
4+
DocumentRoot /var/www/html
5+
6+
<Directory /var/www/html/>
7+
DirectoryIndex index.php
8+
Options Indexes FollowSymLinks MultiViews
9+
AllowOverride All
10+
Order deny,allow
11+
Allow from all
12+
</Directory>
13+
14+
ErrorLog ${APACHE_LOG_DIR}/error.log
15+
CustomLog ${APACHE_LOG_DIR}/access.log combined
16+
17+
</VirtualHost>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:55de48fd0af8afa27690d64af2f9ac057efc210c46baf81e036c9befc8754dea
3+
size 69950267

src/index.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
const QUERY = 'query';
4+
5+
$host = getenv('DB2_HOST');
6+
$port = getenv('DB2_PORT');
7+
$db = getenv('DB2_DB');
8+
$schema = getenv('DB2_SCHEMA');
9+
$prt = getenv('DB2_PROTOCOL');
10+
$user = getenv('DB2_USER');
11+
$pwd = getenv('DB2_PASSWORD');
12+
13+
$cnx = db2_connect(
14+
"DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$db;" .
15+
"HOSTNAME=$host;" .
16+
"PROTOCOL=$prt;" .
17+
"UID=$user;" .
18+
"PWD=$pwd;" .
19+
"PORT=$port",
20+
$user,
21+
$pwd
22+
);
23+
24+
if (array_key_exists(QUERY, $_GET)) {
25+
$sql = $_GET[QUERY];
26+
} else {
27+
$sql = 'SELECT current date FROM sysibm.sysdummy1;';
28+
}
29+
30+
$stm = db2_exec($cnx, $sql);
31+
$results = [];
32+
while($result = db2_fetch_assoc($stm)){
33+
$results[] = $result;
34+
}
35+
$response = json_encode($results, JSON_INVALID_UTF8_IGNORE);
36+
$json_error = json_last_error() . ' ' . json_last_error_msg();
37+
38+
header('Content-Type: application/json');
39+
echo $response;

0 commit comments

Comments
 (0)