How can I connect to the IB Gateway with ib_async running in a docker container? #177
Replies: 4 comments
-
|
the key question here is what is your ib-gateway instance IP, and if it's listening to 127.0.0.1 like it does by default. with the extra twist that from within your docker container 127.0.0.1 is not going to help. If you run ib-gateway and your code in docker, the put them in a docker compose service services:
basic:
... your things here
networks:
- algo
gateway:
...
networks:
- algo
networks:
algo:
name: algothen from your code ...
# hostname == service name
# port 4004 for paper in https://github.com/gnzsnz/ib-gateway-docker?tab=readme-ov-file#ports
ib = ib_async.IB().connect("gateway",4004,10)
...Every combination is different. the key is that ib-gateway and TWS listen on 127.0.0.1 so you need your code to be able to access 127.0.0.1, you can't do that within docker. the docker image that you pointed uses netcat to forward the port to an external IP, you either use that IP, or the service name like in the example above. |
Beta Was this translation helpful? Give feedback.
-
|
@gnzsnz Thank you for your reply! I tried to replicate your example by putting everything in the docker network and use IB Gateway in the docker container logs: However, if I run my python code not from within my It all works and immediately API connection check succeeds. For some reason it's when my python code runs in the docker container that causes this issue. Would you possibly have any other ideas on what could be wrong in my set up and why running it in a docker container breaks it? set up with issue
|
Beta Was this translation helpful? Give feedback.
-
|
@gnzsnz Nevermind I think I found the issue 😅 In my python docker container I had a bash script entrypoint that runs a check through Using this command to check if the port is open, and then quickly after running the python app to connect to that port with The fix was to add some time between the Sorry for the bother, and thanks for all the help! 🙏 |
Beta Was this translation helpful? Give feedback.
-
|
your code is probably causing troubles. https://ib-api-reloaded.github.io/ib_async/api.html#module-ib_async.ib please follow "The One Rule" this is creating problems |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello thanks for the awesome tool!
I am running my
ib_asyncpython code in a docker container and trying to connect it to my IB Gateway app, but I am having some trouble and feel like I'm missing something in the setup.Does anyone have any guidance on how to connect to the IB Gateway through
ib_asynccode that is running in a docker container?I am facing this issue when trying to connect to the IB Gateway app which is directly running on my host machine (Mac OS) or even when it's in another docker container using ib-gateway and connecting through the docker network. To try to simplify my issue though, below I'll explain the issue only when running IB Gateway app on my host machine.
Any help would be much appreciated, thank you!
Issue
(see below for my minimal reproducible set up)
It seems that my from inside the docker container it can find the IB Gateway port 4002 open using
nccommand. The functionib_async.connectwill also connect successfully to the IB Gateway socket. However, if I'm not mistaken, after this I am thinking thatib_asyncwill hang and never receive any data from the socket and therefore not complete the API connection and timeout at the await asyncio.wait_for(self.apiStart, timeout) part.basic.py logsHowever if I run the python code outside of the docker container and directly on my host machine, then the initial connection and API connection in
ib_asyncquickly succeeds, and everything works as expected.Minimal reproducible set up
docker-compose.ymlDockerfile for basicentrypoint.shbasic.pyIB Gateway settings
OFF4002OFFBeta Was this translation helpful? Give feedback.
All reactions