-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
66 lines (57 loc) · 3.02 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Dockerfile for FOSS News Telegram Bot.
#
# Copyright (C) 2021 PermLUG
#
# This file is part of fossnewsbot, FOSS News Telegram Bot.
#
# fossnewsbot is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# fossnewsbot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# This Docker image is based on Debian Bullseye instead of Alpine
# because the latter uses musl and therefore does not support locales.
FROM python:3.9-slim-bullseye as build
# Copy source files for venv and locales got the image
COPY requirements.txt /
COPY locales /locales
# Create Python virtual environment, install dependences, compile translations.
RUN set -eu;\
apt-get update -qq; apt-get install -qqy gcc gettext; rm -rf /var/lib/apt/lists/*;\
python -m venv /venv; . /venv/bin/activate; pip install --upgrade pip; pip install -r /requirements.txt;\
for po in /locales/*/LC_MESSAGES/*.po; do msgfmt --output-file="${po%.po}.mo" "$po"; rm "$po"; done
FROM python:3.9-slim-bullseye as fossnewsbot
ARG lang="ru_RU.UTF-8"
ARG tz="Asia/Yekaterinburg"
ARG workdir="/srv/fossnewsbot"
# Install and setup locales and timezone; create user `bot` and working directory.
RUN set -eu;\
apt-get update -qq; apt-get install -qqy locales; rm -rf /var/lib/apt/lists/*;\
echo "en_US.UTF-8 UTF-8\n${lang} ${lang#*.}" >/etc/locale.gen; locale-gen; echo "LANG=$lang" >/etc/default/locale;\
echo "en_US\ten_US.UTF-8\nen\ten_US.UTF-8\n${lang%.*}\t${lang}\n${lang%_*}\t${lang}" >/etc/locale.alias;\
echo "$tz" >/etc/timezone; ln -sf "/usr/share/zoneinfo/$tz" /etc/localtime;\
useradd --create-home --comment='FOSS News Bot' --user-group bot; yes bot | passwd bot;\
mkdir -p "$workdir"
# Copy bot code and configuration to the image
WORKDIR "$workdir"
COPY --chown=bot:bot cache.py config.yml .secrets.yml entrypoint.sh ./
# Docker (up to version 20.10) cannot copy multiple directories in a single layer (`COPY` command), its content only.
# See https://stackoverflow.com/questions/26504846/copy-directory-to-another-directory-using-add-command.
# So copying directories one by one.
COPY --chown=bot:bot fossnewsbot fossnewsbot
COPY --from=build --chown=bot:bot /venv venv
COPY --from=build --chown=bot:bot /locales locales
# Change user AFTER all chowns
USER bot
# Fix venv directory in the activation script and make entrypoint script executable
RUN sed "/^VIRTUAL_ENV=/s:/:$PWD/:" -i venv/bin/activate && chmod +x entrypoint.sh
ENV PATH="$workdir:$PATH" LANG="ru_RU.UTF-8" LC_MESSAGES="en_US.UTF-8"
ENTRYPOINT ["entrypoint.sh"]
CMD ["python", "-m", "fossnewsbot"]