Auto-Login Mechanism
Relevant source files
The following files were used as context for generating this wiki page:
The Auto-Login Mechanism in the mt5-docker project provides an automated, environment-driven way to authenticate MetaTrader 5 (MT5) terminals within a Linux/Wine container. It eliminates the need for manual interaction through a VNC interface by leveraging environment variables to provision credentials or dynamically create demo accounts.
This system supports two primary paths: a credential-based login using pre-existing account details and a "zero-touch" demo account provisioning system. The latter uses GUI automation via xdotool to drive the MT5 "Open an Account" wizard, ensuring the container can reach a functional state without human intervention. Sources: README.md:27-35, docker/container/metatrader/open_demo_account.py:1-25
Architecture and Data Flow
The mechanism is orchestrated through container startup scripts and a specialized Python automation script. The process begins during the container entrypoint execution, where environment variables are parsed and converted into MT5-compatible configuration files.
Startup Orchestration Flow
The following diagram illustrates how the system determines which login path to take during container initialization.
The flow ensures that if manual credentials are provided, they take precedence. If absent and the demo flag is set, the automation wizard takes over. Sources: docker/container/metatrader/start.sh:65-71, docker/container/metatrader/setup.sh:317-345, tests/test_static.py:539-550
Configuration Management
The core of the auto-login mechanism is the startup.ini file, which is the authoritative source MT5 reads upon every boot to re-attempt authentication. Sources: docker/container/metatrader/open_demo_account.py:46-50
Environment Variables
| Variable | Description | Default |
|---|---|---|
MT5_LOGIN | MetaTrader 5 account number | - |
MT5_PASSWORD | Account password | - |
MT5_SERVER | Broker server name | MetaQuotes-Demo |
MT5_AUTO_CREATE_DEMO | Enables headless demo provisioning | 0 |
MT5_DEMO_SERVER | Server to use for auto-demo creation | MetaQuotes-Demo |
Sources: README.md:214-220, docker/compose.yaml:63-65, docker/container/metatrader/open_demo_account.py:56-61
Configuration Generation
The setup.sh script includes a generate_mt5_config function that writes the [Common] and [Experts] blocks required by MT5. This includes setting KeepPrivate=1 to ensure credentials are encrypted by the terminal and AllowLiveTrading=1 to enable Expert Advisors. Sources: docker/container/metatrader/setup.sh:329-350
Zero-Touch Demo Provisioning
When MT5_AUTO_CREATE_DEMO is enabled, the system executes open_demo_account.py. This script drives the MT5 GUI via xdotool on the Xvfb display to simulate a human opening a demo account. Sources: docker/container/metatrader/open_demo_account.py:1-15
Automation Steps
- Wait for Terminal: The script polls for the MT5 window on the KasmVNC display.
- Dismiss Popups: It best-effort closes "LiveUpdate" windows or first-run dialogs using
wmctrl. - Drive Wizard: Navigates
File -> Open an Accountusing calibrated pixel coordinates (e.g.,FILE_MENU_XYat 22, 42). - Capture Credentials: Uses
xclipto copy the generated password from the final wizard page to avoid OCR. - Persistence: Writes the captured login and password to
startup.iniso the account survives container restarts.
Sources: docker/container/metatrader/open_demo_account.py:165-185, docker/container/metatrader/open_demo_account.py:270-285, docker/container/metatrader/open_demo_account.py:440-475
Authoritative Verification
The mechanism does not rely solely on the GUI's success message. It scans the MT5 terminal log (stored in UTF-16 format) for specific markers to confirm successful authorization. Sources: docker/container/metatrader/open_demo_account.py:207-225, tests/test_static.py:440-455
Persistence and Idempotency
To prevent repeated provisioning, the system maintains a success marker at /config/auto_demo.json. This file stores the confirmed login and server but never the password. On subsequent boots, the confirmed_result_login function checks if the current terminal session matches the saved marker. Sources: README.md:144-150, docker/container/metatrader/open_demo_account.py:510-530
If the startup.ini file exists and matches the provided environment variables, the system skips reconfiguration to preserve existing terminal settings. Sources: docker/container/metatrader/setup.sh:329-335
Security Considerations
The auto-login mechanism is designed to handle credentials with care:
- No Passwords in Logs: The automation script explicitly avoids capturing screenshots during the wizard's final page to prevent leaking passwords into logs. Sources: docker/container/metatrader/open_demo_account.py:28-32, tests/test_static.py:507-520
- Encryption: MT5's
KeepPrivate=1setting ensures the terminal encrypts the password stored in its internal storage after the first successful login. Sources: docker/container/metatrader/setup.sh:343 - Clipboard Safety: The script clears the X11 clipboard before and after capturing credentials to minimize the window of exposure. Sources: docker/container/metatrader/open_demo_account.py:330-340