From 273716b0af2e54ca470cc5c8dd1b483e23298c4d Mon Sep 17 00:00:00 2001 From: Dmitriy Krivopalov Date: Tue, 14 Apr 2026 22:42:09 +0300 Subject: [PATCH] add environment example on postgres and mysql --- .docker/mysql/Dockerfile | 5 + .docker/mysql/conf.d/collation.cnf | 9 ++ .../01-databases.sql | 4 + .docker/mysql/my.cnf | 23 ++++ .docker/postgresql/Dockerfile | 3 + .docker/postgresql/conf/pg_hba.conf | 8 ++ .docker/postgresql/initdb/init.sql | 2 + Makefile | 42 ++++-- docker-compose.yml | 120 ++++++++++++++++++ 9 files changed, 207 insertions(+), 9 deletions(-) create mode 100644 .docker/mysql/Dockerfile create mode 100644 .docker/mysql/conf.d/collation.cnf create mode 100644 .docker/mysql/docker-entrypoint-initdb.d/01-databases.sql create mode 100644 .docker/mysql/my.cnf create mode 100755 .docker/postgresql/Dockerfile create mode 100755 .docker/postgresql/conf/pg_hba.conf create mode 100644 .docker/postgresql/initdb/init.sql create mode 100644 docker-compose.yml diff --git a/.docker/mysql/Dockerfile b/.docker/mysql/Dockerfile new file mode 100644 index 0000000..fa54a06 --- /dev/null +++ b/.docker/mysql/Dockerfile @@ -0,0 +1,5 @@ +FROM mysql:9.5 + +COPY docker-entrypoint-initdb.d /docker-entrypoint-initdb.d +COPY conf.d/collation.cnf /etc/mysql/conf.d/collation.cnf +COPY my.cnf /etc/my.cnf diff --git a/.docker/mysql/conf.d/collation.cnf b/.docker/mysql/conf.d/collation.cnf new file mode 100644 index 0000000..c018067 --- /dev/null +++ b/.docker/mysql/conf.d/collation.cnf @@ -0,0 +1,9 @@ +[client] +default-character-set = utf8mb4 + +[mysql] +default-character-set = utf8mb4 + +[mysqld] +character-set-server = utf8mb4 +collation-server = utf8mb4_unicode_ci diff --git a/.docker/mysql/docker-entrypoint-initdb.d/01-databases.sql b/.docker/mysql/docker-entrypoint-initdb.d/01-databases.sql new file mode 100644 index 0000000..1d73daa --- /dev/null +++ b/.docker/mysql/docker-entrypoint-initdb.d/01-databases.sql @@ -0,0 +1,4 @@ +-- GRANT PRIVILEGES +CREATE USER IF NOT EXISTS 'dbuser'@'%' IDENTIFIED BY 'dbpassword'; +GRANT ALL ON main.* TO 'dbuser'@'%'; +FLUSH PRIVILEGES; diff --git a/.docker/mysql/my.cnf b/.docker/mysql/my.cnf new file mode 100644 index 0000000..aa95c65 --- /dev/null +++ b/.docker/mysql/my.cnf @@ -0,0 +1,23 @@ +[mysqld] +max_connections = 5 +disable-log-bin = 1 +performance-schema = 0 + +innodb_buffer_pool_size = 1G +innodb_flush_log_at_trx_commit = 2 +innodb_read_io_threads = 12 +innodb_write_io_threads = 12 +innodb_stats_on_metadata = 0 +innodb_file_per_table = 1 + +thread_cache_size = 8 +table_definition_cache = 65536 +table_open_cache = 65536 + +tmp_table_size = 64M +max_heap_table_size = 64M + +read_buffer_size = 256K +join_buffer_size = 512K +sort_buffer_size = 512K +read_rnd_buffer_size = 512K diff --git a/.docker/postgresql/Dockerfile b/.docker/postgresql/Dockerfile new file mode 100755 index 0000000..a56c8d9 --- /dev/null +++ b/.docker/postgresql/Dockerfile @@ -0,0 +1,3 @@ +FROM postgres:16-alpine3.21 + +COPY initdb /docker-entrypoint-initdb.d diff --git a/.docker/postgresql/conf/pg_hba.conf b/.docker/postgresql/conf/pg_hba.conf new file mode 100755 index 0000000..e32a281 --- /dev/null +++ b/.docker/postgresql/conf/pg_hba.conf @@ -0,0 +1,8 @@ +# TYPE DATABASE USER ADDRESS METHOD +local all all trust +host all all 127.0.0.1/32 trust +host all all ::1/128 trust +host all all 172.17.0.1/16 trust +host all all 172.18.0.1/16 trust +host all all 10.0.0.0/8 trust +host all all all md5 diff --git a/.docker/postgresql/initdb/init.sql b/.docker/postgresql/initdb/init.sql new file mode 100644 index 0000000..22355e9 --- /dev/null +++ b/.docker/postgresql/initdb/init.sql @@ -0,0 +1,2 @@ +-- Create a new logical database +CREATE DATABASE maincopy; diff --git a/Makefile b/Makefile index ff42b7b..82f1255 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ ARGS = $(filter-out $@,$(MAKECMDGOALS)) DOCKER_RUN = docker run --init -it --rm -u ${USER} -v "$$(pwd):/app" -w /app # https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html -.PHONY: help tests fix check +.PHONY: help tests fix check example .DEFAULT_GOAL := help help: ## Display this help screen @@ -61,14 +61,9 @@ rector: ## rector ghcr.io/kuaukutsu/php:${PHP_VERSION}-cli \ ./vendor/bin/rector -fix: ## run fix tools - make phpcbf - make rector +fix: phpcbf rector ## run fix tools -check: ## run analysis tools - make phpcs - make psalm - make phpstan +check: phpcs psalm phpstan ## run analysis tools infection: docker build \ @@ -115,7 +110,36 @@ tests: ## Application -app: +cli: $(DOCKER_RUN) -w /app/example \ ghcr.io/kuaukutsu/php:${PHP_VERSION}-cli \ sh -l + +example: + VERSION=$(VERSION) USER=$(USER) \ + docker compose run --rm -u $(USER) -w /example app sh -l + make stop + +stop: ## Stop server + docker compose -f ./docker-compose.yml stop + +down: stop + docker compose -f ./docker-compose.yml down --remove-orphans + +remove: down _image_remove _container_remove _volume_remove + +_image_remove: + docker image rm -f \ + migrator-app \ + migrator-postgres \ + migrator-mysql + +_container_remove: + docker rm -f \ + migrator_postgres \ + migrator_mysql + +_volume_remove: + docker volume rm -f \ + migrator_pg_data \ + migrator_mysql_data diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..fda2ee7 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,120 @@ +x-default-logging: &default-logging + driver: local + options: + max-size: "5m" + max-file: "3" + +x-volumes: + src: &vol-src + type: bind + source: ./src + target: /src + + example: &vol-example + type: bind + source: ./example + target: /example + + vendor: &vol-vendor + type: bind + source: ./vendor + target: /vendor + read_only: true + +name: migrator +services: + app: + container_name: migrator_app + build: + context: .docker/php/cli + target: devel + args: + UID: ${USER:-1} # ID:1 daemon, support: linux, mac + WORKDIR: "/example" + depends_on: + postgres: + condition: service_healthy + mysql: + condition: service_healthy + networks: + - migrator + volumes: + - *vol-src + - *vol-example + - *vol-vendor + logging: *default-logging + + postgres: + container_name: migrator_postgres + build: + context: .docker/postgresql + deploy: + resources: + limits: + cpus: "1.0" + memory: 512M + reservations: + cpus: "0.5" + memory: 256M + ports: + - "5435:5432" + environment: + - POSTGRES_DB=main + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + networks: + - migrator + volumes: + - pg_data:/var/lib/postgresql/data + healthcheck: + test: "pg_isready -U \"$$POSTGRES_USER\" -d \"$$POSTGRES_DB\"" + interval: 5s + timeout: 5s + retries: 5 + start_period: 2s + start_interval: 1s + logging: *default-logging + + mysql: + container_name: migrator_mysql + build: + context: .docker/mysql + cap_add: + - SYS_NICE # CAP_SYS_NICE + deploy: + resources: + limits: + cpus: "1.0" + memory: 512M + reservations: + cpus: "0.5" + memory: 256M + ports: + - "3306:3306" + networks: + - migrator + command: [ + '--character-set-server=utf8mb4', + '--collation-server=utf8mb4_unicode_ci' + ] + environment: + MYSQL_ROOT_PASSWORD: root + MYSQL_DATABASE: main + MYSQL_USER: dbuser + MYSQL_PASSWORD: dbpassword + volumes: + - mysql_data:/var/lib/mysql + healthcheck: + test: [ "CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-proot" ] + interval: 10s + timeout: 5s + retries: 5 + start_period: 10s + logging: *default-logging + +volumes: + pg_data: + mysql_data: + +networks: + migrator: