Docker Compose Deployment
Relevant source files
The following files were used as context for generating this wiki page:
The Docker Compose deployment provides a streamlined, "preferred way" to orchestrate the MetaTrader 5 (MT5) environment. It encapsulates a multi-layer architecture consisting of a Debian-based container running Wine for Windows application compatibility, a gRPC/RPyC bridge for remote Python control, and a VNC server for graphical interface access. This deployment model ensures reproducibility by pinning core component versions such as Python, NumPy, and Wine-specific dependencies within the containerized environment.
Sources: README.md:162-164, docker/compose.yaml:4-16, tests/test_static.py:101-105
Service Architecture
The deployment is centered around the mt5 service, which utilizes a multi-stage Dockerfile build to establish a production-ready environment. The architecture separates the graphical interaction layer (KasmVNC) from the programmatic control layer (gRPC/RPyC).
Container Orchestration Flow
The following diagram illustrates the initialization sequence when the container is started via Docker Compose.
The initialization process ensures that the Wine prefix is built at runtime during the first boot to avoid race conditions and non-deterministic failures associated with build-time prefix creation.
Sources: docker/container/metatrader/start.sh:37-65, docker/container/metatrader/setup.sh:82-95, tests/test_static.py:221-229
Configuration and Environment
Deployment configuration is managed through a combination of compose.yaml and environment files. The system implements a hierarchical environment loading strategy, requiring a template file while allowing optional local overrides.
Core Configuration Options
| Variable | Default | Purpose |
|---|---|---|
MT5_LOGIN | - | MT5 account number for auto-login |
MT5_PASSWORD | - | MT5 account password |
MT5_SERVER | MetaQuotes-Demo | Broker server address |
ENABLE_WIN_DOTNET | 1 | Installs .NET Framework 4.8 via Wine Mono |
AUTO_RECOVERY_ENABLED | 1 | Enables background health monitoring and auto-restart |
MT5_AUTO_CREATE_DEMO | 0 | Triggers headless GUI automation to create a demo account |
Sources: docker/compose.yaml:45-56, README.md:315-333, tests/test_static.py:291-300
Port Mapping
| Port | Protocol | Description |
|---|---|---|
3000 | HTTP | KasmVNC web interface (Desktop access) |
8001 | TCP | gRPC/RPyC bridge for remote control |
8002 | TCP | Container health check endpoint |
Sources: docker/compose.yaml:30-33, tests/constants.py:246-248
Persistence and Volumes
Persistence is critical for maintaining the Wine environment, MT5 terminal settings, and Expert Advisor (EA) files. The compose.yaml defines a named volume config_data mapped to the internal /config path.
The /config volume stores the MT5 data folder structure, allowing users to place .mq5 or .ex5 files in config/.wine/drive_c/Program Files/MetaTrader 5/MQL5 without requiring a container restart.
Sources: docker/compose.yaml:118-123, README.md:214-222, docker/container/metatrader/open_demo_account.py:53-55
Resource Management and Security
To ensure MT5 stability and protection against process memory access issues, the deployment applies specific Linux capabilities and resource limits.
Security Configurations
- Capabilities: Adds
SYS_PTRACE, which is required for certain MetaQuotes protection mechanisms to function within the container. - Security Options: Uses
seccomp:unconfinedto allow the terminal to execute necessary system calls. - Ulimits: Sets
nofile(file descriptors) to65536to prevent Wine crashes under heavy load.
Sources: docker/compose.yaml:94-106, tests/test_static.py:311-325
Deployment Health Monitoring
The service includes a Docker healthcheck that verifies the availability of the gRPC server on port 8001. A significant start_period of 900 seconds is defined to accommodate the first-run installation of MT5 and Wine dependencies.
Sources: docker/compose.yaml:107-114, tests/test_static.py:302-309
Automated Demo Provisioning
A unique feature of this deployment is the "Zero-touch" demo account creation. If MT5_LOGIN is unset and MT5_AUTO_CREATE_DEMO is enabled, the system uses xdotool to drive the MT5 GUI wizard.
This process captures the generated master password and persists it to startup.ini so the account survives container restarts.
Sources: README.md:188-199, docker/container/metatrader/open_demo_account.py:22-38, tests/test_static.py:381-395
Conclusion
The Docker Compose deployment facilitates a robust, production-grade MetaTrader 5 environment. By combining automated setup scripts, health monitoring, and specific security configurations, it provides a stable platform for both manual trading via VNC and automated algorithmic trading through the gRPC/Python bridge.