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

Looking for a WordPress expert?
Hire top notch developers on Codeable!

Consult an expert

Leave a Reply Cancel reply

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

Get in touch

Contact me using the details below

Marinus Klasen

Wilhelmina van Pruisenweg 35, 2595 AN Den Haag

marinus@mklasen.com
twitter.com/marinusklasen
linkedin.com/in/marinusklasen

  • GitHub
  • LinkedIn
  • Twitter

Have you read?

  • Elementor Widget UsageMarch 27, 2025
  • Programmatically showing popups with ElementorMarch 25, 2025
  • The Elementor sanitize_settings errorSeptember 12, 2024
  • Announcing.. Wooping Shop Health!June 13, 2024
  • Swiper setup when using wp-scriptsMarch 14, 2024

Copyright © 2025 ยท Marinus Klasen | Webdesign by Team Rood