Merge pull request #12712 from appwrite/fix/compose-generator-main

Generate installer compose from base compose
This commit is contained in:
Chirag Aggarwal authored and GitHub committed 2026-06-29 16:55:51 +05:30
commit 386a6b13cf
13 files changed
+1243 -1710

No files matched your search

+14 -1
View File
@@ -5,7 +5,7 @@ concurrency:
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
env:
COMPOSE_FILE: docker-compose.yml
COMPOSE_FILE: docker-compose.yml:docker-compose.override.yml
IMAGE: appwrite-dev
K6_VERSION: '0.53.0'
@@ -107,6 +107,11 @@ jobs:
tools: composer:v2
coverage: none
- name: Setup Node
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
with:
node-version: '24'
- name: Validate composer.json
run: composer validate
@@ -114,6 +119,10 @@ jobs:
id: install
run: composer install --prefer-dist --no-progress --ignore-platform-reqs
- name: Install Node dependencies
if: ${{ !cancelled() && steps.install.outcome == 'success' }}
run: npm ci
- name: Cache PHPStan result cache
if: ${{ !cancelled() && steps.install.outcome == 'success' }}
uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
@@ -133,6 +142,10 @@ jobs:
if: ${{ !cancelled() && steps.install.outcome == 'success' }}
run: composer lint
- name: Check Docker Compose formatting
if: ${{ !cancelled() && steps.install.outcome == 'success' }}
run: npm run lint:compose
- name: Run Rector
if: ${{ !cancelled() && steps.install.outcome == 'success' }}
run: composer refactor:check
+1 -1
View File
@@ -1,7 +1,7 @@
name: "SDK Preview"
env:
COMPOSE_FILE: docker-compose.yml
COMPOSE_FILE: docker-compose.yml:docker-compose.override.yml
on:
workflow_dispatch:
+1 -1
View File
@@ -1,7 +1,7 @@
name: "Generate Specs"
env:
COMPOSE_FILE: docker-compose.yml
COMPOSE_FILE: docker-compose.yml:docker-compose.override.yml
on:
workflow_dispatch:
File diff suppressed because it is too large. Load diff
+455 -36
View File
@@ -1,10 +1,387 @@
# Dev tools for local development only.
# This file is automatically loaded by `docker compose` alongside docker-compose.yml.
# CI sets COMPOSE_FILE=docker-compose.yml explicitly, so these services are excluded from test runs.
services:
appwrite:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./docker-compose.yml:/usr/src/code/docker-compose.yml
- ./.env:/usr/src/code/.env
- ./phpunit.xml:/usr/src/code/phpunit.xml
- ./tests:/usr/src/code/tests
- ./app:/usr/src/code/app
- ./docs:/usr/src/code/docs
- ./public:/usr/src/code/public
- ./src:/usr/src/code/src
- ./dev:/usr/src/code/dev
depends_on:
- coredns
ports:
- 9501:80
dns:
- 127.0.0.11
- 172.16.238.100
entrypoint:
- php
- -e
- app/http.php
environment:
- _APP_MESSAGE_SMS_TEST_DSN
- _APP_MESSAGE_EMAIL_TEST_DSN
- _APP_MESSAGE_PUSH_TEST_DSN
- _TESTS_OAUTH2_GITHUB_CLIENT_ID
- _TESTS_OAUTH2_GITHUB_CLIENT_SECRET
appwrite-realtime:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
ports:
- 9502:80
appwrite-worker-audits:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
appwrite-worker-webhooks:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
depends_on:
- request-catcher-sms
- request-catcher-webhook
appwrite-worker-deletes:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
appwrite-worker-databases:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
appwrite-worker-builds:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
appwrite-worker-screenshots:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
appwrite-worker-certificates:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
appwrite-worker-executions:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
appwrite-worker-functions:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
appwrite-worker-mails:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
depends_on:
- maildev
appwrite-worker-notifications:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
depends_on:
- maildev
appwrite-worker-messaging:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
appwrite-worker-migrations:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
- ./tests:/usr/src/code/tests
appwrite-task-maintenance:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
appwrite-task-interval:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
appwrite-task-stats-resources:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
appwrite-worker-stats-resources:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
appwrite-worker-stats-usage:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
appwrite-task-scheduler-functions:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
appwrite-task-scheduler-executions:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
appwrite-task-scheduler-messages:
build:
context: .
target: development
args:
DEBUG: false
TESTING: true
VERSION: dev
image: appwrite-dev
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
coredns:
image: coredns/coredns:1.12.4
container_name: appwrite-coredns
restart: unless-stopped
logging:
driver: json-file
options:
max-file: "5"
max-size: 10m
command:
- -conf
- /mnt/resources/Corefile
networks:
appwrite:
ipv4_address: 172.16.238.100
volumes:
- ./tests/resources/coredns:/mnt/resources:ro
maildev:
image: appwrite/mailcatcher:1.1.1
container_name: appwrite-mailcatcher
logging:
driver: json-file
options:
max-file: "5"
max-size: 10m
ports:
- 9503:1080
networks:
- appwrite
- gateway
labels:
- traefik.enable=true
- traefik.constraint-label-stack=appwrite
- traefik.docker.network=gateway
- traefik.http.services.appwrite_maildev.loadbalancer.server.port=1080
- traefik.http.routers.appwrite_maildev_http.entrypoints=appwrite_web
- traefik.http.routers.appwrite_maildev_http.rule=Host(`mail.localhost`)
- traefik.http.routers.appwrite_maildev_http.service=appwrite_maildev
- traefik.http.routers.appwrite_maildev_https.entrypoints=appwrite_websecure
- traefik.http.routers.appwrite_maildev_https.rule=Host(`mail.localhost`)
- traefik.http.routers.appwrite_maildev_https.service=appwrite_maildev
- traefik.http.routers.appwrite_maildev_https.tls=true
environment:
- MAILDEV_INCOMING_USER=${_APP_SMTP_USERNAME}
- MAILDEV_INCOMING_PASS=${_APP_SMTP_PASSWORD}
request-catcher-webhook:
image: appwrite/requestcatcher:1.1.0
container_name: appwrite-requestcatcher-webhook
logging:
driver: json-file
options:
max-file: "5"
max-size: 10m
ports:
- 9504:5000
networks:
- appwrite
request-catcher-sms:
image: appwrite/requestcatcher:1.1.0
container_name: appwrite-requestcatcher-sms
logging:
driver: json-file
options:
max-file: "5"
max-size: 10m
ports:
- 9507:5000
networks:
- appwrite
appwrite-mongo-express:
profiles: ["mongodb"]
profiles:
- mongodb
image: mongo-express
container_name: appwrite-mongo-express
networks:
@@ -12,13 +389,12 @@ services:
ports:
- "9508:8081"
environment:
ME_CONFIG_MONGODB_URL: "mongodb://root:${_APP_DB_ROOT_PASS}@appwrite-mongodb:27017/?replicaSet=rs0&directConnection=true"
ME_CONFIG_MONGODB_URL: mongodb://root:${_APP_DB_ROOT_PASS}@appwrite-mongodb:27017/?replicaSet=rs0&directConnection=true
ME_CONFIG_BASICAUTH_USERNAME: ${_APP_DB_USER}
ME_CONFIG_BASICAUTH_PASSWORD: ${_APP_DB_PASS}
depends_on:
mongodb:
condition: service_healthy
adminer:
image: adminer
container_name: appwrite-adminer
@@ -37,20 +413,19 @@ services:
configs:
- source: adminer-index.php
target: /var/www/html/index.php
mode: 0755
mode: 493
labels:
- "traefik.enable=true"
- "traefik.constraint-label-stack=appwrite"
- "traefik.docker.network=gateway"
- "traefik.http.services.appwrite_adminer.loadbalancer.server.port=8080"
- "traefik.http.routers.appwrite_adminer_http.entrypoints=appwrite_web"
- "traefik.http.routers.appwrite_adminer_http.rule=Host(`mysql.localhost`)"
- "traefik.http.routers.appwrite_adminer_http.service=appwrite_adminer"
- "traefik.http.routers.appwrite_adminer_https.entrypoints=appwrite_websecure"
- "traefik.http.routers.appwrite_adminer_https.rule=Host(`mysql.localhost`)"
- "traefik.http.routers.appwrite_adminer_https.service=appwrite_adminer"
- "traefik.http.routers.appwrite_adminer_https.tls=true"
- traefik.enable=true
- traefik.constraint-label-stack=appwrite
- traefik.docker.network=gateway
- traefik.http.services.appwrite_adminer.loadbalancer.server.port=8080
- traefik.http.routers.appwrite_adminer_http.entrypoints=appwrite_web
- traefik.http.routers.appwrite_adminer_http.rule=Host(`mysql.localhost`)
- traefik.http.routers.appwrite_adminer_http.service=appwrite_adminer
- traefik.http.routers.appwrite_adminer_https.entrypoints=appwrite_websecure
- traefik.http.routers.appwrite_adminer_https.rule=Host(`mysql.localhost`)
- traefik.http.routers.appwrite_adminer_https.service=appwrite_adminer
- traefik.http.routers.appwrite_adminer_https.tls=true
redis-insight:
image: redis/redisinsight:latest
restart: unless-stopped
@@ -62,22 +437,21 @@ services:
configs:
- source: redisinsight-connections.json
target: /mnt/connections.json
mode: 0755
mode: 493
labels:
- "traefik.enable=true"
- "traefik.constraint-label-stack=appwrite"
- "traefik.docker.network=gateway"
- "traefik.http.services.appwrite_redisinsight.loadbalancer.server.port=5540"
- "traefik.http.routers.appwrite_redisinsight_http.entrypoints=appwrite_web"
- "traefik.http.routers.appwrite_redisinsight_http.rule=Host(`redis.localhost`)"
- "traefik.http.routers.appwrite_redisinsight_http.service=appwrite_redisinsight"
- "traefik.http.routers.appwrite_redisinsight_https.entrypoints=appwrite_websecure"
- "traefik.http.routers.appwrite_redisinsight_https.rule=Host(`redis.localhost`)"
- "traefik.http.routers.appwrite_redisinsight_https.service=appwrite_redisinsight"
- "traefik.http.routers.appwrite_redisinsight_https.tls=true"
- traefik.enable=true
- traefik.constraint-label-stack=appwrite
- traefik.docker.network=gateway
- traefik.http.services.appwrite_redisinsight.loadbalancer.server.port=5540
- traefik.http.routers.appwrite_redisinsight_http.entrypoints=appwrite_web
- traefik.http.routers.appwrite_redisinsight_http.rule=Host(`redis.localhost`)
- traefik.http.routers.appwrite_redisinsight_http.service=appwrite_redisinsight
- traefik.http.routers.appwrite_redisinsight_https.entrypoints=appwrite_websecure
- traefik.http.routers.appwrite_redisinsight_https.rule=Host(`redis.localhost`)
- traefik.http.routers.appwrite_redisinsight_https.service=appwrite_redisinsight
- traefik.http.routers.appwrite_redisinsight_https.tls=true
ports:
- "9514:5540"
graphql-explorer:
container_name: appwrite-graphql-explorer
image: appwrite/altair:0.3.0
@@ -85,10 +459,51 @@ services:
networks:
- appwrite
ports:
- "9509:3000"
- 9509:3000
environment:
- SERVER_URL=http://localhost:9520/v1/graphql
traefik:
command:
- --providers.file.directory=/storage/config
- --providers.file.watch=true
- --providers.docker=true
- --providers.docker.exposedByDefault=false
- --providers.docker.constraints=Label(`traefik.constraint-label-stack`,`appwrite`)
- --entrypoints.appwrite_web.address=:80
- --entrypoints.appwrite_websecure.address=:443
- --log.level=DEBUG
- --api.insecure=true
- --accesslog=true
ports:
- 9520:80
- 9522:80
- 9521:443
- 9500:8080
networks:
appwrite:
aliases:
- appwrite.test
gateway:
aliases:
- appwrite.test
runtimes:
aliases:
- appwrite.test
mariadb:
ports:
- "9510:3306"
mongodb:
ports:
- "9511:27017"
postgresql:
ports:
- "9512:5432"
redis:
ports:
- "9513:6379"
appwrite-embedding:
ports:
- "9505:3000"
configs:
redisinsight-connections.json:
content: |
@@ -129,7 +544,6 @@ configs:
"tags": []
}
]
adminer-index.php:
content: |
<?php
@@ -143,3 +557,8 @@ configs:
];
}
include './adminer.php';
networks:
appwrite:
ipam:
config:
- subnet: 172.16.238.0/24
+255 -381
View File
File diff suppressed because it is too large. Load diff
+3 -3
View File
@@ -24,11 +24,11 @@ This information is also used to generate the website documentation at https://a
If the newly introduced environment variable has a default value, add it to the `.env` and `Dockerfile` along with other environment variables. `.env` file uses settings for Appwrite development environment.
### Add to Docker Compose file and template
### Add to Docker Compose file
Add the new environment variables to the `docker-compose.yml` and `app/views/install/compose.phtml` for each docker service that requires access to those environment variables.
Add the new environment variables to `docker-compose.yml` for each Docker service that requires access to those environment variables.
The `docker-compose.yml` file is used by the Appwrite maintainers during development, whereas the `app/views/install/compose.phtml` file is used by the Appwrite setup script.
The setup script generates the install compose file from `docker-compose.yml`, so changes should be made in the base compose file instead of a separate template. Run `npm run format:compose` after editing compose files.
With these steps, your environment variable is properly added and can be accessed inside Appwrite code and any other containers where it is passed. You can access and use those variables to implement the features you are trying to achieve.
+20 -1
View File
@@ -4,7 +4,26 @@
"requires": true,
"packages": {
"": {
"name": "@appwrite.io/repo"
"name": "@appwrite.io/repo",
"devDependencies": {
"prettier": "^3.9.0"
}
},
"node_modules/prettier": {
"version": "3.9.0",
"resolved": "https://registry.npmjs.org/prettier/-/prettier-3.9.0.tgz",
"integrity": "sha512-LjIqSIC5VYLzs9WedVmJ2ljNAGnU+DteIClbahu4L/DBeWjZ6iT/k1lAYyu9JUh+1xINxWadaPw/Pl63y/agAw==",
"dev": true,
"license": "MIT",
"bin": {
"prettier": "bin/prettier.cjs"
},
"engines": {
"node": ">=14"
},
"funding": {
"url": "https://github.com/prettier/prettier?sponsor=1"
}
}
}
}
+8 -1
View File
@@ -5,8 +5,15 @@
"type": "git",
"url": "git+https://github.com/appwrite/appwrite.git"
},
"scripts": {
"format:compose": "prettier --write docker-compose.yml docker-compose.override.yml",
"lint:compose": "prettier --check docker-compose.yml docker-compose.override.yml"
},
"files": [
"docs/examples",
"app/config/specs"
]
],
"devDependencies": {
"prettier": "^3.9.0"
}
}
+290
View File
@@ -0,0 +1,290 @@
<?php
namespace Appwrite\Docker\Compose;
use Exception;
class Generator
{
private const array SELECTABLE_SERVICE_GROUPS = [
'database' => [
'default' => 'mongodb',
'services' => [
'mariadb',
'mongodb',
'postgresql',
],
],
];
private const array SELECTABLE_VOLUME_GROUPS = [
'database' => [
'mariadb' => ['appwrite-mariadb'],
'mongodb' => ['appwrite-mongodb', 'appwrite-mongodb-keyfile'],
'postgresql' => ['appwrite-postgresql'],
],
];
private const array OPTIONAL_SERVICES = [
'enableAssistant' => 'appwrite-assistant',
];
private const array LOCAL_VOLUME_PREPENDS = [
'appwrite' => [
'param' => 'hostPath',
'volume' => '{hostPath}:/usr/src/code:rw',
],
];
private const array PARAM_DEFAULTS = [
'version' => 'latest',
'database' => 'mongodb',
'hostPath' => '',
'enableAssistant' => false,
];
private const array DEPENDENCY_SELECTORS = [
'database' => [
'param' => 'database',
'condition' => [
'condition' => 'service_healthy',
],
'placeholders' => [
'${_APP_DB_HOST:-mongodb}',
'${_APP_DB_HOST:-mariadb}',
],
],
];
private const array YAML_DOCUMENT_MARKER_PATTERNS = [
'/^---\R/',
'/\R\\.\\.\\.\R?$/',
];
/**
* @var array<string, mixed>
*/
private array $compose;
/**
* @var array<string, mixed>
*/
private array $params = [];
private array $selectableServices = [
'mariadb',
'mongodb',
'postgresql',
];
public function __construct(string $compose)
{
$parsed = \yaml_parse($compose);
if (!\is_array($parsed)) {
throw new Exception('Failed to parse Docker Compose file');
}
$this->compose = $parsed;
}
/**
* @param array<string, mixed> $params
*/
public function render(array $params): string
{
$compose = $this->compose;
$this->params = $this->normalizeParams($params);
$this->selectableServices = $this->getSelectableServices();
$compose['services'] = $this->filterServices($compose['services'] ?? []);
$compose['volumes'] = $this->filterVolumes($compose['volumes'] ?? []);
foreach ($compose['services'] as $name => &$service) {
if (!\is_array($service)) {
continue;
}
$this->rewriteDependencies($service);
$this->rewriteLocalVolumes($name, $service);
}
unset($service);
return $this->emit($compose);
}
/**
* @param array<string, mixed> $params
* @return array<string, mixed>
*/
private function normalizeParams(array $params): array
{
$params = \array_merge(self::PARAM_DEFAULTS, $params);
foreach (self::SELECTABLE_SERVICE_GROUPS as $param => $config) {
if (!\in_array($params[$param], $config['services'], true)) {
$params[$param] = $config['default'];
}
}
$params['hostPath'] = \rtrim((string)$params['hostPath'], '/');
return $params;
}
/**
* @return string[]
*/
private function getSelectableServices(): array
{
$services = [];
foreach (self::SELECTABLE_SERVICE_GROUPS as $config) {
$services = \array_merge($services, $config['services']);
}
return \array_values(\array_unique($services));
}
/**
* @param array<string, mixed> $services
* @return array<string, mixed>
*/
private function filterServices(array $services): array
{
foreach (self::SELECTABLE_SERVICE_GROUPS as $param => $config) {
foreach ($config['services'] as $service) {
if ($service !== $this->params[$param]) {
unset($services[$service]);
}
}
}
foreach (self::OPTIONAL_SERVICES as $param => $service) {
if (empty($this->params[$param])) {
unset($services[$service]);
}
}
return $services;
}
/**
* @param array<string, mixed> $volumes
* @return array<string, mixed>
*/
private function filterVolumes(array $volumes): array
{
foreach (self::SELECTABLE_VOLUME_GROUPS as $param => $groups) {
foreach ($groups as $service => $names) {
if ($service === $this->params[$param]) {
continue;
}
foreach ($names as $name) {
unset($volumes[$name]);
}
}
}
return $volumes;
}
/**
* @param array<string, mixed> $service
*/
private function rewriteDependencies(array &$service): void
{
if (!isset($service['depends_on'])) {
return;
}
foreach (self::DEPENDENCY_SELECTORS as $selector) {
$selected = $this->params[$selector['param']];
$removable = [
...$this->selectableServices,
...$selector['placeholders'],
];
if (\array_is_list($service['depends_on'])) {
$hasSelectedDependency = false;
$dependsOn = \array_values(\array_filter(
$service['depends_on'],
function (mixed $dependency) use ($removable, &$hasSelectedDependency): bool {
if (!\in_array($dependency, $removable, true)) {
return true;
}
$hasSelectedDependency = true;
return false;
}
));
if ($hasSelectedDependency && !\in_array($selected, $dependsOn, true)) {
$dependsOn[] = $selected;
}
$service['depends_on'] = $dependsOn;
continue;
}
$hasSelectedDependency = false;
foreach ($removable as $dependency) {
if (\array_key_exists($dependency, $service['depends_on'])) {
$hasSelectedDependency = true;
}
unset($service['depends_on'][$dependency]);
}
if ($hasSelectedDependency) {
$service['depends_on'][$selected] = $selector['condition'];
}
}
}
/**
* @param array<string, mixed> $service
*/
private function rewriteLocalVolumes(string $name, array &$service): void
{
if ($this->params['version'] !== 'local' || !isset(self::LOCAL_VOLUME_PREPENDS[$name])) {
return;
}
$config = self::LOCAL_VOLUME_PREPENDS[$name];
if (empty($this->params[$config['param']])) {
return;
}
$service['volumes'] ??= [];
\array_unshift($service['volumes'], $this->replacePlaceholders($config['volume']));
}
/**
* @param array<string, mixed> $compose
*/
private function emit(array $compose): string
{
$yaml = \yaml_emit($compose, YAML_UTF8_ENCODING, YAML_LN_BREAK);
foreach (self::YAML_DOCUMENT_MARKER_PATTERNS as $pattern) {
$yaml = \preg_replace($pattern, '', $yaml) ?? $yaml;
}
return $yaml;
}
private function replacePlaceholders(string $value): string
{
foreach ($this->params as $param => $replacement) {
if (\is_scalar($replacement)) {
$value = \str_replace('{' . $param . '}', (string)$replacement, $value);
}
}
return $value;
}
}
+11 -2
View File
@@ -52,6 +52,10 @@ class Server
private const string DEFAULT_IMAGE = 'appwrite-dev';
public const string DEFAULT_CONTAINER = 'appwrite-installer';
private const string COMPOSE_FILE = 'docker-compose.yml';
private const string ENV_FILE = '.env';
private const string LOCAL_COMPOSE_FILE = 'docker-compose.web-installer.yml';
private const string LOCAL_ENV_FILE = '.env.web-installer';
private State $state;
private array $paths = [];
@@ -208,8 +212,13 @@ class Server
}
$basePath = $config->isLocal() ? '/usr/src/code' : (getcwd() ?: '.');
$composePath = $basePath . '/docker-compose.yml';
$envPath = $basePath . '/.env';
if ($config->isLocal()) {
$composePath = $basePath . '/' . self::LOCAL_COMPOSE_FILE;
$envPath = $basePath . '/' . self::LOCAL_ENV_FILE;
} else {
$composePath = $basePath . '/' . self::COMPOSE_FILE;
$envPath = $basePath . '/' . self::ENV_FILE;
}
if (!file_exists($composePath) && !file_exists($envPath)) {
return;
+51 -25
View File
@@ -3,6 +3,7 @@
namespace Appwrite\Platform\Tasks;
use Appwrite\Docker\Compose;
use Appwrite\Docker\Compose\Generator;
use Appwrite\Docker\Env;
use Appwrite\Platform\Installer\Runtime\State;
use Appwrite\Platform\Installer\Server as InstallerServer;
@@ -532,7 +533,12 @@ class Install extends Action
}
$templateForEnv = new View($this->buildFromProjectPath('/app/views/install/env.phtml'));
$templateForCompose = new View($this->buildFromProjectPath('/app/views/install/compose.phtml'));
$composePath = $this->buildFromProjectPath('/docker-compose.yml');
$composeYaml = \file_get_contents($composePath);
if ($composeYaml === false) {
throw new \RuntimeException('Failed to read docker-compose.yml from ' . $composePath);
}
$composeGenerator = new Generator($composeYaml);
$database = $input['_APP_DB_ADAPTER'] ?? 'mongodb';
@@ -557,16 +563,18 @@ class Install extends Action
}
$executorImages = \implode(',', \array_unique($runtimeImages));
$templateForCompose
->setParam('httpPort', $httpPort)
->setParam('httpsPort', $httpsPort)
->setParam('version', $version)
->setParam('organization', $organization)
->setParam('image', $image)
->setParam('database', $database)
->setParam('hostPath', $this->hostPath)
->setParam('enableAssistant', $enableAssistant)
->setParam('executorImages', $executorImages);
$input['_APP_HTTP_PORT'] = $httpPort;
$input['_APP_HTTPS_PORT'] = $httpsPort;
$input['_APP_IMAGE'] = "{$organization}/{$image}";
$input['_APP_VERSION'] = $version;
$input['_APP_EXECUTOR_IMAGES'] = $executorImages;
$composeContent = $composeGenerator->render([
'version' => $version,
'database' => $database,
'hostPath' => $this->hostPath,
'enableAssistant' => $enableAssistant,
]);
$templateForEnv->setParam('vars', $input);
@@ -604,7 +612,7 @@ class Install extends Action
$this->updateProgress($progress, InstallerServer::STEP_DOCKER_COMPOSE, InstallerServer::STATUS_IN_PROGRESS, $messages);
if (!$useExistingConfig) {
$this->writeComposeFile($templateForCompose);
$this->writeComposeFile($composeContent);
}
$this->updateProgress($progress, InstallerServer::STEP_DOCKER_COMPOSE, InstallerServer::STATUS_COMPLETED, $messages);
@@ -623,7 +631,7 @@ class Install extends Action
}
if ($database === 'mongodb' && !$useExistingConfig && $startIndex <= 1) {
$this->copyMongoEntrypointIfNeeded();
$this->copyMongoFilesIfNeeded();
}
if (!$noStart) {
@@ -639,7 +647,7 @@ class Install extends Action
}
}
if (!$isLocalInstall || file_exists('/.dockerenv') || file_exists('/run/.containerenv')) {
if (!$isLocalInstall && (file_exists('/.dockerenv') || file_exists('/run/.containerenv'))) {
$this->connectInstallerToAppwriteNetwork();
}
@@ -919,10 +927,9 @@ class Install extends Action
$healthPath = '/v1/health/version';
if ($isLocalInstall) {
$candidates = [
self::APPWRITE_API_URL . $healthPath,
'http://localhost:' . $httpPort . $healthPath,
];
$candidates = (file_exists('/.dockerenv') || file_exists('/run/.containerenv'))
? ['http://host.docker.internal:' . $httpPort . $healthPath]
: ['http://localhost:' . $httpPort . $healthPath];
} else {
$candidates = [
self::APPWRITE_API_URL . $healthPath,
@@ -1079,11 +1086,10 @@ class Install extends Action
];
}
private function writeComposeFile(View $template): void
private function writeComposeFile(string $renderedContent): void
{
$composeFileName = $this->getComposeFileName();
$targetPath = $this->path . '/' . $composeFileName;
$renderedContent = $template->render(false);
$result = @file_put_contents($targetPath, $renderedContent);
if ($result === false) {
@@ -1101,13 +1107,23 @@ class Install extends Action
}
}
private function copyMongoEntrypointIfNeeded(): void
private function copyMongoFilesIfNeeded(): void
{
$mongoEntrypoint = $this->buildFromProjectPath('/mongo-entrypoint.sh');
$files = [
'mongo-entrypoint.sh',
'mongo-init.js',
];
if (file_exists($mongoEntrypoint)) {
// Always use container path for file operations
copy($mongoEntrypoint, $this->path . '/mongo-entrypoint.sh');
foreach ($files as $file) {
$source = $this->buildFromProjectPath('/' . $file);
if (file_exists($source)) {
$target = $this->path . '/' . $file;
if (@copy($source, $target) === false) {
$lastError = error_get_last();
$errorMsg = $lastError ? $lastError['message'] : 'Unknown error';
throw new \RuntimeException('Failed to copy ' . $file . ' to ' . $target . ': ' . $errorMsg);
}
}
}
}
@@ -1153,6 +1169,16 @@ class Install extends Action
$command[] = '--project-directory';
$command[] = $composePath;
$validateCommand = $command;
$validateCommand[] = 'config';
$validateCommand[] = '--quiet';
\exec($env . implode(' ', array_map(escapeshellarg(...), $validateCommand)) . ' 2>&1', $validateOutput, $validateExit);
if ($validateExit !== 0) {
$message = trim(implode("\n", $validateOutput));
throw new \RuntimeException('Invalid Docker Compose file', 0, $message !== '' ? new \RuntimeException($message) : null);
}
$command[] = 'up';
$command[] = '-d';
$command[] = '--remove-orphans';
+134
View File
@@ -0,0 +1,134 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Docker\Compose;
use Appwrite\Docker\Compose\Generator;
use PHPUnit\Framework\TestCase;
final class GeneratorTest extends TestCase
{
private Generator $generator;
public function setUp(): void
{
$compose = \file_get_contents(__DIR__ . '/../../../../docker-compose.yml');
$this->assertIsString($compose);
$this->generator = new Generator($compose);
}
public function testSelectsDatabaseService(): void
{
$compose = $this->render([
'database' => 'mariadb',
'enableAssistant' => false,
]);
$this->assertArrayHasKey('mariadb', $compose['services']);
$this->assertArrayNotHasKey('mongodb', $compose['services']);
$this->assertArrayNotHasKey('postgresql', $compose['services']);
$this->assertArrayHasKey('appwrite-mariadb', $compose['volumes']);
$this->assertArrayNotHasKey('appwrite-mongodb', $compose['volumes']);
$this->assertArrayNotHasKey('appwrite-postgresql', $compose['volumes']);
}
public function testTogglesAssistantService(): void
{
$disabled = $this->render([
'enableAssistant' => false,
]);
$enabled = $this->render([
'enableAssistant' => true,
]);
$this->assertArrayNotHasKey('appwrite-assistant', $disabled['services']);
$this->assertArrayHasKey('appwrite-assistant', $enabled['services']);
}
public function testKeepsProductionWorkers(): void
{
$compose = $this->render();
$this->assertArrayHasKey('appwrite-worker-screenshots', $compose['services']);
$this->assertArrayHasKey('appwrite-task-interval', $compose['services']);
$this->assertArrayHasKey('appwrite-embedding', $compose['services']);
}
public function testKeepsMongoInitFiles(): void
{
$compose = $this->render([
'database' => 'mongodb',
]);
$this->assertContains('./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro', $compose['services']['mongodb']['volumes']);
$this->assertContains('./mongo-entrypoint.sh:/mongo-entrypoint.sh:ro', $compose['services']['mongodb']['volumes']);
}
public function testAddsLocalHostPathMount(): void
{
$compose = $this->render([
'version' => 'local',
'hostPath' => '/tmp/appwrite',
]);
$this->assertSame('/tmp/appwrite:/usr/src/code:rw', $compose['services']['appwrite']['volumes'][0]);
}
public function testDoesNotAddDatabaseDependencyWithoutPlaceholder(): void
{
$compose = $this->render([
'database' => 'postgresql',
]);
$this->assertSame(['appwrite'], $compose['services']['traefik']['depends_on']);
$this->assertContains('postgresql', $compose['services']['appwrite']['depends_on']);
$this->assertNotContains('${_APP_DB_HOST:-mongodb}', $compose['services']['appwrite']['depends_on']);
}
public function testKeepsLongCommandsReadable(): void
{
$mariadb = $this->render([
'database' => 'mariadb',
]);
$postgresql = $this->render([
'database' => 'postgresql',
]);
$this->assertSame(['mysqld', '--innodb-flush-method=fsync'], $mariadb['services']['mariadb']['command']);
$this->assertSame([
'postgres',
'-c',
'fsync=off',
'-c',
'synchronous_commit=off',
'-c',
'full_page_writes=off',
], $postgresql['services']['postgresql']['command']);
$this->assertSame([
'redis-server',
'--maxmemory',
'512mb',
'--maxmemory-policy',
'allkeys-lru',
'--maxmemory-samples',
'5',
], $postgresql['services']['redis']['command']);
}
/**
* @param array<string, mixed> $params
* @return array<string, mixed>
*/
private function render(array $params = []): array
{
$yaml = $this->generator->render($params);
$compose = \yaml_parse($yaml);
$this->assertIsArray($compose);
return $compose;
}
}