Interactive Brokers' Trader Workstation (TWS) container with GUI
Create an Interactive Brokers' Trader Workstation platform Docker image that uses your computer's native GUI. This can be useful when your operating system is not supported by Trader Workstation or no recent version is available.
The docker image is based on alekna/docker-ib-tws Docker image (by Laurynas Alekna), but this derived version displays the GUI directly on your host computer. Which is a lot snappier and more robust. It is also based on https://tpaschalis.github.io/sandboxed-browser-with-docker for the browser sandbox from a container and sound problems (?) fix.
On the Docker host computer, if you run the container manually, do not forget to use xhost
to allow access to the X-server, as follows.
1xhost +LOCAL:
See the Makefile
for an easier way to to build and start the container.
The below project is on github: maridonkers/ib-tws-docker. I use it under NixOS to run IB TWS stable.
Disclaimer
Read Interactive Brokers' Warnings and Disclosures information. Also beware: this method of deploying IB TWS (via a Docker container) is not endorsed nor supported by IB. Caveat emptor.
Shared files with container
This is done via the data
subdirectory. Check if your uid is 1000 via the id
command. If it is not then find and change all uid instances in the files of the project and change the 1000 to your uid value.
Define the image via a Dockerfile
The Dockerfile
:
1# Interactive Brokers' Trader Workstation (TWS) container with GUI
2# displaying on the Docker host's X-server.
3#
4# This is based on alekna/ib-tws Interactive Brokers' Trader
5# Workstation (TWS) docker container, which uses VNC to access the
6# GUI.
7# https://github.com/alekna/docker-ib-tws
8#
9# It is also based on
10# https://tpaschalis.github.io/sandboxed-browser-with-docker
11#
12# and for a browser sandbox from a container and sound problems (?) fix.
13# https://github.com/TheBiggerGuy/docker-pulseaudio-example
14#
15
16FROM debian:buster
17
18# Timezone is also in docker-compose file.
19ENV HOME /root
20ENV TZ Europe/Amsterdam
21ENV SHELL /bin/bash
22
23# Install basic Desktop environment for ibtws.
24RUN apt-get update; \
25 apt-get upgrade -y; \
26 apt-get install -y procps sudo curl less vim-nox zip openbox tint2 pcmanfm xfce4-terminal; \
27 apt-get clean
28
29RUN sed -i "s#\smain\s*\$# main contrib non-free#" /etc/apt/sources.list
30
31# The Chromium web browser must be installed because TWS apparently
32# uses its functionality for e.g. iBot.
33#
34# Configure browser in TWS settings, as follows:
35# /usr/bin/firefox or /usr/bin/chromium
36#
37RUN apt-get update; \
38 apt-get install -y \
39 firefox-esr \
40 chromium \
41 chromium-l10n \
42 fonts-liberation \
43 fonts-roboto \
44 hicolor-icon-theme \
45 libcanberra-gtk-module \
46 libexif-dev \
47 libgl1-mesa-dri \
48 libgl1-mesa-glx \
49 libpango1.0-0 \
50 libv4l-0 \
51 fonts-symbola \
52 pulseaudio-utils \
53 libglib2.0-0 \
54 libavcodec58 \
55 libavformat58 \
56 --no-install-recommends; \
57 rm -rf /var/lib/apt/lists/*; \
58 apt-get clean
59
60# Create a non-root account to run TWS with.
61RUN useradd -ms /bin/bash --uid 1000 --gid 100 tws; \
62 usermod -G audio,video tws;
63
64# RUN echo "tws ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
65
66# Check the pulse-client.conf file (is your uid 1000 ?).
67COPY pulse-client.conf /etc/pulse/client.conf
68
69USER tws
70WORKDIR /home/tws
71ENV HOME /home/tws
72
73RUN mkdir -p /home/tws/Downloads; \
74 mkdir -p /home/tws/Desktop; \
75 mkdir -p /home/tws/bin
76
77# Retrieve and install TWS (and its embedded JRE). Choose between stable and latest.
78# Stable: https://download2.interactivebrokers.com/installers/tws/stable/tws-stable-linux-x64.sh
79# Latest: https://download2.interactivebrokers.com/installers/tws/latest/tws-latest-linux-x64.sh
80RUN cd /home/tws ; \
81 curl -sO https://download2.interactivebrokers.com/installers/tws/latest/tws-latest-linux-x64.sh; \
82 echo "/home/tws/Jts" | sh ./tws-latest-linux-x64.sh; \
83 rm ./tws-latest-linux-x64.sh
84
85# The DISPLAY variable is required to display TWS on your desktop.
86ENV PS1='$ '
87ENV DISPLAY=":0"
88
89# Start the installed Interactive Brokers' TWS. Its GUI will display on
90# the computer that is hosting the Docker container. Be sure to allow
91# access to its X-server via the following command:
92# xhost +LOCAL:
93#
94RUN echo "" >> /home/tws/.bashrc ;\
95 echo "echo \"Press Ctrl+C within 5 seconds to abort TWS start...\"" >> /home/tws/.bashrc ;\
96 echo "sleep 5" >> /home/tws/.bashrc ;\
97 echo "Jts/tws" >> /home/tws/.bashrc
98ENTRYPOINT ["/bin/bash"]
The pulse-client.conf
file:
1# Connect to the host's server using the mounted UNIX socket
2default-server = unix:/run/user/1000/pulse/native
3
4# Prevent a server running in the container
5autospawn = no
6daemon-binary = /bin/true
7
8# Prevent the use of shared memory
9enable-shm = false
The Docker compose file
1version: "2.0"
2services:
3 "tws":
4 image: ib-tws
5 build: .
6 stdin_open: true
7 tty: true
8 privileged: true
9 security_opt:
10 - seccomp:"./chrome.json"
11 devices:
12 - "/dev/snd/:/dev/snd/"
13 ipc: host
14 environment:
15 - TZ=Europe/Amsterdam
16 network_mode: host
17 volumes:
18 - "./data:/home/tws/data:rw"
19 - "./.mozilla:/home/tws/.mozilla:rw"
20 - "./Jts:/home/tws/Jts:rw"
21 - "/tmp/.X11-unix/:/tmp/.X11-unix/:ro"
22 - "~/.Xauthority:/home/tws/.Xauthority:rw"
23 - "/var/lib/usbmux:/var/lib/usbmux"
24 - "/var/run/user/1000/pulse:/run/user/1000/pulse"
25 - "/etc/asound.conf:/etc/asound.conf"
Makefile
Use the make
command to build the Docker container and bring it up. The Makefile
is shown below. Initially use make rebuild
to create the container. Then simply use make
to run it (leave the command prompt window open). After exit of TWS use CTRL+C to abort the container. To bring it down completely, use make down
.
1# Brings up the Docker container, which automatically starts IB TWS.
2# The attach can be used to connect to the command prompt in the
3# container, where e.g. a Ctrl-c can be used to force a stop.
4#
5all: up
6
7up:
8 xhost +LOCAL:
9 docker-compose up
10
11down:
12 sync
13 docker-compose down
14
15# If problems persist after a force-down then manually restart Docker daemon.
16force-down:
17 sync
18 docker rm -f ib_tws_1
19
20ls:
21 docker ps -a
22
23# Get custom seccomp profile (the wget) for browser sound.
24rebuild:
25 mkdir -p ./Jts
26 mkdir -p ./data
27 wget https://raw.githubusercontent.com/jfrazelle/dotfiles/master/etc/docker/seccomp/chrome.json -O ./chrome.json
28 xhost +LOCAL:
29 docker-compose build --no-cache
30
31build:
32 xhost +LOCAL:
33 docker-compose build
34
35attach:
36 xhost +LOCAL:
37 docker attach ib_tws_1
38
39shell:
40 xhost +LOCAL:
41 docker exec -it ib_tws_1 /bin/bash
IB TWS running
If it fails then check Interactive Brokers' server status page to see if there's e.g. any scheduled maintenace.
First time build:
1make rebuild
After a lot of downloading and building the Docker container is ready. Subsequent builds (normally not necessary) use the then already built container and will therefore be much faster. Simply running it does not require a new build.
Running the container:
1make
NOTE: To configure the browser enter /usr/bin/firefox
in TWS settings.
Graphical problems
In case of problems, try disabling the compositor in desktop settings and re-login. I specifically had problems with xfwm4 (the XFCE window manager) and disabling the compositor didn't help, so I switched back to KDE Plasma, which is lightweight these days. Also KDE feels much snappier, is apparently more robust and more feature rich. Why on earth did I switch to XMonad and then xfce? (It was something about KDE being heavy on resources, which is apparently no longer true nowadays.)