System & Dependency Setup (setup.sh)
Relevant source files
The following files were used as context for generating this wiki page:
The setup.sh script is the primary orchestration component for initializing the MetaTrader 5 (MT5) environment within the Docker container. It consolidates all setup operations, including Wine prefix initialization, system dependency configuration, MT5 terminal installation, and the deployment of the gRPC bridge. This script ensures that the environment is production-ready by handling complex sequences such as headless X server management for installers and idempotency checks to prevent redundant configurations.
This setup process is triggered by the container entrypoint, start.sh, and is designed to fail-fast on critical errors, ensuring the container does not enter a degraded state.
Sources: docker/container/metatrader/setup.sh:1-12, docker/container/metatrader/start.sh:78-83, README.md:215-220
Setup Orchestration Flow
The setup process follows a strict linear sequence to ensure dependencies are met before proceeding to application installation.
The diagram shows the high-level logic and the detailed sub-process for Wine initialization.
Sources: docker/container/metatrader/setup.sh:401-430, docker/container/metatrader/setup.sh:65-156
Core Initialization Components
1. Wine Prefix Initialization (init_wine_prefix)
The script initializes a 64-bit Wine prefix at $WINEPREFIX (default /config/.wine). This process is idempotent, keyed on the existence of a .build-complete marker. If an incomplete prefix is detected, it is removed and rebuilt from scratch to prevent corruption.
Key actions during this phase:
- Headless X Server: Launches
Xvfbon display:99to provide a graphical environment for Windows installers. - Registry Overrides: Disables
winemenubuilder.exeandmscoreeto prevent popups and use Wine Mono instead. - Component Installation: Installs Wine Mono, Wine Gecko (x86 and x86_64), and a Windows-native Python 3.12 environment.
- gRPC Bridge Dependencies: Installs
grpcio,protobuf, andnumpy(v1.26.4) into the Windows Python environment.
Sources: docker/container/metatrader/setup.sh:65-81, docker/container/metatrader/setup.sh:105-156, tests/test_static.py:461-468
2. MetaTrader 5 Deployment
The script handles both the Python integration and the GUI terminal.
| Task | Method | Description |
|---|---|---|
| MT5 Pip Package | install_mt5_pip | Installs the MetaTrader5 library into Wine Python. Supports MT5_UPDATE environment variable to force upgrades. |
| MT5 Terminal | install_mt5_terminal | Downloads mt5setup.exe and runs it with the /auto flag. Uses a 300s timeout and monitors for the creation of terminal64.exe. |
| Auto-Login Config | generate_mt5_config | Generates a startup.ini file if MT5_LOGIN, MT5_PASSWORD, and MT5_SERVER are provided. |
Sources: docker/container/metatrader/setup.sh:196-218, docker/container/metatrader/setup.sh:233-255, docker/container/metatrader/setup.sh:305-325
3. gRPC Bridge Deployment (copy_bridge)
The gRPC bridge (comprising bridge.py and generated protobuf stubs) is copied into the Wine Python site-packages/mt5linux directory. This enables remote control of the MT5 terminal via the mt5linux library. The script performs a "fail-loud" check by attempting to import the stubs immediately after copying to detect protobuf version mismatches.
Sources: docker/container/metatrader/setup.sh:343-388, tests/test_static.py:487-500
Error Handling and Retries
The setup script utilizes a retry_with_backoff helper function for critical network-dependent or racy operations.
retry_with_backoff() {
local max_attempts=$1
local func_name=$2
# ... logic for exponential backoff (5s, 10s, 20s...)
}
Sources: docker/container/metatrader/setup.sh:33-55
Criticality Levels
The system distinguishes between required and recommended setup steps:
- Required (Exit on Failure): Wine prefix initialization, MT5 Pip installation, MT5 Terminal installation.
- Recommended/Optional: Config unpacking, generating login INI, and copying the bridge.
Sources: README.md:237-250, docker/container/metatrader/setup.sh:408-422
Configuration Parameters
The following variables influence the behavior of setup.sh and are typically passed via docker-compose.yaml or start.sh.
| Variable | Default | Purpose |
|---|---|---|
WINEPREFIX | /config/.wine | Location of the Wine environment. |
MT5_UPDATE | 1 | Whether to force update the MT5 pip package. |
MT5_INSTALL_TIMEOUT | 300 | Maximum seconds to wait for mt5setup.exe. |
ENABLE_WIN_DOTNET | 1 | Enables .NET Framework 4.8 via Wine Mono. |
STAGING_DIR | /opt/mt5-staging | Location of pre-downloaded MSI/EXE installers. |
Sources: docker/container/metatrader/start.sh:26-38, docker/container/metatrader/setup.sh:16, docker/compose.yaml:57-65
Conclusion
The setup.sh script provides a robust, idempotent initialization sequence that transforms a base Debian/Wine container into a specialized MetaTrader 5 host. By automating the installation of the Windows Python environment and the MT5 terminal, it enables seamless "zero-touch" deployments and consistent runtime environments for algorithmic trading.