Skip to content

Commit

Permalink
Dockerize python-threatexchange (#1291)
Browse files Browse the repository at this point in the history
* Define a basic Docker container based on the official Python container
* Enable persistent config and state with a Docker volume
* Make the state dir configurable via environment variable `TX_STATEDIR`
  • Loading branch information
dougneal committed Mar 28, 2023
1 parent 7f5eeb8 commit 991ef58
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
12 changes: 12 additions & 0 deletions python-threatexchange/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM python:3.11-bullseye

WORKDIR /usr/src/threatexchange
COPY . .
RUN pip install .
RUN rm -rf /usr/src/threatexchange

ENV TX_STATEDIR=/var/lib/threatexchange

VOLUME ["/var/lib/threatexchange"]
CMD ["threatexchange"]
ENTRYPOINT ["/usr/local/bin/threatexchange"]
21 changes: 21 additions & 0 deletions python-threatexchange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ To get similar functionality in a deployable service, check out hasher-matcher-a

![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/facebook/ThreatExchange/python-threatexchange-ci.yaml?branch=main) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/threatexchange) ![PyPI - Downloads](https://img.shields.io/pypi/dm/threatexchange) ![PyPI](https://img.shields.io/pypi/v/threatexchange) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

## Run the CLI in Docker container

A Dockerfile is provided which allows you to run the CLI with minimal dependencies.

First build the container:
```
$ docker build --tag threatexchange .
```

Then run:
```
$ docker run threatexchange
```

To persist the configuration and data between invocations, mount the `/var/lib/threatexchange` volume:

```
$ docker run --volume $HOME/.threatexchange:/var/lib/threatexchange
```


## Installation

If you don't have `pip`, learn how to install it [here](https://pip.pypa.io/en/stable/installation/).
Expand Down
4 changes: 3 additions & 1 deletion python-threatexchange/threatexchange/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ def _setup_logging(level_str: str, *, initial: bool = False) -> None:

def inner_main(
args: t.Optional[t.Sequence[t.Text]] = None,
state_dir: pathlib.Path = pathlib.Path("~/.threatexchange"),
state_dir: pathlib.Path = pathlib.Path(
os.getenv("TX_STATEDIR", "~/.threatexchange")
),
) -> None:
"""The main called by tests"""
config = CliState(
Expand Down

0 comments on commit 991ef58

Please sign in to comment.