Skip to content

Commit 4e96f88

Browse files
committed
Signed-off-by: gnzsnz <[email protected]>
1 parent bc39ca1 commit 4e96f88

File tree

16 files changed

+813
-286
lines changed

16 files changed

+813
-286
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,33 @@ assignees: ''
77

88
---
99

10-
**Describe the bug**
10+
## Describe the bug
11+
1112
A clear and concise description of what the bug is.
1213

13-
**To Reproduce**
14-
Steps to reproduce the behavior.
14+
## To Reproduce
15+
16+
Steps to reproduce the behavior. Please include information related to docker, ex docker run command, .env file, docker-compose.yml.
17+
18+
Please provide the output of `docker compose config`
1519

16-
**Expected behavior**
20+
## Expected behavior
1721
A clear and concise description of what you expected to happen.
1822

19-
**Container logs**
23+
## Container logs
2024
If applicable, add the container logs `docker logs <CONTAINER>` or `docker-compose logs` to help explain your problem.
2125

22-
**Versions (please complete the following information):**
26+
## Versions
27+
28+
Please complete the following information:
2329
- OS: [e.g. Windows]
2430
- Docker version: [e.g. chrome, safari]
25-
- Image Tag (`docker --version`): [e.g. latest]
31+
- Image Tag (`docker image inspect ghcr.io/gnzsnz/ib-gateway:tag`): [e.g. latest]
2632
- Image Digest (`docker images --digests`): [e.g. sha256:60d9d54009b1b66908bbca1ebf5b8a03a39fe0cb35c2ab4023f6e41b55d17894]
2733

28-
**Additional context**
34+
## Additional context
2935
Add any other context about the problem here.
36+
37+
What have you tried and failed.
38+
39+
My primary objective is to fix any bug on the container, ex Dockerfile, run.sh script, docker-compose.yml. Please don't expect upstream issues to be solved here (ex. IB gateway, IBC, etc)

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/.env
22
tws_settings/
3+
ssh/

Dockerfile.template

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
2-
#
1+
##############################################################################
32
# Setup Stage: install apps
43
#
54
# This is a dedicated stage so that donwload archives don't end up on
65
# production image and consume unnecessary space.
7-
#
6+
##############################################################################
87

98
FROM ubuntu:22.04 as setup
109

