How to setup frontail with Django and Docker
I am trying to setup Frontail to access the logs for a Django application deployed using Docker locally. Has anyone done this before? There is very little documentation or other information online.
I have figured out one solution to this issue. You can install npm inside a Docker image (credit) which then allows you to install Frontail as well. This is done by adding the following code to the Dockerfile:
# Install npm to use frontail
RUN apt-get update && apt-get install -y \
npm
RUN npm install npm@latest -g && \
npm install n -g && \
n latest
# Install frontail
RUN npm i frontail -g
After that, you need to add the Frontail port to the docker-compose.yml file:
ports:
- "XXXX:XXXX"
- "9001:9001"
At this stage you should be able to successfully run Frontail from the Django shell using frontail path_to_log_file but if you want it to automatically start when starting your container, you need to add this to your entrypoint shell script. For example, if in your Dockerfile the entrypoint is run_server.sh and orignally, Django is launched with python manage.py runserver 0.0.0.0:8000, this needs to be changed to:
python manage.py runserver 0.0.0.0:8000 & frontail path_to_log_file
The & allows you to run both commands in parallel. This is not the suggested use of a Docker container (one service per container) (see here and here) but is working for me at the moment.
The best way to use Frontail would be to use it as a separate container but I have still not managed to get that working. It would be great to hear from anyone who has :)
I found a solution by adding Frontail as another service to the docker-compose.yml file. After pulling the Frontail image from Docker Under your other services, you can add:
services:
...
logs:
image: mthenw/frontail
ports:
- "9001:9001"
command: /var/log/log_filename.log
volumes:
- type: bind
source: path_to_logs_directory
target: /var/log
Where log_filename.log is the filename your Django logger is using and path_to_logs_directory is the relative directory of this file. The command argument is what is passed to Frontail (i.e. the logs file to monitor). Note that there is no real reason I used the /var/log directory in the Frontail image - it just seemed to make sense after looking around the file structure.
The only way I could get this to work was with a bind mount thanks to nonNumericalFloat but I'd be interested to know if there is a better way to do this.