gnzsnz / ib-gateway-docker

Docker image with IB Gateway/TWS and IBC
https://github.com/users/gnzsnz/packages/container/package/ib-gateway
MIT License
253 stars 48 forks source link

Unable to connect via docker networking #83

Closed craigp318 closed 7 months ago

craigp318 commented 7 months ago

I’m following the instructions to connect ib-gateway to a python image in the same docker compose.

My .env file follows the example from the README exactly (except I’ve substituted my own TWS_USERID and TWS_PASSWORD). My docker compose follows the example in the README exactly and is shown below. Note that I’ve commented the ports section and replaced with networks: trader: as specified in the “Security Considerations” section.

My python runner is a basic python script that just tries to connect (shown below). I've tried using host=ib-gateway, 0.0.0.1, localhost; but I keep getting TimeoutError.

Any ideas how to access the container via networking mode?

===================== docker_compose.yml

version: "3.4"

name: algo-trader services: py: build: context: . dockerfile: Dockerfile container_name: py networks:

networks: trader:

================ run.py

from ib_insync import *

if name == "main": ib = IB() host = "ib-gateway" ib.connect(host, 4004, clientId=123)

gnzsnz commented 7 months ago

it should work. i don't see anything wrong with it. i have the same setup working with no problems at all.

make sure that ibgateway is actually working and receiving connections. you can check on vnc the API log

craigp318 commented 7 months ago

Thanks for your reply. I don't have vnc setup yet; is there a way to test that ibgateway is actually working without it? E.g,. from the command line?

craigp318 commented 7 months ago

Looking at the docker logs on the ib-gateway image, it seems like it's successfully connected (though I do see a recurring java.lang.InterruptedException which was deemed just a warning)

