Nikola setup using Docker

Dockerfile

FROM python:3.6
MAINTAINER X

RUN apt-get update \
      && printf 'locales locales/locales_to_be_generated multiselect en_US.UTF-8 UTF-8\nlocales locales/default_environment_locale select en_US.UTF-8\n' | debconf-set-selections \
      && apt-get install --no-install-recommends -y \
              build-essential \
              libjpeg-dev \
              libxml2-dev \
              libxslt1-dev \
              libyaml-dev \
              libzmq3-dev \
              locales \
              python3-dev \
              python3-pip \
              zlib1g-dev \
      && pip install 'Nikola[extras]' \
      && useradd -c Nikola -m -s /bin/bash nikola

WORKDIR /home/nikola/my_blog/
EXPOSE 10000

USER nikola

ENTRYPOINT ["nikola"]

docker-compose.yml

version: "3"
services:
  nikola:
    build: .
    volumes:
      - .:/home/nikola
  auto:
    build: .
    ports:
      - "10000:10000"
    volumes:
      - .:/home/nikola
    command: "auto -p 10000 -a 0.0.0.0"

Initial setup and all nikola command except auto use: docker-compose run --rm nikola <command>

The auto nikola command is used like this: docker-compose up auto. You can now browse your live updated nikola on port 10000.

Restart hanging Docker container

Restart a Docker container if there is no change in its log since last run of script below. This was used on an arm64 with a always deadlocking Docker container. The script was run via cron every 5 minutes.

#!/bin/bash

echo started at $(date)
docker logs --tail=10 DOCKERCONTAINER 2> /tmp/check_new

if cmp -s "/tmp/check_new" "/tmp/check_prev"
then
  echo "restart"
  docker restart DOCKERCONTAINER
fi
cp /tmp/check_prev /tmp/check_prev_prev
mv /tmp/check_new /tmp/check_prev

Push notifications from Python to Phone using pushover

If something strange happens on your server or in your python-app you may want to send a push notification. I chose pushover for this.

Create an app here: https://pushover.net/apps/build.

Your usertoken is on your dashboard mainpage in pushover.

The script needs requests.

import requests
import sys

r = requests.post("https://api.pushover.net/1/messages.json",
                  data={'token': 'APP_TOKEN',
                        'user': 'USER_KEY',
                        'message': u' '.join(sys.argv[1:])})

This can now be called for example like this:

python pushover.py This is my message!

I use this on error in cronjobs for private projects.