Fix #3 add dockerfile

This commit is contained in:
sebthom
2022-01-24 06:25:12 +01:00
parent ba72989a85
commit cc9aaf9a4b
7 changed files with 240 additions and 2 deletions

75
docker/build-image.sh Normal file
View File

@@ -0,0 +1,75 @@
#!/usr/bin/env bash
#
# Copyright (C) 2022 Sebastian Thomschke and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
#
set -eu
#################################################
# execute script with bash if loaded with other shell interpreter
#################################################
if [ -z "${BASH_VERSINFO:-}" ]; then /usr/bin/env bash "$0" "$@"; exit; fi
set -o pipefail
#################################################
# configure error reporting
#################################################
trap 'rc=$?; echo >&2 "$(date +%H:%M:%S) Error - exited with status $rc in [$BASH_SOURCE] at line $LINENO:"; cat -n $BASH_SOURCE | tail -n+$((LINENO - 3)) | head -n7' ERR
#################################################
# determine directory of current script
#################################################
this_file_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd -P)
project_root=$(cd "$this_file_dir/.."; pwd -P)
docker_file="$this_file_dir/image/Dockerfile"
echo "project_root=$project_root"
#################################################
# use .gitignore as .dockerignore
#################################################
cp -f "$project_root/.gitignore" "$project_root/.dockerignore"
#################################################
# specify target docker registry/repo
#################################################
image_repo=kleinanzeigen-bot/kleinanzeigen-bot
image_name=$image_repo:latest
#################################################
# build the image
#################################################
echo "Building docker image [$image_name] from [$project_root]..."
docker image pull python:3-slim || true # ensure we have the latest version of the base image
if [[ $OSTYPE == "cygwin" || $OSTYPE == "msys" ]]; then
project_root=$(cygpath -w "$project_root")
docker_file=$(cygpath -w "$docker_file")
fi
docker build "$project_root" \
--file "$docker_file" \
--progress=plain \
--build-arg BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ") \
--build-arg GIT_BRANCH="${GIT_BRANCH:-$(git rev-parse --abbrev-ref HEAD)}" \
--build-arg GIT_COMMIT_DATE="$(date -d @$(git log -1 --format='%at') --utc +'%Y-%m-%d %H:%M:%S UTC')" \
--build-arg GIT_COMMIT_HASH="$(git rev-parse --short HEAD)" \
--build-arg GIT_REPO_URL="$(git config --get remote.origin.url)" \
-t $image_name \
"$@"
#################################################
# push image with tags to remote docker image registry
#################################################
if [[ "${DOCKER_PUSH:-0}" == "1" ]]; then
docker image tag $image_name docker.io/$image_name
docker push docker.io/$image_name
fi

82
docker/image/Dockerfile Normal file
View File

@@ -0,0 +1,82 @@
#
# Copyright (C) 2022 Sebastian Thomschke and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
#
######################
# runtime image base
######################
FROM python:3-slim as runtime-base-image
LABEL maintainer="Sebastian Thomschke"
ARG DEBIAN_FRONTEND=noninteractive
ARG LC_ALL=C
RUN set -eu \
#
&& apt-get update -y \
&& echo "#################################################" \
&& echo "Install Chromium + Driver..." \
&& echo "#################################################" \
&& apt-get install --no-install-recommends -y chromium chromium-driver \
#
&& rm -rf \
/var/cache/{apt,debconf} \
/var/lib/apt/lists/* \
/var/log/{apt,alternatives.log,bootstrap.log,dpkg.log} \
/tmp/* /var/tmp/*
######################
# build image
######################
# https://hub.docker.com/_/python?tab=tags&name=3-slim
FROM python:3-slim AS build-image
RUN apt-get update \
&& apt-get install --no-install-recommends -y git \
&& python -m pip install --upgrade pip
COPY kleinanzeigen_bot /opt/app/kleinanzeigen_bot
COPY .git /opt/app/.git
COPY *.py *.txt *.toml /opt/app/
RUN cd /opt/app \
&& ls -la . \
&& pip install --user . \
# generates version.py
&& python setup.py --version
######################
# final image
######################
FROM runtime-base-image
COPY --from=build-image /root/.local /root/.local
ARG BUILD_DATE
ARG GIT_COMMIT_HASH
ARG GIT_COMMIT_DATE
ARG GIT_REPO_URL
LABEL \
org.label-schema.schema-version="1.0" \
org.label-schema.build-date=$BUILD_DATE \
org.label-schema.vcs-ref=$GIT_COMMIT_HASH \
org.label-schema.vcs-url=$GIT_REPO_URL
# https://stackoverflow.com/a/59812588/5116073
ENV PYTHONUNBUFFERED=1
ENV DISPLAY=0:0
ENTRYPOINT ["/bin/bash", "/opt/run.sh"]
ENV \
INIT_SH_FILE='' \
CONFIG_FILE=/mnt/data/config.yaml
COPY docker/image/run.sh /opt/run.sh
VOLUME /mnt/data

24
docker/image/run.sh Normal file
View File

@@ -0,0 +1,24 @@
#!/usr/bin/env bash
#
# Copyright (C) 2022 Sebastian Thomschke and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
#
set -e -u
##############################
# execute script with bash if loaded with other shell interpreter
##############################
if [ -z "${BASH_VERSINFO:-}" ]; then /usr/bin/env bash "$0" "$@"; exit; fi
set -o pipefail
trap 'echo >&2 "$(date +%H:%M:%S) Error - exited with status $? at line $LINENO:"; pr -tn $0 | tail -n+$((LINENO - 3)) | head -n7' ERR
if [ -f "$INIT_SH_FILE" ]; then
source "$INIT_SH_FILE"
fi
cd /mnt/data
python -m kleinanzeigen_bot --config $CONFIG_FILE "$@"