Skip to content

Commit 867c132

Browse files
authored
Add Docker (#895)
1 parent 50cee04 commit 867c132

File tree

8 files changed

+430
-45
lines changed

8 files changed

+430
-45
lines changed

.gitattributes

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
*.gif binary
2222
*.ttf binary
2323

24-
# Ignore some meta files when creating an archive of this repository
24+
# Ignore some files when creating an archive of this repository
2525
/.github export-ignore
2626
/.editorconfig export-ignore
2727
/.gitattributes export-ignore
2828
/.gitignore export-ignore
2929
/phpunit.xml.dist export-ignore
30+
/docker export-ignore
3031
/docs export-ignore
3132

3233
# Avoid merge conflicts in CHANGELOG
3334
# https://about.gitlab.com/2015/02/10/gitlab-reduced-merge-conflicts-by-90-percent-with-changelog-placeholders/
3435
/CHANGELOG.md merge=union
35-

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,6 @@ phpunit.phar
4444
#codeception
4545
/tests/_output
4646
c3.php
47+
48+
# Docker
49+
docker/docker-compose.override.yml

Makefile

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
.PHONY: help
2+
help: ## Show the list of available commands with description.
3+
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
4+
.DEFAULT_GOAL := help
5+
6+
build: ## Build services.
7+
docker compose -f docker/docker-compose.yml -f docker/docker-compose.override.yml build
8+
up: ## Start services.
9+
docker compose -f docker/docker-compose.yml -f docker/docker-compose.override.yml up -d --remove-orphans
10+
build-up: # Build and start services.
11+
docker compose -f docker/docker-compose.yml -f docker/docker-compose.override.yml up -d --remove-orphans --build
12+
ps: ## List running services
13+
docker compose -f docker/docker-compose.yml -f docker/docker-compose.override.yml ps
14+
stop: ## Stop running services.
15+
docker compose -f docker/docker-compose.yml -f docker/docker-compose.override.yml stop
16+
down: ## Stop running services and remove all services (not defined services, containers, networks, volumes, images).
17+
docker compose -f docker/docker-compose.yml -f docker/docker-compose.override.yml down \
18+
--remove-orphans \
19+
--volumes \
20+
--rmi all
21+
22+
run: ## Run arbitrary command.
23+
docker compose -f docker/docker-compose.yml -f docker/docker-compose.override.yml run \
24+
--rm \
25+
--entrypoint $(CMD) \
26+
php
27+
28+
shell: CMD="bash" ## Open interactive shell.
29+
shell: run
30+
31+
test-all: test-base \
32+
test-drivers ## Run all available tests.
33+
test-drivers: test-sqlite \
34+
test-mysql \
35+
test-mariadb \
36+
test-pgsql \
37+
test-mssql \
38+
test-oracle ## Run tests for all drivers.
39+
test-base: testsuite-Db ## Run tests for base db package only.
40+
test-sqlite: testsuite-Sqlite ## Run tests for SQLite driver only.
41+
test-mysql: testsuite-Mysql ## Run tests for MySQL driver only (using MySQL database).
42+
test-mariadb: ## Run tests for MySQL driver only (using MariaDB database).
43+
docker compose -f docker/docker-compose.yml -f docker/docker-compose.override.yml run \
44+
--rm \
45+
--entrypoint "vendor/bin/phpunit --testsuite Mysql $(RUN_ARGS)" \
46+
-e YII_MYSQL_TYPE=mariadb \
47+
php
48+
test-pgsql: testsuite-Pgsql ## Run tests for PostgreSQL driver only.
49+
test-mssql: testsuite-Mssql ## Run tests for Microsoft SQL Server driver only.
50+
test-oracle: ## Run tests for Oracle driver only.
51+
docker compose -f docker/docker-compose.yml -f docker/docker-compose.override.yml run \
52+
--rm \
53+
--entrypoint "bash -c -l 'vendor/bin/phpunit --testsuite Oracle $(RUN_ARGS)'" \
54+
php
55+
56+
testsuite-%:
57+
docker compose -f docker/docker-compose.yml -f docker/docker-compose.override.yml run \
58+
--rm \
59+
--entrypoint "vendor/bin/phpunit --testsuite $(subst testsuite-,,$@) $(RUN_ARGS)" \
60+
php
61+
62+
mutation: CMD="\
63+
vendor/bin/roave-infection-static-analysis-plugin \
64+
--threads=2 \
65+
--min-msi=0 \
66+
--min-covered-msi=100 \
67+
--ignore-msi-with-no-mutations \
68+
--only-covered" ## Run mutation tests using Infection.
69+
mutation: run
70+
71+
static-analysis: CMD="vendor/bin/psalm --no-cache" ## Run static analysis using Psalm.
72+
static-analysis: run
73+
74+
rector: CMD="vendor/bin/rector" ## Check code style using Rector.
75+
rector: run
76+
77+
composer-require-checker: CMD="vendor/bin/composer-require-checker" ## Check dependencies using Composer Require Checker.
78+
composer-require-checker: run

composer.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@
6161
},
6262
"autoload-dev": {
6363
"psr-4": {
64+
"Yiisoft\\ActiveRecord\\": "vendor/yiisoft/active-record/src",
65+
"Yiisoft\\Db\\Migration\\": "vendor/yiisoft/db-migration/src",
66+
"Yiisoft\\Db\\Mssql\\": "vendor/yiisoft/db-mssql/src",
67+
"Yiisoft\\Db\\Mysql\\": "vendor/yiisoft/db-mysql/src",
68+
"Yiisoft\\Db\\Oracle\\": "vendor/yiisoft/db-oracle/src",
69+
"Yiisoft\\Db\\Pgsql\\": "vendor/yiisoft/db-pgsql/src",
70+
"Yiisoft\\Db\\Sqlite\\": "vendor/yiisoft/db-sqlite/src",
6471
"Yiisoft\\Db\\Tests\\": "tests",
6572
"Yiisoft\\ActiveRecord\\Tests\\": "vendor/yiisoft/active-record/tests",
6673
"Yiisoft\\Db\\Mssql\\Tests\\": "vendor/yiisoft/db-mssql/tests",

docker/Dockerfile

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
FROM composer/composer:latest-bin AS composer
2+
3+
FROM php:8.3-cli
4+
5+
# System packages
6+
7+
RUN apt-get update && apt-get install -y \
8+
unzip \
9+
# PostgreSQL
10+
libpq-dev \
11+
# MSSQL
12+
apt-transport-https \
13+
gnupg2 \
14+
libpng-dev \
15+
# Oracle
16+
libaio1 && \
17+
rm -rf /var/lib/apt/lists/*
18+
19+
# MSSQL dependencies
20+
21+
ENV ACCEPT_EULA=Y
22+
23+
# Install prerequisites for the sqlsrv and pdo_sqlsrv PHP extensions.
24+
# Some packages are pinned with lower priority to prevent build issues due to package conflicts.
25+
# Link: https://github.com/microsoft/linux-package-repositories/issues/39
26+
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
27+
&& curl https://packages.microsoft.com/config/debian/11/prod.list > /etc/apt/sources.list.d/mssql-release.list \
28+
&& echo "Package: unixodbc\nPin: origin \"packages.microsoft.com\"\nPin-Priority: 100\n" >> /etc/apt/preferences.d/microsoft \
29+
&& echo "Package: unixodbc-dev\nPin: origin \"packages.microsoft.com\"\nPin-Priority: 100\n" >> /etc/apt/preferences.d/microsoft \
30+
&& echo "Package: libodbc1:amd64\nPin: origin \"packages.microsoft.com\"\nPin-Priority: 100\n" >> /etc/apt/preferences.d/microsoft \
31+
&& echo "Package: odbcinst\nPin: origin \"packages.microsoft.com\"\nPin-Priority: 100\n" >> /etc/apt/preferences.d/microsoft \
32+
&& echo "Package: odbcinst1debian2:amd64\nPin: origin \"packages.microsoft.com\"\nPin-Priority: 100\n" >> /etc/apt/preferences.d/microsoft \
33+
&& apt-get update \
34+
&& apt-get install -y msodbcsql18 mssql-tools18 unixodbc-dev \
35+
&& rm -rf /var/lib/apt/lists/*
36+
37+
# Oracle dependencies
38+
39+
RUN cd /tmp && curl -L https://download.oracle.com/otn_software/linux/instantclient/2350000/instantclient-basic-linux.x64-23.5.0.24.07.zip -O
40+
RUN cd /tmp && curl -L https://download.oracle.com/otn_software/linux/instantclient/2350000/instantclient-sdk-linux.x64-23.5.0.24.07.zip -O
41+
RUN cd /tmp && curl -L https://download.oracle.com/otn_software/linux/instantclient/2350000/instantclient-sqlplus-linux.x64-23.5.0.24.07.zip -O
42+
43+
RUN unzip /tmp/instantclient-basic-linux.x64-23.5.0.24.07.zip -d /usr/local/
44+
RUN unzip -o /tmp/instantclient-sdk-linux.x64-23.5.0.24.07.zip -d /usr/local/
45+
RUN unzip -o /tmp/instantclient-sqlplus-linux.x64-23.5.0.24.07.zip -d /usr/local/
46+
47+
RUN ln -s /usr/local/instantclient_23_5 /usr/local/instantclient
48+
RUN ln -s /usr/local/instantclient/lib* /usr/lib
49+
RUN ln -s /usr/local/instantclient/sqlplus /usr/bin/sqlplus
50+
51+
RUN echo 'export LD_LIBRARY_PATH="/usr/local/instantclient"' >> /root/.bashrc
52+
RUN echo 'umask 002' >> /root/.bashrc
53+
54+
# PHP extensions
55+
56+
RUN docker-php-ext-install \
57+
pdo_mysql \
58+
pdo_pgsql \
59+
# For Psalm, to make use of JIT for a 20%+ performance boost.
60+
opcache
61+
62+
RUN pecl install sqlsrv
63+
RUN printf "; priority=20\nextension=sqlsrv.so\n" > /usr/local/etc/php/conf.d/php-sqlsrv.ini
64+
65+
RUN pecl install pdo_sqlsrv
66+
RUN printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /usr/local/etc/php/conf.d/php-pdo-sqlsrv.ini
67+
68+
RUN echo 'instantclient,/usr/local/instantclient' | pecl install oci8
69+
RUN echo "extension=oci8.so" > /usr/local/etc/php/conf.d/php-oci8.ini
70+
71+
RUN echo 'instantclient,/usr/local/instantclient' | pecl install pdo_oci
72+
RUN echo "extension=pdo_oci.so" > /usr/local/etc/php/conf.d/php-pdo-oci.ini
73+
74+
# For code coverage (mutation testing)
75+
RUN pecl install pcov && docker-php-ext-enable pcov
76+
77+
# Composer
78+
79+
COPY --from=composer /composer /usr/bin/composer
80+
81+
# Code
82+
83+
COPY . /code
84+
WORKDIR /code
85+
86+
# PHP packages
87+
88+
RUN COMPOSER_ALLOW_SUPERUSER=1 composer install

docker/docker-compose.yml

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
name: yiisoft-db
2+
services:
3+
php:
4+
build:
5+
context: ./..
6+
dockerfile: ./docker/Dockerfile
7+
volumes:
8+
- ./../src:/code/src
9+
- ./../tests:/code/tests
10+
- ./../composer.json:/code/composer.json
11+
- ./../phpunit.xml.dist:/code/phpunit.xml.dist
12+
environment:
13+
YII_MYSQL_DATABASE: yii
14+
YII_MYSQL_HOST: mysql
15+
YII_MYSQL_PORT: 3306
16+
YII_MYSQL_USER: root
17+
YII_MYSQL_PASSWORD: root
18+
YII_MYSQL_TYPE: mysql
19+
20+
YII_MARIADB_DATABASE: yii
21+
YII_MARIADB_HOST: mariadb
22+
YII_MARIADB_PORT: 13306
23+
YII_MARIADB_USER: root
24+
YII_MARIADB_PASSWORD: root
25+
26+
YII_PGSQL_DATABASE: yii
27+
YII_PGSQL_HOST: postgres
28+
YII_PGSQL_PORT: 5432
29+
YII_PGSQL_USER: postgres
30+
YII_PGSQL_PASSWORD: postgres
31+
32+
YII_MSSQL_DATABASE: tempdb
33+
YII_MSSQL_HOST: mssql
34+
YII_MSSQL_PORT: 1433
35+
YII_MSSQL_USER: SA
36+
YII_MSSQL_PASSWORD: YourStrong!Passw0rd
37+
38+
YII_ORACLE_SID: FREE
39+
YII_ORACLE_DATABASE: FREEPDB1
40+
YII_ORACLE_HOST: oracle
41+
YII_ORACLE_PORT: 1521
42+
YII_ORACLE_USER: system
43+
YII_ORACLE_PASSWORD: sys_user_password
44+
45+
ORACLE_PASSWORD: sys_user_password
46+
APP_USER: my_user
47+
APP_USER_PASSWORD: password_i_should_change
48+
command: tail -F anything
49+
depends_on:
50+
mysql:
51+
condition: service_healthy
52+
mariadb:
53+
condition: service_healthy
54+
postgres:
55+
condition: service_healthy
56+
mssql:
57+
condition: service_healthy
58+
oracle:
59+
condition: service_healthy
60+
mysql:
61+
image: mysql:9
62+
ports:
63+
- "3306:3306"
64+
volumes:
65+
- type: tmpfs
66+
target: /var/lib/mysql
67+
environment:
68+
MYSQL_DATABASE: yii
69+
MYSQL_ROOT_PASSWORD: root
70+
MYSQL_ROOT_HOST: "%"
71+
healthcheck:
72+
test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost", "-uroot" ]
73+
interval: 5s
74+
timeout: 5s
75+
retries: 20
76+
mariadb:
77+
image: mariadb:11
78+
ports:
79+
- "13306:3306"
80+
volumes:
81+
- type: tmpfs
82+
target: /var/lib/mysql
83+
environment:
84+
MYSQL_DATABASE: yii
85+
MYSQL_ROOT_PASSWORD: root
86+
MYSQL_ROOT_HOST: "%"
87+
MYSQL_TCP_PORT: 13306
88+
healthcheck:
89+
test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
90+
start_period: 10s
91+
interval: 10s
92+
timeout: 5s
93+
retries: 30
94+
postgres:
95+
image: postgres:17
96+
ports:
97+
- "5432:5432"
98+
volumes:
99+
- type: tmpfs
100+
target: /var/lib/postgresql/data
101+
environment:
102+
POSTGRES_DB: yii
103+
POSTGRES_USER: postgres
104+
POSTGRES_PASSWORD: postgres
105+
healthcheck:
106+
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
107+
interval: 5s
108+
timeout: 5s
109+
retries: 5
110+
mssql:
111+
image: mcr.microsoft.com/mssql/server:2022-latest
112+
ports:
113+
- "1433:1433"
114+
user: root
115+
volumes:
116+
- mssql-data:/var/opt/mssql/data
117+
- mssql-log:/var/opt/mssql/log
118+
- mssql-secrets:/var/opt/mssql/secrets
119+
environment:
120+
SA_PASSWORD: YourStrong!Passw0rd
121+
ACCEPT_EULA: Y
122+
healthcheck:
123+
test: /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "$${SA_PASSWORD}" -Q "SELECT 1" -b -C -o /dev/null
124+
interval: 10s
125+
timeout: 3s
126+
retries: 100
127+
start_period: 10s
128+
oracle:
129+
build:
130+
context: oracle
131+
ports:
132+
- "1521:1521"
133+
volumes:
134+
- oracle-data:/opt/oracle/oradata
135+
environment:
136+
ORACLE_PASSWORD: sys_user_password
137+
APP_USER: my_user
138+
APP_USER_PASSWORD: password_i_should_change
139+
healthcheck:
140+
test: ["CMD", "healthcheck.sh"]
141+
interval: 10s
142+
timeout: 5s
143+
retries: 100
144+
start_period: 5s
145+
start_interval: 5s
146+
volumes:
147+
mssql-data:
148+
mssql-log:
149+
mssql-secrets:
150+
oracle-data:

docker/oracle/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM gvenzl/oracle-free:23
2+
3+
RUN chown -R 54321:54321 /opt/oracle/oradata && chmod 0777 /opt/oracle/oradata

0 commit comments

Comments
 (0)