Container Startup Sequence (start.sh)
Relevant source files
The following files were used as context for generating this wiki page:
The start.sh script serves as the primary entrypoint for the MetaTrader 5 (MT5) Docker container. It is responsible for orchestrating the initialization of the environment, including directory exporting, dependency verification, and execution of the detailed setup.sh operations. The script ensures that the Wine prefix is correctly configured and that the MetaTrader 5 terminal and its associated gRPC bridge are ready for operation.
This startup sequence follows a "fail-fast" design, where critical failures during the initialization process result in an immediate non-zero exit to prevent the container from entering an unstable state. It also coordinates with the s6-overlay system by setting specific startup markers that other services, such as the gRPC bridge server, rely on before starting.
Sources: docker/container/metatrader/start.sh:1-12, README.md:27-31
Execution Orchestration
The startup flow follows a logical progression from environment configuration to service readiness.
The diagram shows the high-level flow from the entrypoint script through the setup phase and into the optional health monitoring background task.
Sources: docker/container/metatrader/start.sh:76-118
Configuration and Environment Setup
start.sh centralizes the configuration for the entire container environment. It exports critical paths for Wine, MetaTrader 5, and the Python bridge. It also sources a build manifest if present in the staging directory.
Core Environment Variables
| Variable | Description | Source |
|---|---|---|
WINEPREFIX | Path to the Wine environment directory (default: /config/.wine). | start.sh:26 |
STAGING_DIR | Directory containing build-time artifacts and installers. | start.sh:19 |
STARTUP_MARKER | File created to signal to s6-overlay that setup is finished. | start.sh:56 |
WINE_PYTHON_PATH | Path to the Python executable within the Wine drive_c. | start.sh:38 |
MT5_AUTO_CREATE_DEMO | Toggle for zero-touch demo account provisioning. | start.sh:72 |
Sources: docker/container/metatrader/start.sh:18-72
Setup Phase (setup.sh)
The setup.sh script is called by start.sh to perform the heavy lifting of installation and configuration. It is designed to be idempotent using specific markers.
Wine Prefix Initialization
The initialization process builds the Wine prefix at first boot, installing Wine Mono, Wine Gecko, and Wine-side Python. It uses a dedicated headless Xvfb instance to ensure reliable installation of GUI-based components.
Sources: docker/container/metatrader/setup.sh:62-85
Component Installation Details
- Wine Mono & Gecko: Provides .NET Framework support and HTML rendering within Wine.
- Wine Python: A specific version (typically 3.12) is installed to maintain compatibility with
numpy1.26.4. - MT5 Terminal: Downloaded via
curland installed automatically using the/autoflag. - gRPC Bridge: The
bridge.pyand generated protobuf stubs are copied to the Wine Pythonsite-packagesdirectory.
Sources: docker/container/metatrader/setup.sh:110-142, docker/container/metatrader/setup.sh:211-235, docker/container/metatrader/setup.sh:293-315
The sequence diagram illustrates the dependencies between the setup script and the resulting environment configuration.
Sources: docker/container/metatrader/setup.sh:353-380
Health Monitoring and Auto-Recovery
If AUTO_RECOVERY_ENABLED is set to 1 (default), the start.sh script launches health_monitor.sh in the background. This script monitors the state of both the MT5 terminal and the gRPC bridge.
Monitoring Logic
- Thresholds: Uses
FAILURE_THRESHOLDto prevent premature restarts. - Restart Mechanism: Instead of killing processes directly, the monitor creates a
RESTART_TOKENfile. Thes6-overlayservicesvc-mt5servermonitors this token and performs a controlled full restart. - Health Check Port: The monitor typically listens on port 8002 for external health validation.
Sources: docker/container/metatrader/health_monitor.sh, tests/test_static.py:539-556
Summary
The start.sh sequence ensures a reproducible and resilient MetaTrader 5 environment. By consolidating setup logic into setup.sh and utilizing s6-overlay markers, the system achieves a reliable transition from container boot to a fully functional gRPC-controlled trading platform. The dual-script architecture allows for complex Wine prefix building and GUI automation (via open_demo_account.py) to occur before the runtime services begin their execution.
Sources: README.md:195-208, docker/container/metatrader/start.sh:103-118