diff --git a/Dockerfile b/Dockerfile index 5333279..abf6940 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ FROM --platform=linux/amd64 debian:bullseye-slim RUN apt-get update -RUN apt-get install -y unzip wget x11vnc xvfb socat nano +RUN apt-get install -y unzip wget x11vnc xvfb socat net-tools nano WORKDIR /root @@ -37,11 +37,19 @@ RUN cp /opt/ibc/config.ini ./ibc/ RUN chmod a+x run_ibc.sh #Environment variables and ports +ENV IB_PORT 4000 +ENV GW_LIVE_PORT 4001 +ENV GW_PAPER_PORT 4002 +ENV TWS_LIVE_PORT 7496 +ENV TWS_PAPER_PORT 7497 ENV DISPLAY :0 -ENV TWS_PORT 4002 ENV VNC_PORT 5900 -EXPOSE $TWS_PORT + +EXPOSE $IB_PORT EXPOSE $VNC_PORT +#Enable healthcheck +HEALTHCHECK CMD netstat -ltn | grep -c ":$GW_LIVE_PORT\|:$GW_PAPER_PORT\|:$TWS_LIVE_PORT\|:$TWS_PAPER_PORT" + #Run IBC CMD ./run_ibc.sh diff --git a/README.md b/README.md index 0d457b8..9f973b5 100644 --- a/README.md +++ b/README.md @@ -31,5 +31,8 @@ Or run directly with `docker run` command: ## Connection Use container address and port **4002** to connect to the IB application. +## Healthcheck +The `netstat` utility is included and used to check IB socket port. You can customize healthcheck parameters and have the container automatically restart on unhealthy status. Please read about [autoheal](https://github.com/willfarrell/docker-autoheal) and see example `docker-compose.yml`. + ## Disclaimer This method of deploying TWS/Gateway is not endorsed nor supported by Interactive Brokers. diff --git a/docker-compose.yml b/docker-compose.yml index cc6e99e..4d7664e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,13 @@ version: '3.9' services: + autoheal: + container_name: autoheal + image: willfarrell/autoheal + restart: unless-stopped + volumes: + - /var/run/docker.sock:/var/run/docker.sock + ib: container_name: ib image: zdytch/ib-dockerized @@ -16,3 +23,9 @@ services: - ./config.ini:/root/ibc/config.ini ports: - 5900:5900 + labels: + autoheal: "true" + healthcheck: + interval: 15s + timeout: 5s + retries: 4 diff --git a/run_ibc.sh b/run_ibc.sh index 47bd2fd..082d2af 100644 --- a/run_ibc.sh +++ b/run_ibc.sh @@ -8,6 +8,31 @@ rm -f /tmp/.X0-lock Xvfb :0 & sleep 1 + +#Prepare arguments and port for Gateway or TWS +APP_ARGS="--mode=$IB_MODE --user=$IB_USER --pw=$IB_PASSWORD" +INT_PORT=0 + +if [ $IB_APP = "gw" ] ; then + APP_ARGS="$(ls ~/Jts/ibgateway) --gateway $APP_ARGS" + + if [ $IB_MODE = "live" ] ; then + INT_PORT=$GW_LIVE_PORT + elif [ $IB_MODE = "paper" ] ; then + INT_PORT=$GW_PAPER_PORT + fi + +elif [ $IB_APP = "tws" ] ; then + APP_ARGS="$(ls ~/Jts | head -1) $APP_ARGS" + + if [ $IB_MODE = "live" ] ; then + INT_PORT=$TWS_LIVE_PORT + elif [ $IB_MODE = "paper" ] ; then + INT_PORT=$TWS_PAPER_PORT + fi +fi + + #Run VNC if password is set if [ $VNC_PASSWORD ] ; then mkdir -p .vnc @@ -15,17 +40,9 @@ if [ $VNC_PASSWORD ] ; then x11vnc -rfbport $VNC_PORT -display :0 -usepw -forever & fi -#Forward TWS port from to 4001, to simulate local connection -socat TCP-LISTEN:$TWS_PORT,fork TCP:localhost:4001,forever & -#Prepare arguments for Gateway or TWS -APP_ARGS="--mode=$IB_MODE --user=$IB_USER --pw=$IB_PASSWORD" -if [ $IB_APP = "gw" ] -then - APP_ARGS="$(ls ~/Jts/ibgateway) --gateway $APP_ARGS" -elif [ $IB_APP = "tws" ] -then - APP_ARGS="$(ls ~/Jts | head -1) $APP_ARGS" -fi +#Forward IB port from internal to external, to simulate local connection +socat TCP-LISTEN:$IB_PORT,fork TCP:localhost:$INT_PORT,forever & + /opt/ibc/scripts/ibcstart.sh $APP_ARGS