Skip to content

Commit

Permalink
Use python base image & add more input args to allow changing mount d…
Browse files Browse the repository at this point in the history
…irectory

Fixes #7563, #7560
  • Loading branch information
jutoft committed Aug 19, 2023
1 parent b7cfa8a commit f515555
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 29 deletions.
36 changes: 18 additions & 18 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
# The base image already builds the libtorrent dependency so only Python pip packages
# are necessary to be installed to run Tribler core process.
FROM triblercore/libtorrent:1.2.10-x
# libtorrent-1.2.9 does not support python 3.11 yet
FROM python:3.10-slim

# Update the base system and install required libsodium and Python pip
RUN apt update && apt upgrade -y
RUN apt install -y libsodium23 python3-pip git

RUN useradd -ms /bin/bash user
USER user
WORKDIR /home/user
RUN apt-get update \

Check notice on line 4 in Dockerfile

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Dockerfile#L4

Avoid additional packages by specifying `--no-install-recommends`

Check warning on line 4 in Dockerfile

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Dockerfile#L4

Pin versions in apt get install. Instead of `apt-get install <package>` use `apt-get install <package>=<version>`
&& apt-get install -y libsodium23 \
&& rm -rf /var/lib/apt/lists/*

# Then, install pip dependencies so that it can be cached and does not
# need to be built every time the source code changes.
# This reduces the docker build time.
RUN mkdir requirements
COPY ./requirements-core.txt requirements/core-requirements.txt
RUN pip3 install -r requirements/core-requirements.txt
COPY ./requirements-core.txt /app/tribler/core-requirements.txt
RUN pip3 install -r /app/tribler/core-requirements.txt

# Copy the source code and set the working directory
COPY ./ tribler
WORKDIR /home/user/tribler
COPY ./src /app/tribler/src/
WORKDIR /app/tribler/

# Set the REST API port and expose it
# Set to -1 to use the default
ENV CORE_API_PORT=20100
EXPOSE 20100
ENV IPV8_PORT=7759
ENV TORRENT_PORT=-1
ENV DOWNLOAD_DIR=/downloads
ENV TSTATEDIR=/state

VOLUME /state
VOLUME /downloads

# Only run the core process with --core switch
CMD ["./src/tribler.sh", "--core"]
CMD exec python3 /app/tribler/src/run_tribler_headless.py --restapi_http_port=${CORE_API_PORT} --ipv8=${IPV8_PORT} --libtorrent=${TORRENT_PORT} --restapi_http_host=0.0.0.0 --restapi_https_host=0.0.0.0 "--statedir=${TSTATEDIR}" "--downloaddir=${DOWNLOAD_DIR}"
11 changes: 9 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ services:
network_mode: "host"
build: .
volumes:
- ~/.Tribler:/home/user/.Tribler
- "~/.Tribler:/state"
- "~/TriblerDownloads:/downloads"
environment:
CORE_API_PORT: 20100

CORE_API_KEY: TEST
TORRENT_PORT: 7000
TSTATEDIR: "/state"
ports:
- "20100:20100" # core api port
- "7000:7000" # torrent listen port
- "7759:7759" # ipv8 listen port
30 changes: 26 additions & 4 deletions src/run_tribler_headless.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,24 @@ async def signal_handler(sig):

print("Starting Tribler")

if options.restapi > 0:
if options.restapi_http_port > 0 or options.restapi_https_port > 0:
config.api.http_enabled = True
config.api.http_port = options.restapi

if options.restapi_http_port > 0:
config.api.http_port = options.restapi_http_port

Check notice on line 97 in src/run_tribler_headless.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/run_tribler_headless.py#L97

Trailing whitespace
if options.restapi_http_host:
config.api.http_host = options.restapi_http_host

if options.restapi_https_port > 0:
config.api.https_port = options.restapi_https_port

if options.restapi_https_host:
config.api.https_host = options.restapi_https_host

api_key = os.environ.get('CORE_API_KEY')
if api_key and (config.api.key != api_key):
config.api.key = api_key

if options.ipv8 > 0:
config.ipv8.port = options.ipv8
Expand All @@ -101,6 +116,9 @@ async def signal_handler(sig):
if options.libtorrent != -1 and options.libtorrent > 0:
config.libtorrent.port = options.libtorrent

if options.downloaddir:
config.download_defaults.saveas = options.downloaddir

if options.ipv8_bootstrap_override is not None:
config.ipv8.bootstrap_override = options.ipv8_bootstrap_override

Expand Down Expand Up @@ -129,7 +147,11 @@ def main(argv):
parser.add_argument('--help', '-h', action='help', default=argparse.SUPPRESS,
help='Show this help message and exit')
parser.add_argument('--statedir', '-s', default=None, help='Use an alternate statedir')
parser.add_argument('--restapi', '-p', default=-1, type=int, help='Use an alternate port for REST API')
parser.add_argument('--downloaddir', default=None, help='Use an alternative download directory')
parser.add_argument('--restapi_http_port', '--restapi', '-p', default=-1, type=int, help='Use an alternate port for http REST API')

Check notice on line 151 in src/run_tribler_headless.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/run_tribler_headless.py#L151

Line too long (135/120)
parser.add_argument('--restapi_http_host', default=None, type=str, help='Use an alternate listen address for http REST API')

Check notice on line 152 in src/run_tribler_headless.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/run_tribler_headless.py#L152

Line too long (128/120)
parser.add_argument('--restapi_https_port', default=-1, type=int, help='Use an alternate port for https REST API')
parser.add_argument('--restapi_https_host', default=None, type=str, help='Use an alternate listen address for https REST API')

Check notice on line 154 in src/run_tribler_headless.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/run_tribler_headless.py#L154

Line too long (130/120)
parser.add_argument('--ipv8', '-i', default=-1, type=int, help='Use an alternate port for the IPv8')
parser.add_argument('--libtorrent', '-l', default=-1, type=int, help='Use an alternate port for libtorrent')
parser.add_argument('--ipv8_bootstrap_override', '-b', default=None, type=str,
Expand All @@ -146,7 +168,7 @@ def main(argv):
coro = service.start_tribler(args)
ensure_future(coro)

if sys.platform == 'win32':
if sys.platform == 'win32' and sys.version < 3.8:
# Unfortunately, this is needed on Windows for Ctrl+C to work consistently.
# Should no longer be needed in Python 3.8.
async def wakeup():
Expand Down
8 changes: 3 additions & 5 deletions src/tribler/core/components/restapi/rest/rest_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ def __init__(self, config: APISettings, root_endpoint: RootEndpoint, state_dir=N
self.config = config
self.state_dir = state_dir

self.http_host = '127.0.0.1'
self.https_host = '0.0.0.0'
self.shutdown_timeout = shutdown_timeout

def get_endpoint(self, name):
Expand Down Expand Up @@ -152,8 +150,8 @@ async def start(self):
self._logger.info('Https enabled')
await self.start_https_site()

self._logger.info(f'Swagger docs: http://{self.http_host}:{self.config.http_port}/docs')
self._logger.info(f'Swagger JSON: http://{self.http_host}:{self.config.http_port}/docs/swagger.json')
self._logger.info(f'Swagger docs: http://{self.config.http_host}:{self.config.http_port}/docs')
self._logger.info(f'Swagger JSON: http://{self.config.http_host}:{self.config.http_port}/docs/swagger.json')

async def start_http_site(self):
api_port = max(self.config.http_port, 0) # if the value in config is -1 we convert it to 0
Expand Down Expand Up @@ -183,7 +181,7 @@ async def start_https_site(self):
ssl_context.load_cert_chain(cert)

port = self.config.https_port
self.site_https = web.TCPSite(self.runner, self.https_host, port, ssl_context=ssl_context)
self.site_https = web.TCPSite(self.runner, self.config.https_host, port, ssl_context=ssl_context)

await self.site_https.start()
self._logger.info("Started HTTPS REST API: %s", self.site_https.name)
Expand Down
2 changes: 2 additions & 0 deletions src/tribler/core/components/restapi/rest/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
class APISettings(TriblerConfigSection):
http_enabled: bool = False
http_port: int = -1
http_host: str = "127.0.0.1"
https_enabled: bool = False
https_host: str = "0.0.0.0"

Check warning on line 14 in src/tribler/core/components/restapi/rest/settings.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/tribler/core/components/restapi/rest/settings.py#L14

Possible binding to all interfaces.
https_port: int = -1
https_certfile: str = ''
key: Optional[str] = None
Expand Down

0 comments on commit f515555

Please sign in to comment.