Marinus Klasen

Marinus Klasen

  • Development
  • Consultancy
  • Developer Resources
  • Blog
  • Contact

May 15, 2021

Auto reload a python script on file change using nodemon

Marinus Klasen

I’m working on a Discord bot with a friend of mine which runs on Python. I’m quite unfamiliar with the language but it bothered me that each time I make a change in code, I need to to restart the bot completely. I did some research and it didn’t seem like there were a lot of standard solutions. The best way to go seems to be unrelated to Python, instead i’m advised to use Node with Nodemon.

To better understand what i’m dealing with I decided to start from scratch.

The goals

  1. Create or find a Dockerfile that fully supports Python and NPM
  2. Integrate this into a docker-compose setup
  3. Start the script on docker-compose up
  4. Restart the script when a file change is detected

The Dockerfile

A quick search led me to nikolaik/python-nodejs:latest Dockerfile. Let’s use that as a starting point. The only thing we have to do is extend it with nodemon. We’ll add this file in the root directory as “Dockerfile”.

FROM nikolaik/python-nodejs:latest

RUN npm install -g nodemon

The docker-compose setup

version: "3"

services:
  python:
    container_name: python-restart-test
    build:
      context: .
    command: python app.py && tail -f /dev/null
    working_dir: /app
    volumes:
      - ./:/app/

This will run the app.py file upon start. For now, we’ll add this to the file:

import os

print("test123")

Upon docker-compose up everything runs fine, “test” is printed in the logs and the container is exited.

Let’s dive into nodemon now. According to this StackOverflow post we can use nodemon as nodemon app.py. Let’s integrate this into the docker-compose.yml file.

version: "3"

services:
  python:
    container_name: python-restart-test
    build:
      context: .
    image: nikolaik/python-nodejs:latest
    command: nodemon app.py
    working_dir: /app
    volumes:
      - ./:/app/

networks:
  default:
    external:
      name: nginx-proxy

Okay, that works. Now the actual app imports some other files and we need nodemon to watch these files as well. I’ll add a folder called ‘inc’ and add 2 .py files here.

Well, apparently nodemon watches subdirectories as well. Looks like we’re good to go!

All files in this project

About Marinus Klasen

Marinus has been working in software/web development for more than a decade. Since 2020 his attention shifted on sharing knowledge and developing products and tools for sharing knowledge.

Marinus Klasen on Twitter

This site runs on Cloudways.
It's fast isn't it?
Cloudways offers high-quality, fast and affordable hosting.

Learn more

Need a hand? Post your project and hire me and other top notch developers on Codeable.

Hire me on Codeable

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Ready to take action?

I'm looking forward to discuss your projects and goals!
Feel free to reach out using the contact details below.

Marinus Klasen

[email protected]
twitter.com/marinusklasen
linkedin.com/in/marinusklasen

  • GitHub
  • LinkedIn
  • Twitter

Have you read?

  • Storing private data with SSH on WPEngine sitesDecember 15, 2022
  • Get the HTML content of a block edit page or post in WordPressNovember 30, 2022
  • Rename Coupon code text to Discount code in WoocommerceNovember 3, 2022
  • Background-size cover in mPDFOctober 22, 2022
  • WordPress.com SSH & duplicating a WordPress.com website locallyOctober 11, 2022

Copyright © 2023 · Marinus Klasen | Webdesign by Team Rood