|
|
|
#+title: Справка
|
|
|
|
#+date: [2022-03-19 сб 17:11]
|
|
|
|
|
|
|
|
* Сборка Docker контейнера
|
|
|
|
#+name: Dockerfile
|
|
|
|
#+begin_src sh
|
|
|
|
FROM python:3.9-slim-buster
|
|
|
|
WORKDIR /project
|
|
|
|
|
|
|
|
ENV TELEGRAM_API_TOKEN=""
|
|
|
|
ENV GBOT_DELAY_TIME=1
|
|
|
|
ENV GBOT_DELAY_TIME_Q=180
|
|
|
|
ENV GBOT_CHAT_ID=""
|
|
|
|
ENV GBOT_DB_PATH="/project/data/support.db"
|
|
|
|
|
|
|
|
# без проброса времени бот счиатет что он в GMT+0
|
|
|
|
ENV TZ=Europe/Moscow
|
|
|
|
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
|
|
|
|
|
|
|
|
#RUN useradd -m -r user && \
|
|
|
|
# chown user /project
|
|
|
|
#USER user
|
|
|
|
COPY requirements.txt ./
|
|
|
|
RUN pip install -r requirements.txt
|
|
|
|
|
|
|
|
#USER user
|
|
|
|
|
|
|
|
COPY . .
|
|
|
|
ENTRYPOINT ["python", "main.py"]
|
|
|
|
#+end_src
|
|
|
|
|
|
|
|
После этого в папке с проектом:
|
|
|
|
- собираем бота =docker build -t gorgorod-bot .=,
|
|
|
|
- пробуем запустить =docker run -it --rm --name gorgorod-tg --mount source=gorgorod_data,target=/project/data gorgorod-bot=
|
|
|
|
|
|
|
|
* Systemd сервис
|
|
|
|
Сам файл `docker.gorgorod-bot.service`:
|
|
|
|
|
|
|
|
#+begin_src sh
|
|
|
|
#/etc/systemd/system/docker.gorgorod-bot.service
|
|
|
|
[Unit]
|
|
|
|
Description=Gorgorod bot
|
|
|
|
After=docker.service
|
|
|
|
Requires=docker.service
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
TimeoutStartSec=0
|
|
|
|
Restart=always
|
|
|
|
WorkingDirectory=/home/redmaycry/gorgorod-bot
|
|
|
|
ExecStartPre=-/usr/bin/docker container stop gorgorod-tg
|
|
|
|
ExecStartPre=-/usr/bin/docker rm gorgorod-tg
|
|
|
|
ExecStart=/usr/bin/docker run --rm --name gorgorod-tg --mount source=gorgorod_data,target=/project/data gorgorod-bot
|
|
|
|
ExecStop=/usr/bin/docker container stop gorgorod-tg
|
|
|
|
|
|
|
|
[Install]
|
|
|
|
WantedBy=multi-user.target
|
|
|
|
#+end_src
|
|
|
|
|
|
|
|
После этого:
|
|
|
|
- перезагружаем список демонов =systemctl daemon-reload=,
|
|
|
|
- включаем наш новый демон в автозагрузку =systemctl enable docker.gorgorod-bot.service=,
|
|
|
|
- запускаем сервис =systemctl start docker.gorgorod-bot.service=
|
|
|
|
|
|
|
|
* Использованные материалы
|
|
|
|
- [[https://jugmac00.github.io/blog/how-to-run-a-dockerized-service-via-systemd/][How to Run a Dockerized Service via systemd | Jürgen Gmach]]
|
|
|
|
- [[https://stackoverflow.com/questions/54767751/running-a-docker-container-with-systemd][running a docker container with systemd - Stack Overflow]]
|
|
|
|
- [[https://www.pybootcamp.com/blog/how-to-write-dockerfile-python-apps/][How to write a great Dockerfile for Python apps]]
|