------------------------------------------------------------
2024-03-18 20:43:32:316 IBC: using default settings provider: ini file is /home/ibgateway/ibc/config.ini
2024-03-18 20:43:32:316 IBC: using default login manager: will get username and password from settings; FIX username and password (if required) from settings
2024-03-18 20:43:32:316 IBC: using default main window manager
2024-03-18 20:43:32:316 IBC: using default trading mode manager: constructor parameter args but trading mode not present - will be taken from settings
2024-03-18 20:43:32:319 IBC: using default config dialog manager
2024-03-18 20:43:32:337 IBC: CommandServer is not started because the port is not configured
2024/03/18 20:43:32 socat[90] E connect(5, AF=2 127.0.0.1:4002, 16): Connection refused
2024-03-18 20:43:32:698 IBC: TWS Settings directory is: /home/ibgateway/Jts
2024-03-18 20:43:32:699 IBC: Starting session: will exit if login dialog is not displayed within 60 seconds
2024-03-18 20:43:32:727 IBC: Ensuring /home/ibgateway/Jts/jts.ini contains required minimal lines
2024-03-18 20:43:32:728 IBC: Found setting: [Logon]/s3store=true
2024-03-18 20:43:32:728 IBC: Found setting: [Logon]/Locale=en
2024-03-18 20:43:32:728 IBC: Found setting: [Logon]/displayedproxymsg=1
2024-03-18 20:43:32:728 IBC: Found setting: [Logon]/UseSSL=true
2024-03-18 20:43:32:728 IBC: Found setting: [IBGateway]/ApiOnly=true
2024-03-18 20:43:32:729 IBC: Confirmed /home/ibgateway/Jts/jts.ini contains required minimal lines
2024-03-18 20:43:32:729 IBC: Starting Gateway
2024-03-18 20:43:35:574 IBC: Getting config dialog
2024-03-18 20:43:35:574 IBC: Creating config dialog future
2024-03-18 20:43:35:576 IBC: Getting config dialog
2024-03-18 20:43:35:577 IBC: Waiting for config dialog future to complete
2024-03-18 20:43:35:577 IBC: Getting main window
2024-03-18 20:43:35:577 IBC: Creating main window future
2024-03-18 20:43:37:837 IBC: detected frame entitled: IBKR Gateway; event=Activated
2024-03-18 20:43:37:846 IBC: detected frame entitled: IBKR Gateway; event=Focused
2024-03-18 20:43:37:846 IBC: detected frame entitled: IBKR Gateway; event=Opened
2024-03-18 20:43:37:847 IBC: Login dialog WINDOW_OPENED: LoginState is LOGGED_OUT
2024-03-18 20:43:37:848 IBC: trading mode from settings: tradingMode=paper
2024-03-18 20:43:37:848 IBC: Setting Trading mode = paper
2024-03-18 20:43:37:944 IBC: Setting user name
2024-03-18 20:43:37:945 IBC: Setting password
2024-03-18 20:43:37:946 IBC: Login attempt: 1
2024-03-18 20:43:37:974 IBC: Click button: Paper Log In
2024-03-18 20:43:39:843 IBC: detected frame entitled: IBKR Gateway; event=Lost focus
2024-03-18 20:43:39:844 IBC: detected frame entitled: IBKR Gateway; event=Deactivated
2024-03-18 20:43:39:844 IBC: detected frame entitled: Loading...; event=Opened
2024-03-18 20:43:39:845 IBC: Found Gateway main window
2024-03-18 20:43:39:845 IBC: Got main window from future
2024-03-18 20:43:39:845 IBC: Invoking config dialog menu
2024-03-18 20:43:39:849 IBC: detected frame entitled: Loading...; event=Activated
2024-03-18 20:43:39:872 IBC: detected frame entitled: Loading...; event=Focused
2024-03-18 20:43:40:168 IBC: detected frame entitled: Authenticating...; event=Opened
2024-03-18 20:43:40:186 IBC: detected frame entitled: Loading...; event=Lost focus
2024-03-18 20:43:40:186 IBC: detected frame entitled: Loading...; event=Deactivated
2024-03-18 20:43:40:187 IBC: detected frame entitled: Authenticating...; event=Activated
2024-03-18 20:43:40:201 IBC: detected frame entitled: Authenticating...; event=Focused
2024-03-18 20:43:40:515 IBC: detected dialog entitled: Existing session detected; event=Opened
2024-03-18 20:43:40:516 IBC: Don't know the type of the other session, so continue this one (scenario 3)
2024-03-18 20:43:40:516 IBC: Click button: Continue Login
2024-03-18 20:43:40:592 IBC: detected frame entitled: Authenticating...; event=Lost focus
2024-03-18 20:43:40:593 IBC: detected frame entitled: Authenticating...; event=Deactivated
2024-03-18 20:43:40:593 IBC: detected dialog entitled: Existing session detected; event=Activated
2024-03-18 20:43:40:597 IBC: detected dialog entitled: Existing session detected; event=Focused
2024-03-18 20:43:40:598 IBC: detected dialog entitled: Existing session detected; event=Closed
2024-03-18 20:43:40:599 IBC: detected dialog entitled: Existing session detected; event=Lost focus
2024-03-18 20:43:40:599 IBC: detected dialog entitled: Existing session detected; event=Deactivated
2024-03-18 20:43:40:599 IBC: detected frame entitled: Authenticating...; event=Activated
2024-03-18 20:43:40:599 IBC: detected frame entitled: Authenticating...; event=Focused
LogModuleConfigurator-Init: Log4j Ver2.x found on classpath
LogModuleConfigurator-Init: LogModuleConfigurator initialized with Log4j Verd.x
2024-03-18 20:43:47:293 IBC: detected frame entitled: Starting application...; event=Lost focus
2024-03-18 20:43:47:365 IBC: detected dialog entitled: DU******* Trader Workstation Configuration (Simulated Trading); event=Opened
2024-03-18 20:43:47:366 IBC: Got config dialog from future
2024-03-18 20:43:47:366 IBC: Got config dialog from future
2024-03-18 20:43:47:367 IBC: detected frame entitled: Starting application...; event=Deactivated
2024-03-18 20:43:47:377 IBC: detected dialog entitled: DU******* Trader Workstation Configuration (Simulated Trading); event=Activated
2024-03-18 20:43:47:377 IBC: detected dialog entitled: DU******* Trader Workstation Configuration (Simulated Trading); event=Focused
2024-03-18 20:43:47:377 IBC: detected dialog entitled: DU******* Trader Workstation Configuration (Simulated Trading); event=Lost focus
2024-03-18 20:43:47:378 IBC: detected frame entitled: Starting application...; event=Closed
2024-03-18 20:43:47:378 IBC: Login has completed
2024-03-18 20:43:47:379 IBC: detected dialog entitled: Warning; event=Opened
2024-03-18 20:43:47:379 IBC: Click button: I understand and accept
2024-03-18 20:43:47:478 IBC: detected dialog entitled: DU******* Trader Workstation Configuration (Simulated Trading); event=Deactivated
2024-03-18 20:43:47:479 IBC: detected dialog entitled: Warning; event=Activated
2024-03-18 20:43:47:481 IBC: detected dialog entitled: Warning; event=Focused
2024-03-18 20:43:47:481 IBC: Setting Auto restart time
2024-03-18 20:43:47:530 IBC: Auto restart time set to 11:59 PM
2024-03-18 20:43:47:530 IBC: Setting ReadOnlyApi
2024-03-18 20:43:47:678 IBC: Read-Only API checkbox is now set to: false
2024-03-18 20:43:47:735 IBC: Configuration tasks completed
2024-03-18 20:43:47:735 IBC: Click button: OK
2024-03-18 20:43:47:875 IBC: detected dialog entitled: Warning; event=Lost focus
2024-03-18 20:43:47:876 IBC: detected dialog entitled: Warning; event=Deactivated
2024-03-18 20:43:47:876 IBC: detected dialog entitled: Warning; event=Closed
2024-03-18 20:43:47:941 IBC: detected dialog entitled: IBKR Gateway; event=Activated
2024-03-18 20:43:47:944 IBC: detected dialog entitled: IBKR Gateway; event=Focused
2024-03-18 20:43:47:945 IBC: detected dialog entitled: IBKR Gateway; event=Opened
2024-03-18 20:43:47:946 IBC: Click button: OK
2024-03-18 20:43:48:054 IBC: detected dialog entitled: Pending Tasks; event=Closed
2024-03-18 20:43:48:055 IBC: detected dialog entitled: IBKR Gateway; event=Lost focus
2024-03-18 20:43:48:056 IBC: detected dialog entitled: IBKR Gateway; event=Deactivated
2024-03-18 20:43:48:061 IBC: detected dialog entitled: IBKR Gateway; event=Closed
2024-03-18 20:43:48:062 IBC: detected dialog entitled: DU******* Trader Workstation Configuration (Simulated Trading); event=Closed
2024-03-18 20:43:56:445 IBC: detected frame entitled: Connecting to server...; event=Opened
2024-03-18 20:43:56:447 IBC: detected frame entitled: Connecting to server...; event=Activated
2024-03-18 20:43:56:452 IBC: detected frame entitled: Connecting to server...; event=Focused
2024-03-18 20:44:01:372 IBC: detected frame entitled: Attempt 2: Authenticating (trying for another 15 seconds)...; event=Lost focus
2024-03-18 20:44:01:372 IBC: detected frame entitled: Attempt 2: Authenticating (trying for another 15 seconds)...; event=Deactivated
2024-03-18 20:44:01:373 IBC: detected dialog entitled: Existing session detected; event=Activated
2024-03-18 20:44:01:374 IBC: detected dialog entitled: Existing session detected; event=Focused
2024-03-18 20:44:01:374 IBC: detected dialog entitled: Existing session detected; event=Opened
2024-03-18 20:44:01:375 IBC: Continue this session and let the other session exit (scenario 5)
2024-03-18 20:44:01:375 IBC: Click button: Reconnect This Session
2024-03-18 20:44:01:451 IBC: detected dialog entitled: Existing session detected; event=Lost focus
2024-03-18 20:44:01:451 IBC: detected dialog entitled: Existing session detected; event=Deactivated
2024-03-18 20:44:01:451 IBC: detected dialog entitled: Existing session detected; event=Closed
java.lang.InterruptedException
        at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:998)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1304)
        at java.util.concurrent.CountDownLatch.await(CountDownLatch.java:231)
        at com.sun.javafx.application.PlatformImpl.waitForStart(PlatformImpl.java:256)
        at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:277)
        at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:268)
        at com.sun.javafx.application.PlatformImpl.checkIdle(PlatformImpl.java:392)
        at com.sun.javafx.application.PlatformImpl.setImplicitExit(PlatformImpl.java:346)
        at javafx.application.Platform.setImplicitExit(Platform.java:138)
        at twslaunch.gstat.j.a(j.java:88)
        at twslaunch.gstat.a.d(a.java:215)
        at twslaunch.gstat.a.g(a.java:117)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
