ib-gateway-docker

repository·master·Indexed 22 days ago

https://github.com/gnzsnz/ib-gateway-docker

A Docker-based solution for running Interactive Brokers (IB) Gateway and Trader Workstation (TWS). It provides containerized environments featuring automated login via IBC, support for live and paper trading modes, and remote access via VNC or RDP. The project includes aarch64 support, SSH tunneling for secure API access, and the ability to execute custom startup scripts.

Tokens
12K
Snippets
27
Records
33
Agent score
72%

What's inside ib-gateway-docker

  1. Overview of Interactive Brokers Gateway Docker

    master

    This project provides Docker images to run Interactive Brokers Gateway and Trader Workstation (TWS) without human interaction. It is designed for automated trading environments and includes several key components:

    • IB Gateway/TWS: The core trading application.
    • IBC (IB Controller): Simulates user input to control TWS/IB Gateway.
    • Xvfb: A virtual X11 framebuffer to run the application without graphics hardware.
    • x11vnc: An optional VNC server to interact with the IB Gateway UI.
    • xrdp/xfce: An environment for TWS (built on linuxserver/rdesktop).
    • socat: Relays TCP connections from non-localhost to the container's localhost (bypassing IB Gateway's default localhost restriction).
    • SSH Tunneling: Optional secure connections for IB Gateway and VNC (available in specific versions).
    • Parallel Execution: Supports running both live and paper trading modes simultaneously.
    • Secrets Support: Secure handling of credentials.
    • aarch64 Support: Compatible with Raspberry Pi, M1/M2/M3 chips, etc.
    • Custom Scripts: Ability to execute scripts during the startup process.
  2. What is Interactive Brokers Gateway Docker?

    master

    This project provides Docker images to run Interactive Brokers Gateway or Trader Workstation (TWS) without human interaction. It automates the setup required for headless environments by including:

    • IB Gateway or TWS: The core trading applications.
    • IBC (IB Controller): Simulates user input to control TWS/IB Gateway.
    • Xvfb: A virtual X11 framebuffer to run applications without graphics hardware.
    • x11vnc: An optional VNC server to interact with the IB Gateway UI.
    • xrdp/xfce: An environment for TWS (based on linuxserver/rdesktop).
    • socat: Relays TCP connections from non-localhost to the container's localhost (bypassing IB Gateway's default localhost restriction).
    • SSH Tunneling: Optional secure connections for IB Gateway and VNC (available in specific versions).
    • Parallel Execution: Supports running live and paper trading modes simultaneously.
    • Custom Scripts: Allows execution of custom scripts during the startup process.
  3. How IB Gateway installation files are managed

    master

    By default, the Dockerfile does not download installer files directly from the IBKR homepage. Instead, it pulls versioned installer files from this project's GitHub Releases. This allows users to target specific, older versions of the Gateway that IBKR no longer provides direct download links for.

    If you want to use the current stable version directly from IBKR, you must modify the Dockerfile to change the curl source URL.

    # Example: Change the RUN command in Dockerfile to use IBKR's stable URL
    RUN curl -sSL https://download2.interactivebrokers.com/installers/ibgateway/stable-standalone/ibgateway-stable-standalone-linux-x64.sh --output ibgateway-${IB_GATEWAY_VERSION}-standalone-linux-x64.sh
  4. Preserve settings across containers using TWS_SETTINGS_PATH

    master

    To persist configuration and data (like jts.ini) across container restarts, set the TWS_SETTINGS_PATH environment variable and mount a volume to that path.

    Important: If jts.ini exists in the volume, the TIME_ZONE setting will not be applied to it to avoid overwriting your manual settings.

    Paths by Image Type:

    • IB Gateway: Use /home/ibgateway/tws_settings (or your custom path).
    • TWS (linuxserver): Use /config/tws_settings (or your custom path).
        environment:
          - TWS_SETTINGS_PATH: /home/ibgateway/tws_settings # IB Gateway
        volumes:
          - ${PWD}/tws_settings:/home/ibgateway/tws_settings # IB Gateway
  5. Configure IB API port access and security

    master

    The IB API uses unencrypted TCP sockets. Choose your exposure level based on your security needs:

    1. Localhost (Default): Only accessible from the docker host. Suitable for testing.
    2. Host Network (Unsecure): Exposes ports directly to the host network. Not recommended.
      ports:
        - "4001:4003"
        - "4002:4004"
        - "5900:5900"
    3. Docker Network (Secure): Accessible to other services in the same Docker network (e.g., a trader network) via the service hostname (e.g., ib-gateway).
    4. SSH Tunnel (Most Secure): Uses an SSH client to create a remote tunnel to a bastion host, exposing the port only through a secure encrypted channel.
  6. Quickstart: Deploy IB Gateway with Docker Compose

    master

    To run IB Gateway, follow these steps:

    1. Create a docker-compose.yml file: Use the provided sample or create your own. Ensure you map the necessary environment variables and ports.
    2. Create an .env file: In the root directory, create an .env file to store your credentials and configuration. You can use .env-dist as a template.
    3. Start the container: Run the following command:
    docker compose up

    To access the GUI, use VNC for ib-gateway or RDP for tws-rdesktop.

    name: algo-trader
    services:
      ib-gateway:
        restart: always
        build:
          context: ./stable
          tags:
            - "ghcr.io/gnzsnz/ib-gateway:stable"
        image: ghcr.io/gnzsnz/ib-gateway:stable
        environment:
          TWS_USERID: ${TWS_USERID}
          TWS_PASSWORD: ${TWS_PASSWORD}
          TWS_PASSWORD_FILE: ${TWS_PASSWORD_FILE}
          TRADING_MODE: ${TRADING_MODE:-paper}
          TWS_SETTINGS_PATH: ${TWS_SETTINGS_PATH:-}
          TWS_ACCEPT_INCOMING: ${TWS_ACCEPT_INCOMING:-}
          TWS_MASTER_CLIENT_ID: ${TWS_MASTER_CLIENT_ID:-}
          READ_ONLY_API: ${READ_ONLY_API:-}
          VNC_SERVER_PASSWORD: ${VNC_SERVER_PASSWORD:-}
          TWOFA_TIMEOUT_ACTION: ${TWOFA_TIMEOUT_ACTION:-exit}
          BYPASS_WARNING: ${BYPASS_WARNING:-}
          AUTO_RESTART_TIME: ${AUTO_RESTART_TIME:-}
          AUTO_LOGOFF_TIME: ${AUTO_LOGOFF_TIME:-}
          TWS_COLD_RESTART: ${TWS_COLD_RESTART:-}
          SAVE_TWS_SETTINGS: ${SAVE_TWS_SETTINGS:-}
          RELOGIN_AFTER_TWOFA_TIMEOUT: ${RELOGIN_AFTER_TWOFA_TIMEOUT:-no}
          TWOFA_EXIT_INTERVAL: ${TWOFA_EXIT_INTERVAL:-60}
          TWOFA_DEVICE: ${TWOFA_DEVICE:-}
          EXISTING_SESSION_DETECTED_ACTION: ${EXISTING_SESSION_DETECTED_ACTION:-primary}
          ALLOW_BLIND_TRADING: ${ALLOW_BLIND_TRADING:-no}
          TIME_ZONE: ${TIME_ZONE:-Etc/UTC}
          TZ: ${TIME_ZONE:-Etc/UTC}
          CUSTOM_CONFIG: ${CUSTOM_CONFIG:-NO}
          JAVA_HEAP_SIZE: ${JAVA_HEAP_SIZE:-}
          SSH_TUNNEL: ${SSH_TUNNEL:-}
          SSH_OPTIONS: ${SSH_OPTIONS:-}
          SSH_ALIVE_INTERVAL: ${SSH_ALIVE_INTERVAL:-}
          SSH_ALIVE_COUNT: ${SSH_ALIVE_COUNT:-}
          SSH_PASSPHRASE: ${SSH_PASSPHRASE:-}
          SSH_REMOTE_PORT: ${SSH_REMOTE_PORT:-}
          SSH_USER_TUNNEL: ${SSH_USER_TUNNEL:-}
          SSH_RESTART: ${SSH_RESTART:-}
          SSH_VNC_PORT: ${SSH_VNC_PORT:-}
          START_SCRIPTS: ${START_SCRIPTS:-}
          X_SCRIPTS: ${X_SCRIPTS:-}
          IBC_SCRIPTS: ${IBC_SCRIPTS:-}
        ports:
          - "127.0.0.1:4001:4003"
          - "127.0.0.1:4002:4004"
          - "127.0.0.1:5900:5900"
  7. Build the Docker image using a local IB Gateway installer

    master

    By default, the Dockerfile downloads installer files from this project's GitHub releases to ensure version stability. If you want to use a local installer or download directly from IB, follow these steps:

    1. Clone the repository:

      git clone https://github.com/gnzsnz/ib-gateway-docker
    2. Modify the Dockerfile to use local files: Replace the RUN curl ... lines that download the .sh and .sha256 files with a COPY command:

      COPY ibgateway-${IB_GATEWAY_VERSION}-standalone-linux-x64.sh
    3. Handle checksums: Remove the line RUN sha256sum --check ./ibgateway-${IB_GATEWAY_VERSION}-standalone-linux-x64.sh.sha256 from the Dockerfile (unless you provide a matching checksum file).

    4. Prepare installer files:

      • Download the IB Gateway installer and name it ibgateway-${IB_GATEWAY_VERSION}-standalone-linux-x64.sh. Ensure {IB_GATEWAY_VERSION} matches the version defined in the first line of your Dockerfile.
      • Download the IBC file and name it IBCLinux-{IBC_VERSION}.zip. Ensure {IBC_VERSION} matches the version defined in your Dockerfile.
    5. Build and run:

      docker-compose up --build
    git clone https://github.com/gnzsnz/ib-gateway-docker
    # ... modify Dockerfile ...
    # ... prepare files ...
    docker-compose up --build
  8. Set up IB Gateway using Docker Compose

    master

    To run IB Gateway, create a docker-compose.yml file and an .env file in your root directory. The .env file should contain your credentials and configuration settings. Use the provided .env-dist as a template.

    Once configured, start the container using:

    docker compose up

    To access the GUI, use VNC for ib-gateway or RDP for tws-rdesktop.

    name: algo-trader
    services:
      ib-gateway:
        restart: always
        image: ghcr.io/gnzsnz/ib-gateway:stable
        environment:
          TWS_USERID: ${TWS_USERID}
          TWS_PASSWORD: ${TWS_PASSWORD}
          TRADING_MODE: ${TRADING_MODE:-paper}
          # ... other env vars
        ports:
          - "127.0.0.1:4001:4003"
          - "127.0.0.1:4002:4004"
          - "127.0.0.1:5900:5900"
  9. Build the Docker image locally using local installer files

    master

    If you want to build the image using your own local IB Gateway and IBC installer files instead of downloading them during the build process, follow these steps:

    1. Clone the repository:
      git clone https://github.com/gnzsnz/ib-gateway-docker
    2. Prepare installer files:
      • Download the IB Gateway installer and name it ibgateway-${IB_GATEWAY_VERSION}-standalone-linux-x64.sh (ensure ${IB_GATEWAY_VERSION} matches the version defined in the Dockerfile).
      • Download the IBC installer and name it IBCLinux-${IBC_VERSION}.zip (ensure ${IBC_VERSION} matches the version defined in the Dockerfile).
    3. Modify the Dockerfile:
      • Replace the RUN curl ... lines for the .sh and .sha256 files with a single COPY command:
        COPY ibgateway-${IB_GATEWAY_VERSION}-standalone-linux-x64.sh
      • Remove the RUN sha256sum --check ... line (unless you wish to manually manage checksums).
    4. Build and run:
      docker-compose up --build
    # Step 1: Clone
    git clone https://github.com/gnzsnz/ib-gateway-docker
    
    # Step 4: Build and run
    docker-compose up --build
  10. Customize IB Gateway or TWS configuration files

    master

    While most settings are available via environment variables, you can use custom configuration files by setting CUSTOM_CONFIG=yes. This prevents the run.sh script from overwriting environment variables into the config files. You must provide ready-to-use files for IB Gateway/TWS and IBC.

    IB Gateway Locations:

    • IB Gateway jts.ini: /home/ibgateway/Jts/jts.ini
    • IBC config.ini: /home/ibgateway/ibc/config.ini

    TWS Locations:

    • TWS jts.ini: /opt/ibkr/jts.ini
    • IBC config.ini: /opt/ibc/config.ini
        environment:
          - CUSTOM_CONFIG: yes
        volumes:
          - ${PWD}/config.ini:/home/ibgateway/ibc/config.ini
          - ${PWD}/jts.ini:/home/ibgateway/Jts/jts.ini # for IB Gateway
          - ${PWD}/jts.ini:/opt/ibkr/jts.ini # for TWS
          - ${PWD}/config.ini:/opt/ibc/config.ini # for TWS
  11. Automate tasks with Start-up scripts

    master

    You can run .sh scripts automatically during container startup by defining paths in environment variables. Scripts are executed in alphabetical order (e.g., 00-script.sh runs before 99-script.sh).

    Execution Phases:

    1. START_SCRIPTS: Runs before the X environment is up.
    2. X_SCRIPTS: Runs once the X environment is up.
    3. IBC_SCRIPTS: Runs once IBC runs.

    Setup Requirements:

    • The script directory must be mounted via a volume.
    • For IB Gateway, $HOME is /home/ibgateway.
    • For TWS, $HOME is /config.

    Example Configuration:

    # .env file
    START_SCRIPTS=init-scripts/start_scripts
    X_SCRIPTS=init-scripts/x_scripts
    IBC_SCRIPTS=init-scripts/ibc_scripts
      volume:
        - ${PWD}/init-scripts:/home/ibgateway/init-scripts
  12. Set up an SSH tunnel for IB Gateway

    master

    The container creates a remote tunnel using an SSH client (it does not run an SSH server). This allows you to expose the IB API port securely through an SSH bastion.

    Minimal Setup:

    1. Environment Variables:
      • SSH_TUNNEL=yes
      • SSH_USER_TUNNEL=user@server
      • SSH_PASSPHRASE: (Recommended) If set, starts ssh-agent and adds keys to it.
    2. Volume Mount: You must mount your SSH keys into the container.
      • IB Gateway: ${PWD}/ssh:/home/ibgateway/.ssh
      • TWS: ${PWD}/config/ssh:/config/.ssh
    3. Requirements:
      • Keys must have standard names (e.g., id_rsa, id_ed25519).
      • Keys must have proper permissions (chmod 600).
      • A known_hosts file must be present in the volume, or use SSH_OPTIONS=-o StrictHostKeyChecking=no (not recommended for production).
        environment:
          - SSH_TUNNEL=yes
          - SSH_USER_TUNNEL=user@bastion
          - SSH_PASSPHRASE=your_passphrase
        volumes:
          - ${PWD}/ssh:/home/ibgateway/.ssh