Below is a minimal-footprint, single-stage Dockerfile that
• Installs Wine (64- & 32-bit) on a slim Debian image
• Silently installs MetaTrader 5 inside its own Wine prefix
• Installs Python 3 + FastAPI + MetaTrader5 package
• Starts a FastAPI service with Uvicorn
(It weighs ± 450 MB compressed. If you need it even smaller, see the “Size-shrinking tips” that follow.)
``Dockerfile
-------------------------------
MetaTrader 5 + FastAPI + Wine
-------------------------------
FROM debian:bookworm-slim
LABEL maintainer="you@example.com"
ENV DEBIAN_FRONTEND=noninteractive
WINEPREFIX=/home/trader/.wine
------------------------------------------------------------
1. OS & Wine — keep it tiny: no docs, no locales, no cache
------------------------------------------------------------
RUN dpkg --add-architecture i386 &&
apt-get update &&
apt-get install -y --no-install-recommends
ca-certificates gnupg2 wget
xvfb winbind
wine64 wine32
python3 python3-pip &&
apt-get clean && rm -rf /var/lib/apt/lists/*
------------------------------------------------------------
2. Non-root user (so MT5 doesn’t run as root)
------------------------------------------------------------
RUN useradd -ms /bin/bash trader
USER trader
WORKDIR /home/trader
------------------------------------------------------------
3. Download & silent-install MetaTrader 5
------------------------------------------------------------
RUN wget -qO /tmp/mt5setup.exe
"https://download.mql5.com/cdn/web/metaquotes.software.corp/mt5/mt5setup.exe" &&
wine /tmp/mt5setup.exe /silent &&
rm /tmp/mt5setup.exe
------------------------------------------------------------
4. Python libs (—no-cache-dir keeps layers small)
------------------------------------------------------------
RUN pip install --no-cache-dir
fastapi uvicorn[standard] MetaTrader5
------------------------------------------------------------
5. FastAPI example
app/main.py will be copied in the build context
------------------------------------------------------------
COPY app ./app
WORKDIR /home/trader/app
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Project tree you’d build:
.
├─ Dockerfile
└─ app/
└─ main.py
Example `app/main.py` – a super-simple FastAPI endpoint that calls MetaTrader 5’s Python wrapper (works because both the wrapper and the terminal live in the same Wine prefix):
```python
from fastapi import FastAPI
import MetaTrader5 as mt5
import os
app = FastAPI()
@app.on_event("startup")
def launch_mt5():
# Path to terminal64.exe inside Wine prefix
term_path = os.path.expanduser("~/.wine/drive_c/Program Files/MetaTrader 5/terminal64.exe")
if not mt5.initialize(term_path):
raise RuntimeError(f"MT5 init failed: {mt5.last_error()}")
@app.get("/")
def root():
return {"status": "MT5 is up!"}
@app.get("/ping")
def ping():
return {"ping": "pong"}
@app.get("/account")
def account():
acc = mt5.account_info()
return acc._asdict() if acc else {"error": mt5.last_error()}
Build & run
docker build -t mt5-fastapi .
docker run -d -p 8000:8000 --name mt5 mt5-fastapi
# then curl http://localhost:8000/
────────────────────────
Size-shrinking tips 🔧
────────────────────────
-
Multi-stage + docker-slim
Build normally, then run docker-slim build --http-probe mt5-fastapi to cut it by ~40–60 %.
-
Strip Wine a bit more
Replace wine64 wine32 with wine only, remove winbind, or install via wine-stable-amd64 alone (but some brokers’ DLLs need 32-bit).
-
Alpine + Wine
Possible but painful (glibc vs musl). If you truly need < 200 MB, try ghcr.io/crazymax/alpine-wine as base.
Enjoy your ultra-lean MT5 + FastAPI container 🚀