2024-03-18 20:44:01:585 IBC: detected frame entitled: Starting application...; event=Closed
2024-03-18 20:44:56:278 IBC: detected frame entitled: Connecting to server...; event=Activated
2024-03-18 20:44:56:280 IBC: detected frame entitled: Connecting to server...; event=Focused
2024-03-18 20:44:56:280 IBC: detected frame entitled: Connecting to server...; event=Opened
2024-03-18 20:44:56:716 IBC: detected dialog entitled: Existing session detected; event=Opened
2024-03-18 20:44:56:717 IBC: Continue this session and let the other session exit (scenario 5)
2024-03-18 20:44:56:717 IBC: Click button: Reconnect This Session
2024-03-18 20:44:56:793 IBC: detected frame entitled: Attempt 2: Authenticating...; event=Lost focus
2024-03-18 20:44:56:794 IBC: detected frame entitled: Attempt 2: Authenticating...; event=Deactivated
2024-03-18 20:44:56:794 IBC: detected dialog entitled: Existing session detected; event=Activated
2024-03-18 20:44:56:794 IBC: detected dialog entitled: Existing session detected; event=Focused
2024-03-18 20:44:56:797 IBC: detected dialog entitled: Existing session detected; event=Lost focus
2024-03-18 20:44:56:797 IBC: detected dialog entitled: Existing session detected; event=Deactivated
2024-03-18 20:44:56:797 IBC: detected dialog entitled: Existing session detected; event=Closed
2024-03-18 20:44:56:798 IBC: detected frame entitled: Attempt 2: Authenticating...; event=Activated
2024-03-18 20:44:56:799 IBC: detected frame entitled: Attempt 2: Authenticating...; event=Focused
java.lang.InterruptedException
        at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:998)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1304)
        at java.util.concurrent.CountDownLatch.await(CountDownLatch.java:231)
        at com.sun.javafx.application.PlatformImpl.waitForStart(PlatformImpl.java:256)
        at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:277)
        at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:268)
        at com.sun.javafx.application.PlatformImpl.checkIdle(PlatformImpl.java:392)
        at com.sun.javafx.application.PlatformImpl.setImplicitExit(PlatformImpl.java:346)
        at javafx.application.Platform.setImplicitExit(Platform.java:138)
        at twslaunch.gstat.j.a(j.java:88)
        at twslaunch.gstat.a.d(a.java:215)
        at twslaunch.gstat.a.g(a.java:117)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
