diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..17688f6 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,43 @@ +# Git files +.git +.gitignore +.github + +# Documentation +README.md +AUTHORS +LICENSE +*.md + +# Development files +vagrant/ +INSTALL/ +.idea +.vscode +.DS_Store + +# Dependencies (will be installed in container) +vendor/ +node_modules/ + +# Data directories (will be mounted as volumes) +data/ + +# Build artifacts +public/css/ +public/js/ +public/img/ +public/flags/ +public/views/ + +# Logs +*.log +npm-debug.log + +# Cache +*.cache +.docker-initialized + +# Environment files (should be configured separately) +.env +.env.dev diff --git a/.env.dev b/.env.dev new file mode 100644 index 0000000..fc3c352 --- /dev/null +++ b/.env.dev @@ -0,0 +1,16 @@ +# MariaDB Configuration +DBPASSWORD_ADMIN=root +DBNAME_COMMON=monarc_common +DBNAME_MASTER=monarc_master +DBUSER_MONARC=sqlmonarcuser +DBPASSWORD_MONARC=sqlmonarcuser + +# Optional: set to 0/false/no to disable Xdebug in the image build +XDEBUG_ENABLED=1 +XDEBUG_MODE=debug +XDEBUG_START_WITH_REQUEST=trigger +# On Linux, set XDEBUG_CLIENT_HOST to your Docker bridge (often 172.17.0.1). +XDEBUG_CLIENT_HOST=host.docker.internal +XDEBUG_CLIENT_PORT=9003 +XDEBUG_IDEKEY=IDEKEY +XDEBUG_DISCOVER_CLIENT_HOST=0 diff --git a/.gitignore b/.gitignore index 487c18a..ee954a3 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,6 @@ bin/ data/* vagrant/.vagrant/ vagrant/*.log +.env +.docker-initialized +docker/db_data diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..f9d4bc9 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,58 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Attach to Chrome", + "port": 9222, + "request": "attach", + "type": "chrome", + "webRoot": "${workspaceFolder}" + }, + { + "name": "Listen for Xdebug", + "type": "php", + "request": "launch", + "port": 9003, + "pathMappings": { + "/var/www/html/monarc": "${workspaceFolder}" + } + }, + { + "name": "Launch currently open script", + "type": "php", + "request": "launch", + "program": "${file}", + "cwd": "${fileDirname}", + "port": 0, + "runtimeArgs": [ + "-dxdebug.start_with_request=yes" + ], + "env": { + "XDEBUG_MODE": "debug,develop", + "XDEBUG_CONFIG": "client_port=${port}" + } + }, + { + "name": "Launch Built-in web server", + "type": "php", + "request": "launch", + "runtimeArgs": [ + "-dxdebug.mode=debug", + "-dxdebug.start_with_request=yes", + "-S", + "localhost:0" + ], + "program": "", + "cwd": "${workspaceRoot}", + "port": 9003, + "serverReadyAction": { + "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started", + "uriFormat": "http://localhost:%s", + "action": "openExternally" + } + } + ] +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0bb5eb9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,112 @@ +FROM ubuntu:22.04 + +ARG XDEBUG_ENABLED=1 +ARG XDEBUG_MODE=debug +ARG XDEBUG_START_WITH_REQUEST=trigger +ARG XDEBUG_CLIENT_HOST=host.docker.internal +ARG XDEBUG_CLIENT_PORT=9003 +ARG XDEBUG_IDEKEY=IDEKEY +ARG XDEBUG_DISCOVER_CLIENT_HOST=0 + +# Prevent interactive prompts during package installation +ENV DEBIAN_FRONTEND=noninteractive +ENV LANGUAGE=en_US.UTF-8 +ENV LANG=en_US.UTF-8 +ENV LC_ALL=en_US.UTF-8 + +# Install system dependencies +RUN apt-get update && apt-get upgrade -y && \ + packages="vim zip unzip git gettext curl gsfonts mariadb-client apache2 php8.1 php8.1-cli \ + php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1-mbstring php8.1-curl php8.1-xml \ + php8.1-bcmath php8.1-intl php8.1-imagick locales wget ca-certificates gnupg"; \ + if [ "$XDEBUG_ENABLED" = "1" ] || [ "$XDEBUG_ENABLED" = "true" ] || [ "$XDEBUG_ENABLED" = "yes" ]; then \ + packages="$packages php8.1-xdebug"; \ + fi; \ + apt-get install -y $packages && \ + locale-gen en_US.UTF-8 && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* + +# Configure PHP +RUN sed -i 's/upload_max_filesize = .*/upload_max_filesize = 200M/' /etc/php/8.1/apache2/php.ini && \ + sed -i 's/post_max_size = .*/post_max_size = 50M/' /etc/php/8.1/apache2/php.ini && \ + sed -i 's/max_execution_time = .*/max_execution_time = 100/' /etc/php/8.1/apache2/php.ini && \ + sed -i 's/max_input_time = .*/max_input_time = 223/' /etc/php/8.1/apache2/php.ini && \ + sed -i 's/memory_limit = .*/memory_limit = 512M/' /etc/php/8.1/apache2/php.ini && \ + sed -i 's/session\\.gc_maxlifetime = .*/session.gc_maxlifetime = 604800/' /etc/php/8.1/apache2/php.ini && \ + sed -i 's/session\\.gc_probability = .*/session.gc_probability = 1/' /etc/php/8.1/apache2/php.ini && \ + sed -i 's/session\\.gc_divisor = .*/session.gc_divisor = 1000/' /etc/php/8.1/apache2/php.ini + +# Configure Xdebug for development +RUN if [ "$XDEBUG_ENABLED" = "1" ] || [ "$XDEBUG_ENABLED" = "true" ] || [ "$XDEBUG_ENABLED" = "yes" ]; then \ + echo "zend_extension=xdebug.so" > /etc/php/8.1/apache2/conf.d/20-xdebug.ini && \ + echo "xdebug.mode=${XDEBUG_MODE}" >> /etc/php/8.1/apache2/conf.d/20-xdebug.ini && \ + echo "xdebug.start_with_request=${XDEBUG_START_WITH_REQUEST}" >> /etc/php/8.1/apache2/conf.d/20-xdebug.ini && \ + echo "xdebug.client_host=${XDEBUG_CLIENT_HOST}" >> /etc/php/8.1/apache2/conf.d/20-xdebug.ini && \ + echo "xdebug.client_port=${XDEBUG_CLIENT_PORT}" >> /etc/php/8.1/apache2/conf.d/20-xdebug.ini && \ + echo "xdebug.discover_client_host=${XDEBUG_DISCOVER_CLIENT_HOST}" >> /etc/php/8.1/apache2/conf.d/20-xdebug.ini && \ + echo "xdebug.idekey=${XDEBUG_IDEKEY}" >> /etc/php/8.1/apache2/conf.d/20-xdebug.ini; \ + fi + +# Enable Apache modules +RUN a2enmod rewrite ssl headers + +# Set global ServerName to avoid AH00558 warning +RUN echo "ServerName localhost" > /etc/apache2/conf-available/servername.conf \ + && a2enconf servername + +# Install Composer +RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer + +# Install Node.js and npm +RUN mkdir -p /etc/apt/keyrings && \ + curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg && \ + echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" > /etc/apt/sources.list.d/nodesource.list && \ + apt-get update && \ + apt-get install -y nodejs && \ + npm install -g grunt-cli && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* + +# Set working directory +WORKDIR /var/www/html/monarc + +# Configure Apache +RUN echo '\n\ + ServerName localhost\n\ + DocumentRoot /var/www/html/monarc/public\n\ +\n\ + \n\ + DirectoryIndex index.php\n\ + AllowOverride All\n\ + Require all granted\n\ + \n\ +\n\ + \n\ + Header always set X-Content-Type-Options nosniff\n\ + Header always set X-XSS-Protection "1; mode=block"\n\ + Header always set X-Robots-Tag none\n\ + Header always set X-Frame-Options SAMEORIGIN\n\ + \n\ +\n\ + SetEnv APP_ENV development\n\ + SetEnv APP_DIR /var/www/html/monarc\n\ +' > /etc/apache2/sites-available/000-default.conf + +# Allow Apache override to all +RUN sed -i 's/AllowOverride None/AllowOverride All/g' /etc/apache2/apache2.conf + +# Create necessary directories +RUN mkdir -p /var/www/html/monarc/data/cache \ + /var/www/html/monarc/data/LazyServices/Proxy \ + /var/www/html/monarc/data/DoctrineORMModule/Proxy \ + /var/www/html/monarc/data/import/files + +# Copy entrypoint script +COPY docker-entrypoint.sh /usr/local/bin/ +RUN chmod +x /usr/local/bin/docker-entrypoint.sh + +EXPOSE 80 + +ENTRYPOINT ["docker-entrypoint.sh"] +CMD ["apachectl", "-D", "FOREGROUND"] diff --git a/INSTALL/INSTALL.docker.md b/INSTALL/INSTALL.docker.md new file mode 100644 index 0000000..ba4b6c7 --- /dev/null +++ b/INSTALL/INSTALL.docker.md @@ -0,0 +1,172 @@ +# Docker Installation Guide for MONARC BackOffice + +This document provides installation instructions for setting up MONARC BackOffice using Docker for development purposes. + +## Quick Installation + +```bash +# Clone the repository +git clone https://github.com/monarc-project/MonarcAppBO +cd MonarcAppBO + +# Start with Makefile (recommended) +make start +# Optional: use ENV to select docker-compose..yml (default: dev) +# make start ENV=prod + +# Or use docker-compose directly +cp .env.dev .env +docker compose -f docker-compose.dev.yml up -d --build +``` + +## What Gets Installed + +The Docker setup includes: + +1. **MONARC BackOffice Application** + - Ubuntu 22.04 base + - PHP 8.1 with all required extensions + - Apache web server + - Composer for PHP dependencies + - Node.js for frontend + - All MONARC modules and dependencies + +2. **MariaDB 10.11** + - Database for MONARC application data + - Pre-configured with proper character sets + +## First Time Setup + +When you start the environment for the first time: + +1. Docker images will be built (5-10 minutes) +2. Dependencies will be installed automatically +3. Databases will be created and initialized +4. Frontend repositories will be cloned and built + +## Access URLs + +After startup, access the application at: + +- **MONARC BackOffice**: http://localhost:5002 + +## Default Credentials + +The BackOffice uses the same authentication system as your configured instances. +You'll need to configure user accounts through the application. + +⚠️ **Security Warning**: Change these credentials before deploying to production! + +## System Requirements + +- Docker Engine 20.10 or later +- Docker Compose V2 +- At least 4GB RAM available for Docker +- At least 10GB free disk space +- Linux, macOS, or Windows with WSL2 + +## Configuration + +Environment variables are defined in the `.env` file (copied from `.env.dev`): + +- Database credentials +- Service ports + +You can customize these before starting the environment. + +## Troubleshooting + +### Port Conflicts + +If port 5002 or 3306 is already in use: + +1. Edit `docker-compose.dev.yml` +2. Change the host port (left side of the port mapping) +3. Example: Change `"5002:80"` to `"8002:80"` + +### Permission Issues + +If you encounter permission errors: + +```bash +docker exec -it monarc-bo-app bash +chown -R www-data:www-data /var/www/html/monarc/data +chmod -R 775 /var/www/html/monarc/data +``` + +### Service Not Starting + +Check the logs: + +```bash +make logs +# or +docker compose -f docker-compose.dev.yml logs +``` + +### Reset Everything + +To start completely fresh: + +```bash +make reset +# or +docker compose -f docker-compose.dev.yml down -v +``` + +## Common Tasks + +### View Logs +```bash +make logs +``` + +### Access Container Shell +```bash +make shell +``` + +### Access Database +```bash +make db +``` + +### Stop Services +```bash +make stop +``` + +### Restart Services +```bash +make restart +``` + +## Comparison with Other Installation Methods + +| Method | Complexity | Time | Best For | +|--------|-----------|------|----------| +| Docker | Low | Fast (2-5 min startup) | Development | +| Vagrant | Medium | Slow (10-15 min startup) | Development | +| Manual | High | Slow (30+ min) | Production | +| VM | Low | Medium | Testing | + +## Next Steps + +After installation: + +1. Read the [full Docker documentation](README.docker.md) +2. Review the [MONARC documentation](https://www.monarc.lu/documentation) +3. Configure your BackOffice instances +4. Start developing! + +## Getting Help + +- **Documentation**: [README.docker.md](README.docker.md) +- **MONARC Website**: https://www.monarc.lu +- **GitHub Issues**: https://github.com/monarc-project/MonarcAppBO/issues +- **Community**: https://www.monarc.lu/community + +## License + +This project is licensed under the GNU Affero General Public License version 3. +See [LICENSE](LICENSE) for details. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..50a0c07 --- /dev/null +++ b/Makefile @@ -0,0 +1,85 @@ +.DEFAULT_GOAL := help + +SHELL := /bin/bash + +ENV ?= dev +COMPOSE_FILE ?= docker-compose.$(ENV).yml +COMPOSE := docker compose -f $(COMPOSE_FILE) + +GREEN := \033[0;32m +YELLOW := \033[1;33m +RED := \033[0;31m +NC := \033[0m + +.PHONY: help check-env start stop restart logs logs-app shell db reset status + +help: + @printf "%b\n" "$(GREEN)MONARC BackOffice Docker Development Environment Manager$(NC)" + @printf "\n%b\n" "Usage: make " + @printf "%b\n" "Environment: ENV= (default: dev, uses docker-compose..yml)" + @printf "\n%b\n" "Commands:" + @printf " %-12s %s\n" "start" "Start all services (builds on first run)" + @printf " %-12s %s\n" "stop" "Stop all services" + @printf " %-12s %s\n" "restart" "Restart all services" + @printf " %-12s %s\n" "logs" "View logs from all services" + @printf " %-12s %s\n" "logs-app" "View logs from MONARC application" + @printf " %-12s %s\n" "shell" "Open a shell in the MONARC container" + @printf " %-12s %s\n" "db" "Open MySQL client in the database" + @printf " %-12s %s\n" "reset" "Reset everything (removes all data)" + @printf " %-12s %s\n" "status" "Show status of all services" + +check-env: + @if [ ! -f .env ]; then \ + printf "%b\n" "$(YELLOW)No .env file found. Creating from .env.dev...$(NC)"; \ + cp .env.dev .env; \ + printf "%b\n" "$(GREEN).env file created. You can edit it to customize configuration.$(NC)"; \ + fi + +start: check-env + @printf "%b\n" "$(GREEN)Starting MONARC BackOffice development environment...$(NC)" + @$(COMPOSE) up -d --build + @printf "%b\n" "$(GREEN)Services started!$(NC)" + @printf "%b\n" "MONARC BackOffice: http://localhost:5002" + @printf "\n%b\n" "$(YELLOW)To view logs: make logs ENV=$(ENV)$(NC)" + +stop: + @printf "%b\n" "$(YELLOW)Stopping all services...$(NC)" + @$(COMPOSE) stop + @printf "%b\n" "$(GREEN)Services stopped.$(NC)" + +restart: + @printf "%b\n" "$(YELLOW)Restarting all services...$(NC)" + @$(COMPOSE) restart + @printf "%b\n" "$(GREEN)Services restarted.$(NC)" + +logs: + @$(COMPOSE) logs -f + +logs-app: + @$(COMPOSE) logs -f monarc + +shell: + @printf "%b\n" "$(GREEN)Opening shell in MONARC container...$(NC)" + @docker exec -it monarc-bo-app bash + +db: + @printf "%b\n" "$(GREEN)Opening MySQL client...$(NC)" + @if [ -f .env ]; then \ + export $$(grep -v '^#' .env | xargs); \ + fi; \ + export MYSQL_PWD="$${DBPASSWORD_MONARC:-sqlmonarcuser}"; \ + docker exec -it monarc-bo-db mysql -u"$${DBUSER_MONARC:-sqlmonarcuser}" "$${DBNAME_COMMON:-monarc_common}" + +reset: + @printf "%b\n" "$(RED)WARNING: This will remove all data!$(NC)"; \ + read -p "Are you sure? (yes/no): " confirm; \ + if [ "$$confirm" = "yes" ]; then \ + printf "%b\n" "$(YELLOW)Stopping and removing all containers, volumes, and data...$(NC)"; \ + $(COMPOSE) down -v; \ + printf "%b\n" "$(GREEN)Reset complete. Run 'make start' to start fresh.$(NC)"; \ + else \ + printf "%b\n" "$(GREEN)Reset cancelled.$(NC)"; \ + fi + +status: + @$(COMPOSE) ps diff --git a/README.docker.md b/README.docker.md new file mode 100644 index 0000000..412d52a --- /dev/null +++ b/README.docker.md @@ -0,0 +1,367 @@ +# Docker Development Environment for MONARC BackOffice + +This guide explains how to set up a local development environment for MONARC BackOffice using Docker. + +## Prerequisites + +- Docker Engine 20.10 or later +- Docker Compose V2 (comes with Docker Desktop) +- At least 4GB of RAM available for Docker +- At least 10GB of free disk space + +## Quick Start + +### Option 1: Using the Makefile (Recommended) + +1. **Clone the repository** (if you haven't already): + ```bash + git clone https://github.com/monarc-project/MonarcAppBO + cd MonarcAppBO + ``` + +2. **Start the development environment**: + ```bash + make start + ``` + + This will automatically: + - Create `.env` file from `.env.dev` if it doesn't exist + - Build and start all services + - Display access URLs + + The first run will take several minutes as it: + - Builds the Docker images + - Installs all dependencies (PHP, Node.js) + - Clones frontend repositories + - Initializes databases + - Builds the frontend + +3. **Access the application**: + - MONARC BackOffice: http://localhost:5002 + +### Option 2: Using Docker Compose Directly + +1. **Clone the repository** (if you haven't already): + ```bash + git clone https://github.com/monarc-project/MonarcAppBO + cd MonarcAppBO + ``` + +2. **Copy the environment file**: + ```bash + cp .env.dev .env + ``` + +3. **Customize environment variables** (optional): + Edit `.env` file to change default passwords and configuration. + +4. **Start the development environment**: + ```bash + docker compose -f docker-compose.dev.yml up --build + ``` + +5. **Access the application**: + - MONARC BackOffice: http://localhost:5002 + +## Services + +The development environment includes the following services: + +| Service | Description | Port | Container Name | +|---------|-------------|------|----------------| +| monarc | Main BackOffice application (PHP/Apache) | 5002 | monarc-bo-app | +| db | MariaDB database | 3306 | monarc-bo-db | + +## Development Workflow + +### Makefile Commands + +The Makefile provides convenient commands for managing the development environment. +Use `ENV=` to select `docker-compose..yml` (default: `dev`). + +```bash +make start # Start all services +make stop # Stop all services +make restart # Restart all services +make logs # View logs from all services +make logs-app # View logs from MONARC application +make shell # Open a shell in the MONARC container +make db # Open MySQL client +make status # Show status of all services +make reset # Reset everything (removes all data) +``` + +### Live Code Editing + +The application source code is mounted as a volume, so changes you make on your host machine will be immediately reflected in the container. After making changes: + +1. **PHP/Backend changes**: Apache automatically reloads modified files +2. **Frontend changes**: You may need to rebuild the frontend: + ```bash + docker exec -it monarc-bo-app bash + cd /var/www/html/monarc + ./scripts/update-all.sh -d + ``` + +### Accessing the Container + +Using Makefile: +```bash +make shell +``` + +Or directly with docker: +```bash +docker exec -it monarc-bo-app bash +``` + +### Database Access + +Using Makefile: +```bash +make db # Connect to MariaDB +``` + +Or directly with docker: +```bash +# Connect to MariaDB +docker exec -it monarc-bo-db mysql -usqlmonarcuser -psqlmonarcuser monarc_common +``` + +### Viewing Logs + +Using Makefile: +```bash +make logs # All services +make logs-app # MONARC application only +``` + +Or directly with docker compose: +```bash +# View logs for all services +docker compose -f docker-compose.dev.yml logs -f + +# View logs for a specific service +docker compose -f docker-compose.dev.yml logs -f monarc +``` + +### Restarting Services + +Using Makefile: +```bash +make restart # Restart all services +``` + +Or directly with docker compose: +```bash +# Restart all services +docker compose -f docker-compose.dev.yml restart + +# Restart a specific service +docker compose -f docker-compose.dev.yml restart monarc +``` + +### Stopping the Environment + +Using Makefile: +```bash +make stop # Stop all services (keeps data) +make reset # Stop and remove everything including data +``` + +Or directly with docker compose: +```bash +# Stop all services (keeps data) +docker compose -f docker-compose.dev.yml stop + +# Stop and remove containers (keeps volumes/data) +docker compose -f docker-compose.dev.yml down + +# Stop and remove everything including data +docker compose -f docker-compose.dev.yml down -v +``` + +## Common Tasks + +### Resetting the Database + +To completely reset the databases: +```bash +docker compose -f docker-compose.dev.yml down -v +docker compose -f docker-compose.dev.yml up --build +``` + +### Installing New PHP Dependencies + +```bash +docker exec -it monarc-bo-app bash +composer require package/name +``` + +### Installing New Node Dependencies + +```bash +docker exec -it monarc-bo-app bash +cd node_modules/ng_backoffice # or ng_anr +npm install package-name +``` + +### Running Database Migrations + +```bash +docker exec -it monarc-bo-app bash +php ./vendor/robmorgan/phinx/bin/phinx migrate -c ./module/Monarc/BackOffice/migrations/phinx.php +``` + +### Rebuilding Frontend + +```bash +docker exec -it monarc-bo-app bash +cd /var/www/html/monarc +./scripts/update-all.sh -d +``` + +## Debugging + +### Xdebug Configuration + +Xdebug is enabled by default in the development image. To disable it, set +`XDEBUG_ENABLED=0` in `.env` and rebuild the image (for example, `make start`). +You can also tune the connection behavior via `.env`: +`XDEBUG_START_WITH_REQUEST`, `XDEBUG_CLIENT_HOST`, and `XDEBUG_CLIENT_PORT`. + +When enabled, Xdebug is pre-configured. To use it: + +1. Configure your IDE to listen on port 9003 +2. Set the IDE key to `IDEKEY` +3. Start debugging in your IDE +4. Trigger a request to the application + +For PhpStorm: +- Go to Settings → PHP → Debug +- Set Xdebug port to 9003 +- Enable "Can accept external connections" +- Set the path mappings: `/var/www/html/monarc` → your local project path + +### Checking Service Health + +```bash +# Check if all services are running +docker compose -f docker-compose.dev.yml ps + +# Check specific service health +docker compose -f docker-compose.dev.yml ps monarc +``` + +## Troubleshooting + +### Port Conflicts + +If you get port conflicts, you can change the ports in the `docker-compose.dev.yml` file: +```yaml +ports: + - "5002:80" # Change 5002 to another available port +``` + +### Permission Issues + +If you encounter permission issues with mounted volumes: +```bash +docker exec -it monarc-bo-app bash +chown -R www-data:www-data /var/www/html/monarc/data +chmod -R 775 /var/www/html/monarc/data +``` + +### Database Connection Issues + +Check if the database is healthy: +```bash +docker compose -f docker-compose.dev.yml ps db +``` + +If needed, restart the database: +```bash +docker compose -f docker-compose.dev.yml restart db +``` + +### Rebuilding from Scratch + +If something goes wrong and you want to start fresh: +```bash +# Stop everything +docker compose -f docker-compose.dev.yml down -v + +# Remove all related containers, images, and volumes +docker system prune -a + +# Rebuild and start +docker compose -f docker-compose.dev.yml up --build +``` + +## Performance Optimization + +For better performance on macOS and Windows: + +1. **Use Docker volume mounts for dependencies**: The compose file already uses named volumes for `vendor` and `node_modules` to improve performance. + +2. **Allocate more resources**: In Docker Desktop settings, increase: + - CPUs: 4 or more + - Memory: 4GB or more + - Swap: 1GB or more + +3. **Enable caching**: The Dockerfile uses apt cache and composer optimizations. + +## Comparison with Vagrant + +| Feature | Docker | Vagrant | +|---------|--------|---------| +| Startup time | Fast (~2-3 min) | Slow (~10-15 min) | +| Resource usage | Lower | Higher | +| Isolation | Container-level | VM-level | +| Portability | Excellent | Good | +| Live code reload | Yes | Yes | +| Learning curve | Moderate | Low | + +## Environment Variables Reference + +All environment variables are defined in the `.env` file: + +| Variable | Description | Default | +|----------|-------------|---------| +| `DBPASSWORD_ADMIN` | MariaDB root password | `root` | +| `DBNAME_COMMON` | Common database name | `monarc_common` | +| `DBNAME_MASTER` | Master database name | `monarc_master` | +| `DBUSER_MONARC` | Database user | `sqlmonarcuser` | +| `DBPASSWORD_MONARC` | Database password | `sqlmonarcuser` | +| `XDEBUG_ENABLED` | Enable Xdebug in the build (`1/0`, `true/false`, `yes/no`) | `1` | +| `XDEBUG_MODE` | Xdebug modes (`debug`, `develop`, etc.) | `debug` | +| `XDEBUG_START_WITH_REQUEST` | Start mode (`trigger` or `yes`) | `trigger` | +| `XDEBUG_CLIENT_HOST` | Host IDE address | `host.docker.internal` | +| `XDEBUG_CLIENT_PORT` | IDE port | `9003` | +| `XDEBUG_IDEKEY` | IDE key | `IDEKEY` | +| `XDEBUG_DISCOVER_CLIENT_HOST` | Auto-detect client host (`1/0`) | `0` | + +## Security Notes + +⚠️ **Important**: The default credentials provided are for development only. Never use these in production! + +For production deployments: +1. Change all default passwords +2. Use proper SSL/TLS certificates +3. Follow security best practices + +## Additional Resources + +- [MONARC Website](https://www.monarc.lu) +- [MONARC Documentation](https://www.monarc.lu/documentation) +- [GitHub Repository](https://github.com/monarc-project/MonarcAppBO) +- [MonarcAppFO (FrontOffice)](https://github.com/monarc-project/MonarcAppFO) + +## Getting Help + +If you encounter issues: + +1. Check the [troubleshooting section](#troubleshooting) +2. Review the logs: `docker compose -f docker-compose.dev.yml logs` +3. Open an issue on [GitHub](https://github.com/monarc-project/MonarcAppBO/issues) +4. Join the MONARC community discussions diff --git a/README.md b/README.md index a4a5226..ea454f3 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,22 @@ For installation instructions see [INSTALL](https://github.com/monarc-project/MonarcAppBO/tree/master/INSTALL). +Development Environment +----------------------- + +For local development, you can use either: + +- **Docker** (recommended): See [README.docker.md](README.docker.md) for instructions + ```bash + make start + ``` + +- **Vagrant**: See [vagrant/README.rst](vagrant/README.rst) for instructions + ```bash + cd vagrant && vagrant up + ``` + + License ------- diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml new file mode 100644 index 0000000..abd9ef2 --- /dev/null +++ b/docker-compose.dev.yml @@ -0,0 +1,78 @@ +services: + # MariaDB database for MONARC BackOffice + db: + image: mariadb:10.11 + container_name: monarc-bo-db + environment: + MYSQL_ROOT_PASSWORD: ${DBPASSWORD_ADMIN} + MYSQL_DATABASE: ${DBNAME_COMMON} + MYSQL_USER: ${DBUSER_MONARC} + MYSQL_PASSWORD: ${DBPASSWORD_MONARC} + command: + - --character-set-server=utf8mb4 + - --collation-server=utf8mb4_general_ci + - --bind-address=0.0.0.0 + ports: + - "3306:3306" + volumes: + - db_data:/var/lib/mysql + - ./docker/db-init:/docker-entrypoint-initdb.d:ro + networks: + - monarc-network + healthcheck: + test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${DBPASSWORD_ADMIN}"] + interval: 10s + timeout: 5s + retries: 5 + + # Main MONARC BackOffice application + monarc: + build: + context: . + dockerfile: Dockerfile + args: + XDEBUG_ENABLED: ${XDEBUG_ENABLED:-1} + XDEBUG_MODE: ${XDEBUG_MODE:-debug} + XDEBUG_START_WITH_REQUEST: ${XDEBUG_START_WITH_REQUEST:-trigger} + XDEBUG_CLIENT_HOST: ${XDEBUG_CLIENT_HOST:-host.docker.internal} + XDEBUG_CLIENT_PORT: ${XDEBUG_CLIENT_PORT:-9003} + XDEBUG_IDEKEY: ${XDEBUG_IDEKEY:-IDEKEY} + XDEBUG_DISCOVER_CLIENT_HOST: ${XDEBUG_DISCOVER_CLIENT_HOST:-0} + container_name: monarc-bo-app + environment: + DBHOST: db + DBNAME_COMMON: ${DBNAME_COMMON} + DBNAME_MASTER: ${DBNAME_MASTER} + DBUSER_MONARC: ${DBUSER_MONARC} + DBPASSWORD_MONARC: ${DBPASSWORD_MONARC} + DBPASSWORD_ADMIN: ${DBPASSWORD_ADMIN} + APP_ENV: development + APP_DIR: /var/www/html/monarc + ports: + - "5002:80" + depends_on: + db: + condition: service_healthy + networks: + - monarc-network + volumes: + # Mount the application code for live development + - ./:/var/www/html/monarc + # Preserve vendor directory in named volume for better performance + - vendor_data:/var/www/html/monarc/vendor + - node_modules_data:/var/www/html/monarc/node_modules + working_dir: /var/www/html/monarc + +networks: + monarc-network: + driver: bridge + +volumes: + db_data: + driver: local + driver_opts: + type: none + device: ./docker/db_data + o: bind + vendor_data: + node_modules_data: diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100755 index 0000000..9ee6504 --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,150 @@ +#!/bin/bash +set -e + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +echo -e "${GREEN}Starting MONARC BackOffice setup...${NC}" + +# Wait for database to be ready +echo -e "${YELLOW}Waiting for MariaDB to be ready...${NC}" +export MYSQL_PWD="${DBPASSWORD_MONARC}" +while ! mysqladmin ping -h"${DBHOST}" -u"${DBUSER_MONARC}" --silent 2>/dev/null; do + echo "Waiting for MariaDB..." + sleep 2 +done +echo -e "${GREEN}MariaDB is ready!${NC}" + +# Check if this is the first run +if [ ! -f "/var/www/html/monarc/.docker-initialized" ]; then + echo -e "${GREEN}First run detected, initializing application...${NC}" + + cd /var/www/html/monarc + + # Install composer dependencies (always, to ensure binaries like Phinx are present) + echo -e "${YELLOW}Installing Composer dependencies...${NC}" + # Using --ignore-platform-req=php to allow flexibility in development environment + # This is acceptable for development but should not be used in production + composer install --ignore-platform-req=php --no-interaction + + # Create module symlinks + echo -e "${YELLOW}Creating module symlinks...${NC}" + mkdir -p module/Monarc + cd module/Monarc + ln -sfn ./../../vendor/monarc/core Core + ln -sfn ./../../vendor/monarc/backoffice BackOffice + cd /var/www/html/monarc + + # Clone frontend repositories + echo -e "${YELLOW}Setting up frontend repositories...${NC}" + mkdir -p node_modules + cd node_modules + + if [ ! -d "ng_backoffice" ]; then + git clone --config core.fileMode=false https://github.com/monarc-project/ng-backoffice.git ng_backoffice + fi + + if [ ! -d "ng_anr" ]; then + git clone --config core.fileMode=false https://github.com/monarc-project/ng-anr.git ng_anr + fi + + cd /var/www/html/monarc + + # Check if master database exists and create databases if needed + echo -e "${YELLOW}Setting up databases...${NC}" + DB_EXISTS=$(mysql -h"${DBHOST}" -u"root" -p"${DBPASSWORD_ADMIN}" -e "SHOW DATABASES LIKE '${DBNAME_MASTER}';" | grep -c "${DBNAME_MASTER}" || true) + + if [ "$DB_EXISTS" -eq 0 ]; then + echo -e "${YELLOW}Creating databases...${NC}" + mysql -h"${DBHOST}" -u"root" -p"${DBPASSWORD_ADMIN}" -e "CREATE DATABASE IF NOT EXISTS ${DBNAME_MASTER} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;" + mysql -h"${DBHOST}" -u"root" -p"${DBPASSWORD_ADMIN}" -e "CREATE DATABASE IF NOT EXISTS ${DBNAME_COMMON} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;" + + echo -e "${YELLOW}Granting privileges to ${DBUSER_MONARC}...${NC}" + mysql -h"${DBHOST}" -u"root" -p"${DBPASSWORD_ADMIN}" -e "GRANT ALL PRIVILEGES ON ${DBNAME_MASTER}.* TO '${DBUSER_MONARC}'@'%';" + mysql -h"${DBHOST}" -u"root" -p"${DBPASSWORD_ADMIN}" -e "GRANT ALL PRIVILEGES ON ${DBNAME_COMMON}.* TO '${DBUSER_MONARC}'@'%';" + mysql -h"${DBHOST}" -u"root" -p"${DBPASSWORD_ADMIN}" -e "FLUSH PRIVILEGES;" + + echo -e "${YELLOW}Populating common database...${NC}" + mysql -h"${DBHOST}" -u"${DBUSER_MONARC}" ${DBNAME_COMMON} < db-bootstrap/monarc_structure.sql + mysql -h"${DBHOST}" -u"${DBUSER_MONARC}" ${DBNAME_COMMON} < db-bootstrap/monarc_data.sql + fi + + # Generate local config (always override to match container DB) + echo -e "${YELLOW}Creating local configuration...${NC}" + cat > config/autoload/local.php < [ + 'connection' => [ + 'orm_default' => [ + 'params' => [ + 'host' => '${DBHOST}', + 'user' => '${DBUSER_MONARC}', + 'password' => '${DBPASSWORD_MONARC}', + 'dbname' => '${DBNAME_COMMON}', + ], + ], + 'orm_cli' => [ + 'params' => [ + 'host' => '${DBHOST}', + 'user' => '${DBUSER_MONARC}', + 'password' => '${DBPASSWORD_MONARC}', + 'dbname' => '${DBNAME_MASTER}', + ], + ], + ], + ], + + 'activeLanguages' => array('fr','en','de','nl','es','ro','it','ja','pl','pt','ru','zh'), + + 'appVersion' => \$package_json['version'], + + 'checkVersion' => false, + 'appCheckingURL' => 'https://version.monarc.lu/check/MONARC', + + 'email' => [ + 'name' => 'MONARC', + 'from' => 'info@monarc.lu', + ], + + 'mospApiUrl' => 'https://objects.monarc.lu/api/', + + 'monarc' => [ + 'ttl' => 60, // timeout + 'salt' => '', // private salt for password encryption + ], +]; +EOF + + # Update and build frontend and run DB migrations + echo -e "${YELLOW}Building frontend and running DB migrations...${NC}" + ./scripts/update-all.sh + + # Create initial user and client (seeds are idempotent) + echo -e "${YELLOW}Creating initial user and client...${NC}" + php ./vendor/robmorgan/phinx/bin/phinx seed:run -c ./module/Monarc/BackOffice/migrations/phinx.php + + # Set permissions + echo -e "${YELLOW}Setting permissions...${NC}" + chown -R www-data:www-data /var/www/html/monarc/data + chmod -R 775 /var/www/html/monarc/data + + # Mark initialization as complete + touch /var/www/html/monarc/.docker-initialized + echo -e "${GREEN}Initialization complete!${NC}" +else + echo -e "${GREEN}Application already initialized, starting services...${NC}" +fi + +# Execute the main command +exec "$@" diff --git a/docker/db-init/10-dev-grants.sh b/docker/db-init/10-dev-grants.sh new file mode 100644 index 0000000..edb0e81 --- /dev/null +++ b/docker/db-init/10-dev-grants.sh @@ -0,0 +1,12 @@ +#!/bin/sh +set -e + +if [ -z "$DBUSER_MONARC" ]; then + echo "DBUSER_MONARC is not set; skipping dev grants." + exit 0 +fi + +mysql -u root -p"$MYSQL_ROOT_PASSWORD" <