From 21a04124f26d5df72ac835f13b79d8cf8d8e22ea Mon Sep 17 00:00:00 2001 From: 17ms Date: Sun, 7 Jul 2024 17:18:35 +0300 Subject: [PATCH] feat: dockerfile and deployment scripts --- Dockerfile | 13 +++++++++++++ data/.gitkeep | 0 scripts/deploy.sh | 23 +++++++++++++++++++++++ scripts/healthcheck.sh | 9 +++++++++ 4 files changed, 45 insertions(+) create mode 100644 Dockerfile create mode 100644 data/.gitkeep create mode 100755 scripts/deploy.sh create mode 100644 scripts/healthcheck.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d8fd7d2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.11-slim + +WORKDIR /app +COPY requirements.txt requirements.txt +RUN pip install --no-cache-dir -r requirements.txt +COPY . . + +# Basic DB healthcheck +COPY ./scripts/healthcheck.sh /app/healthcheck.sh +RUN chmod +x /app/healthcheck.sh +HEALTHCHECK --interval=120s --timeout=10s --start-period=5s --retries=3 CMD /app/healthcheck.sh + +ENTRYPOINT ["python", "main.py"] diff --git a/data/.gitkeep b/data/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100755 index 0000000..375f550 --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +echo "[+] Starting the deployment script" + +! command -v docker &> /dev/null && echo "[!] Docker could not be found, exiting..." && exit 1 + +# Building with '--no-cache' ensures a fresh build will always be used +echo -e "\n[+] Building the Docker image without caching..." +docker build --no-cache -t chainmapper . + +[ ! -d "./data" ] && mkdir data && echo -e "\n[+] Created the default volume directory 'data'" + +OLD_ID=$(docker ps -a -q -f name="chainmapper-prod") + +if [ "$OLD_ID" ] +then + read -p "[?] Existing container found with id '$OLD_ID', do you want to remove it? " -n 1 -r + [[ "$REPLY" =~ ^[Yy]$ ]] || (echo "[!] Exiting..." && exit 0) + docker rm "$OLD_ID" &> /dev/null +fi + +echo -e "\n[+] Deploying the container with 'docker run' ('data' as the volume)..." +docker run -it -v ./data:/app/data --name chainmapper-prod -d chainmapper diff --git a/scripts/healthcheck.sh b/scripts/healthcheck.sh new file mode 100644 index 0000000..4c3cd4a --- /dev/null +++ b/scripts/healthcheck.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +# Included into the built image via its Dockerfile + +if [ -s /app/chainmapper.sqlite3 ]; then + exit 0 +else + exit 1 +fi