2024-03-18 20:44:56:914 IBC: detected frame entitled: Starting application...; event=Lost focus
2024-03-18 20:44:56:914 IBC: detected frame entitled: Starting application...; event=Deactivated
2024-03-18 20:44:56:915 IBC: detected frame entitled: Starting application...; event=Closed
2024-03-18 20:45:56:391 IBC: detected frame entitled: Connecting to server...; event=Activated
2024-03-18 20:45:56:392 IBC: detected frame entitled: Connecting to server...; event=Focused
2024-03-18 20:45:56:392 IBC: detected frame entitled: Connecting to server...; event=Opened
2024-03-18 20:45:56:819 IBC: detected dialog entitled: Existing session detected; event=Opened
2024-03-18 20:45:56:819 IBC: Continue this session and let the other session exit (scenario 5)
2024-03-18 20:45:56:819 IBC: Click button: Reconnect This Session
2024-03-18 20:45:56:898 IBC: detected frame entitled: Attempt 2: Authenticating...; event=Lost focus
2024-03-18 20:45:56:898 IBC: detected frame entitled: Attempt 2: Authenticating...; event=Deactivated
2024-03-18 20:45:56:898 IBC: detected dialog entitled: Existing session detected; event=Activated
2024-03-18 20:45:56:900 IBC: detected dialog entitled: Existing session detected; event=Focused
2024-03-18 20:45:56:902 IBC: detected dialog entitled: Existing session detected; event=Lost focus
2024-03-18 20:45:56:902 IBC: detected dialog entitled: Existing session detected; event=Deactivated
2024-03-18 20:45:56:902 IBC: detected dialog entitled: Existing session detected; event=Closed
2024-03-18 20:45:56:902 IBC: detected frame entitled: Attempt 2: Authenticating...; event=Activated
2024-03-18 20:45:56:903 IBC: detected frame entitled: Attempt 2: Authenticating...; event=Focused
java.lang.InterruptedException
        at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:998)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1304)
        at java.util.concurrent.CountDownLatch.await(CountDownLatch.java:231)
        at com.sun.javafx.application.PlatformImpl.waitForStart(PlatformImpl.java:256)
        at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:277)
        at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:268)
        at com.sun.javafx.application.PlatformImpl.checkIdle(PlatformImpl.java:392)
        at com.sun.javafx.application.PlatformImpl.setImplicitExit(PlatformImpl.java:346)
        at javafx.application.Platform.setImplicitExit(Platform.java:138)
        at twslaunch.gstat.j.a(j.java:88)
        at twslaunch.gstat.a.d(a.java:215)
        at twslaunch.gstat.a.g(a.java:117)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
2024-03-18 20:45:57:018 IBC: detected frame entitled: Starting application...; event=Lost focus
2024-03-18 20:45:57:018 IBC: detected frame entitled: Starting application...; event=Deactivated
2024-03-18 20:45:57:019 IBC: detected frame entitled: Starting application...; event=Closed
gnzsnz commented 7 months ago

the log is clearly showing that you are not connecting, there are many authentication attempts. that explains why you can't connect with your python script on a separate container.

vnc will help you to identify this type of issues.

this is not a bug on the ib-gateway image, so i'm taking out the but label.

craigp318 commented 7 months ago

Seems like there was some issue associated with my paper account being signed in elsewhere. I logged in on the website, and logged out, then the docker image worked. Thanks for the tips and for ib-gateway-docker!