A Docker Container for the Interactive Brokers Gateway in a Headless Environment. Supports running both Live and Paper Environments.
As of 11/25/2020 I did not see any other currently working examples of building a docker container for the IB Gateway on Github. In order to create run this Docker container you will need to have the Docker and Docker-Compose CLI components installed on your machine.
This will make the IB Gateway available on your local machine on port 4003 regardless of whether you run the container in paper or live mode.
- This implies you cannot run more than one container at a time. Although with minimal changes I suppose you could.
After you run the container, you need to wait 30 seconds before trying to connect to port 4003 because this is the amount of time provided for the IB Gateway to start before the ports are forked inside the container.
- Clone this repository onto your local machine.
- Review the settings in config.ini so you understand the behavior of the gateway and change if necessary.
- It is not required to change anything and the fields that are blank should remain so because they are set by the environment section of the yml files.
- Open a text editor and create a file called tws_credentials.env inside the context directory and insert the username and password you use to login to Interactive Brokers
- Refer to the example file if necessary.
- This is included in the .gitignore so it will not be pushed to your source control but your password is still stored as plain text on your local machine so take necessary precaution to protect this file.
- Note - i use the same ID and password for both live and paper, but you could create two .envs, and add the appropriate .env file to the env_file section in each yml file.
Then, follow these steps to build a Docker Image and run the container.
- cd into the context directory (where the Dockerfile lives)
- Run
docker-compose build
in the console - Run
docker-compose up
to start the container with TRADING_MODE set to paper.- To run the container with trading mode set to live run
docker-compose -f docker-compose-live.yml -f prod.yml up
instead
- To run the container with trading mode set to live run
- Connect to the IB Gateway from your local machine with
ipaddress="127.0.0.1", portid=4003
- Traditionally you can connect to IB Gateway locally by specifying
ipaddress=""
, however, this would not work for me.
- Traditionally you can connect to IB Gateway locally by specifying
I have not tested shutting down the container via Docker, however telnet is installed in the container so you can communicate with the IBC service on port 7462 e.g. { echo "STOP"; } | telnet 127.0.0.1 7462
in order to close the IB Gateway.
If you see any issues or improvements, please let me know.