@@ -20,6 +19,8 @@ RUN apt-get update -y && \
2019
curl \
2120
ca-certificates \
2221
unzip && \
22+
apt-get clean && \
23+
rm -rf /var/lib/apt/lists/* && \
2324
# Install IB Gateway
2425
# Use this instead of "RUN curl .." to install a local file:
2526
#COPY ibgateway-${IB_GATEWAY_VERSION}-standalone-linux-x64.sh .
@@ -41,36 +42,44 @@ COPY ./config/ibc/config.ini.tmpl /root/ibc/config.ini.tmpl
4142
# Copy scripts
4243
COPY ./scripts /root/scripts
4344

44-
#
45+
##############################################################################
4546
# Build Stage: build production image
46-
#
47+
##############################################################################
4748

4849
FROM ubuntu:22.04
4950

5051
ENV IB_GATEWAY_VERSION=$VERSION
51-
52-
WORKDIR /root
52+
# IB Gateway user constants
53+
ARG USER_ID="${USER_ID:-1000}"
54+
ARG USER_GID="${USER_GID:-1000}"
55+
# IBC env vars
56+
ENV TWS_MAJOR_VRSN=${IB_GATEWAY_VERSION}
57+
ENV TWS_PATH=/home/ibgateway/Jts
58+
ENV TWS_INI=jts.ini
59+
ENV IBC_PATH=/home/ibgateway/ibc
60+
ENV IBC_INI=/home/ibgateway/ibc/config.ini
61+
# Copy files
5362
COPY --from=setup /usr/local/i4j_jres/ /usr/local/i4j_jres
63+
COPY --chown=${USER_ID}:${USER_GID} --from=setup /root/ /home/ibgateway/
5464

5565
# Prepare system
5666
RUN apt-get update -y && \
57-
apt-get install --no-install-recommends --yes \
58-
gettext-base \
59-
socat \
60-
xvfb \
61-
x11vnc \
62-
&& rm -rf /var/lib/apt/lists/*
63-
64-
# Copy files
65-
COPY --from=setup /root/ .
66-
RUN chmod a+x /root/scripts/*.sh
67+
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends --yes \
68+
gettext-base socat xvfb x11vnc sshpass openssh-client && \
69+
apt-get clean && \
70+
rm -rf /var/lib/apt/lists/* && \
71+
groupadd --gid ${USER_GID} ibgateway && \
72+
useradd -ms /bin/bash --uid ${USER_ID} --gid ${USER_GID} ibgateway && \
73+
chmod a+x /home/ibgateway/scripts/*.sh
6774

68-
# IBC env vars
69-
ENV TWS_MAJOR_VRSN ${IB_GATEWAY_VERSION}
70-
ENV TWS_PATH /root/Jts
71-
ENV TWS_INI jts.ini
72-
ENV IBC_PATH /root/ibc
73-
ENV IBC_INI /root/ibc/config.ini
75+
USER ${USER_ID}:${USER_GID}
76+
WORKDIR /home/ibgateway/
7477

7578
# Start run script
76-
CMD ["/root/scripts/run.sh"]
79+
CMD ["/home/ibgateway/scripts/run.sh"]
80+
81+
LABEL org.opencontainers.image.source=https://github.com/gnzsnz/ib-gateway-docker
82+
LABEL org.opencontainers.image.url=https://github.com/gnzsnz/ib-gateway-docker/pkgs/container/ib-gateway
83+
LABEL org.opencontainers.image.description="Docker image with IB Gateway and IBC "
84+
LABEL org.opencontainers.image.licenses="Apache License Version 2.0"
85+
LABEL org.opencontainers.image.version=${IB_GATEWAY_VERSION}-${IB_GATEWAY_RELEASE_CHANNEL}

README.md

Lines changed: 232 additions & 97 deletions
Large diffs are not rendered by default.

docker-compose.yml

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ services:
44
ib-gateway:
55
restart: always
66
build:
7-
context: ./stable
7+
context: ./latest
88
tags:
9-
- "ghcr.io/gnzsnz/ib-gateway:stable"
10-
image: ghcr.io/gnzsnz/ib-gateway:stable
9+
- "ghcr.io/gnzsnz/ib-gateway:latest"
10+
image: ghcr.io/gnzsnz/ib-gateway:latest
1111
environment:
1212
TWS_USERID: ${TWS_USERID}
1313
TWS_PASSWORD: ${TWS_PASSWORD}
@@ -21,11 +21,21 @@ services:
2121
TWOFA_EXIT_INTERVAL: ${TWOFA_EXIT_INTERVAL:-60}
2222
TIME_ZONE: ${TIME_ZONE:-Etc/UTC}
2323
CUSTOM_CONFIG: ${CUSTOM_CONFIG:-NO}
24+
SSH_TUNNEL: ${SSH_TUNNEL:-}
25+
SSH_OPTIONS: ${SSH_OPTIONS:-}
26+
SSH_ALIVE_INTERVAL: ${SSH_ALIVE_INTERVAL:-}
27+
SSH_ALIVE_COUNT: ${SSH_ALIVE_COUNT:-}
28+
SSH_PASSPHRASE: ${SSH_PASSPHRASE:-}
29+
SSH_REMOTE_PORT: ${SSH_REMOTE_PORT:-}
30+
SSH_USER_TUNNEL: ${SSH_USER_TUNNEL:-}
31+
SSH_RESTART: ${SSH_RESTART:-}
32+
SSH_VNC_PORT: ${SSH_VNC_PORT:-}
2433
# volumes:
25-
# - ${PWD}/jts.ini:/root/Jts/jts.ini
26-
# - ${PWD}/config.ini:/root/ibc/config.ini
27-
# - ${PWD}/tws_settings/:${TWS_SETTINGS_PATH:-/root/Jts}
34+
# - ${PWD}/jts.ini:/home/ibgateway/Jts/jts.ini
35+
# - ${PWD}/config.ini:/home/ibgateway/ibc/config.ini
36+
# - ${PWD}/tws_settings/:${TWS_SETTINGS_PATH:-/home/ibgateway/Jts}
37+
# - ${PWD}/ssh/:/home/ibgateway/.ssh
2838
ports:
29-
- "127.0.0.1:4001:4001"
30-
- "127.0.0.1:4002:4002"
39+
- "127.0.0.1:4001:4003"
40+
- "127.0.0.1:4002:4004"
3141
- "127.0.0.1:5900:5900"

image-files/config/ibc/config.ini.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ LoginDialogDisplayTimeout=60
264264
# NB: this setting is now DEPRECATED. You should use the
265265
# TWS_SETTINGS_PATH setting in the relevant start script.
266266

267-
IbDir=/root/Jts
267+
#IbDir=/home/ibgateway/Jts
268268

269269

270270
# Store settings on server
@@ -340,7 +340,7 @@ ExistingSessionDetectedAction=primary
340340
# non-FIX users will never need it, so don't use it unless you know
341341
# you need it.
342342

343-
OverrideTwsApiPort=4000
343+
OverrideTwsApiPort=
344344

345345

346346
# Override TWS Master Client ID
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
3+
if [ "$SSH_TUNNEL" = "yes" ]; then
4+
5+
if [ "$TRADING_MODE" = "paper" ] ; then
6+
SSH_LOCAL_PORT=4002
7+
else
8+
SSH_LOCAL_PORT=4001
9+
fi
10+
11+
if [ -z "$SSH_REMOTE_PORT" ]; then
12+
SSH_REMOTE_PORT="$SSH_LOCAL_PORT"
13+
fi
14+
15+
if [ -n "$SSH_VNC_PORT" ] && [ -n "$VNC_SERVER_PASSWORD" ]
16+
then
17+
SSH_VNC_TUNNEL="-R 127.0.0.1:5900:localhost:$SSH_VNC_PORT"
18+
fi
19+
20+
while true
21+
do
22+
echo "> ssh sock: $SSH_AUTH_SOCK"
23+
bash -c "ssh ${SSH_ALL_OPTIONS} -TNR 127.0.0.1:${SSH_LOCAL_PORT}:localhost:${SSH_REMOTE_PORT} ${SSH_VNC_TUNNEL:-} ${SSH_USER_TUNNEL}"
24+
sleep "${SSH_RESTART:-5}"
25+
done
26+
27+
else
28+
# no ssh tunnel, start socat
29+
sleep 30
30+
#
31+
if [ "$TRADING_MODE" = "paper" ]; then
32+
# paper
33+
printf "Forking :::4002 onto 0.0.0.0:4004 > trading mode %s \n" \
34+
"${TRADING_MODE}"
35+
socat TCP-LISTEN:4004,fork TCP:127.0.0.1:4002
36+
else
37+
# live
38+
printf "Forking :::4001 onto 0.0.0.0:4003 > trading mode %s \n" \
39+
"${TRADING_MODE}"
40+
socat TCP-LISTEN:4003,fork TCP:127.0.0.1:4001
41+
fi
42+
fi

image-files/scripts/run.sh

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,105 @@
1-
#!/bin/sh
1+
#!/bin/bash
22

33
export DISPLAY=:1
44

5+
stop() {
6+
echo "> 😘 Received SIGINT or SIGTERM. Shutting down IB Gateway."
7+
8+
#
9+
if [ -n "$VNC_SERVER_PASSWORD" ]; then
10+
echo "> Stopping x11vnc."
11+
pkill x11vnc
12+
fi
13+
#
14+
echo "> Stopping Xvfb."
15+
pkill Xvfb
16+
#
17+
if [ -n "$SSH_TUNNEL" ]; then
18+
echo "> Stopping ssh."
19+
pkill ssh
20+
else
21+
echo "> Stopping socat."
22+
pkill socat
23+
fi
24+
# Get PID
25+
local pid
26+
pid=$(</tmp/pid)
27+
# Set TERM
28+
echo "> Stopping IBC."
29+
kill -SIGTERM "${pid}"
30+
# Wait for exit
31+
wait "${pid}"
32+
# All done.
33+
echo "> Done... $?"
34+
}
35+
36+
# start Xvfb
537
rm -f /tmp/.X1-lock
638
Xvfb $DISPLAY -ac -screen 0 1024x768x16 &
739

40+
# setup SSH Tunnel
41+
if [ "$SSH_TUNNEL" = "yes" ]; then
42+
43+
_SSH_OPTIONS="-o ServerAliveInterval=${SSH_ALIVE_INTERVAL:-20}"
44+
_SSH_OPTIONS+=" -o ServerAliveCountMax=${SSH_ALIVE_COUNT:-3}"
45+
46+
if [ -n "$SSH_OPTIONS" ]; then
47+
_SSH_OPTIONS+=" $SSH_OPTIONS"
48+
fi
49+
SSH_ALL_OPTIONS="$_SSH_OPTIONS"
50+
export SSH_ALL_OPTIONS
51+
echo "> SSH options: $SSH_ALL_OPTIONS"
52+
53+
if [ -n "$SSH_PASSPHRASE" ]; then
54+
echo "> Starting ssh-agent."
55+
export SSH_ASKPASS_REQUIRE="never"
56+
eval "$(ssh-agent)"
57+
SSHPASS="${SSH_PASSPHRASE}" sshpass -e -P "passphrase" ssh-add
58+
echo "> ssh-agent identities: $(ssh-add -l)"
59+
fi
60+
fi
61+
62+
# start VNC server
863
if [ -n "$VNC_SERVER_PASSWORD" ]; then
9-
echo "Starting VNC server"
10-
/root/scripts/run_x11_vnc.sh &
64+
echo "> Starting VNC server"
65+
/home/ibgateway/scripts/run_x11_vnc.sh &
1166
fi
1267

13-
if [ "$CUSTOM_CONFIG" != "YES" ]; then
68+
# apply settings
69+
if [ "$CUSTOM_CONFIG" != "yes" ]; then
1470
# replace env variables
1571
envsubst < "${IBC_INI}.tmpl" > "${IBC_INI}"
1672

1773
# where are settings stored
1874
if [ -n "$TWS_SETTINGS_PATH" ]; then
1975
_JTS_PATH=$TWS_SETTINGS_PATH
76+
if [ ! -d "$TWS_SETTINGS_PATH" ]; then
77+
# if TWS_SETTINGS_PATH does not exists, create it
78+
echo "> Creating directory: $TWS_SETTINGS_PATH"
79+
mkdir "$TWS_SETTINGS_PATH"
80+
fi
2081
else
2182
_JTS_PATH=$TWS_PATH
2283
fi
23-
2484
# only if jts.ini does not exists
2585
if [ ! -f "$_JTS_PATH/$TWS_INI" ]; then
2686
echo "Setting timezone in ${_JTS_PATH}/${TWS_INI}"
2787
envsubst < "${TWS_PATH}/${TWS_INI}.tmpl" > "${_JTS_PATH}/${TWS_INI}"
2888
fi
29-
3089
fi
3190

32-
/root/scripts/fork_ports_delayed.sh &
91+
# forward ports, socat or ssh
92+
/home/ibgateway/scripts/port_forwarding.sh &
3393

34-
/root/ibc/scripts/ibcstart.sh "${TWS_MAJOR_VRSN}" -g \
94+
# start IBC
95+
/home/ibgateway/ibc/scripts/ibcstart.sh "${TWS_MAJOR_VRSN}" -g \
3596
"--tws-path=${TWS_PATH}" \
3697
"--ibc-path=${IBC_PATH}" "--ibc-ini=${IBC_INI}" \
3798
"--on2fatimeout=${TWOFA_TIMEOUT_ACTION}" \
38-
"--tws-settings-path=${TWS_SETTINGS_PATH:-}"
99+
"--tws-settings-path=${TWS_SETTINGS_PATH:-}" &
100+
101+
pid="$!"
102+
echo "$pid" > /tmp/pid
103+
trap stop SIGINT SIGTERM
104+
wait "${pid}"
105+
exit $?

image-files/scripts/run_x11_vnc.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
#!/bin/sh
1+
#!/bin/bash
22

3-
x11vnc -ncache_cr -display :1 -forever -shared -logappend /var/log/x11vnc.log -bg -noipv6 -passwd "$VNC_SERVER_PASSWORD"
3+
x11vnc -ncache_cr -display :1 -forever -shared -bg -noipv6 -passwd "$VNC_SERVER_PASSWORD"

latest/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
FROM ubuntu:22.04 as setup
1010

11-
ENV IB_GATEWAY_VERSION=10.25.1m
11+
ENV IB_GATEWAY_VERSION=10.25.1n
1212
ENV IB_GATEWAY_RELEASE_CHANNEL=latest
1313
ENV IBC_VERSION=3.18.0
1414

@@ -47,7 +47,7 @@ COPY ./scripts /root/scripts
4747

4848
FROM ubuntu:22.04
4949

50-
ENV IB_GATEWAY_VERSION=10.25.1m
50+
ENV IB_GATEWAY_VERSION=10.25.1n
5151

5252
WORKDIR /root
5353
COPY --from=setup /usr/local/i4j_jres/ /usr/local/i4j_jres

0 commit comments

Comments
 (0)