0

I have a simple script like

print('hey 01')

and I have dockerized it like below:

FROM python:3.8

WORKDIR /crawler_app

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

CMD ["python", "-u", "boot_up.py"]

and also I have a compose file like below:

version: "3.7"

services:
  db:
    image: mongo:latest
    container_name: ${DB_CONTAINER_NAME}
    volumes:
      - ./mongo-volume:/data/db
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_USER}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_USER_PASS}
    ports:
      - ${EXPOSED_PORT}:27017
    networks:
      - crawl-network

  crawler:
    build: crawler
    container_name: ${CRAWLER_APP_NAME}
    restart: always
    volumes:
      - ./crawler:/crawler_app
    networks:
      - crawl-network
    depends_on:
      - db

networks:
  crawl-network:

The Problem

The problem is that although I have a volume to inside my Docker container, and when I change code in my editor and save it, the source code inside container would update but there is no way to restart the Python script to start with new updated code.

I have searched a lot about this issue and I found some threads on GitHub and Stack Overflow but none of them was not useful to me and I got no answer from them.

My main question is, how can I restart a Python script inside container when I change the source code and save it?

I found a way that mentions to restart your container every time, but I think that there should be a simple way like something like nodemon to JavaScript.

halfer
  • 19,824
  • 17
  • 99
  • 186
amdev
  • 6,703
  • 6
  • 42
  • 64
  • Does this answer your question? [How do i watch python source code files and restart when i save?](https://stackoverflow.com/questions/49355010/how-do-i-watch-python-source-code-files-and-restart-when-i-save). – SuperStormer Sep 07 '20 at 21:36
  • i have read that when i was struggling with issue and , it kinda helps but it is not right way, because i want to do it in python way, not js way with nodemon!, i want my container to not have any kinda extra stuff like npm to be able to run `npm install nodemon`, i want it in pure python! – amdev Sep 07 '20 at 21:39
  • 3rd answer uses `watchdog` from pip – SuperStormer Sep 07 '20 at 21:40
  • i used this command in my Docker file `CMD ["watchmedo", "shell-command", "--patterns='*.py'", "--command='python -u \"boot_up.py\"'", "."]` and it gives me this error `Traceback (most recent call last): File "/usr/local/bin/watchmedo", line 5, in from watchdog.watchmedo import main File "/usr/local/lib/python3.8/site-packages/watchdog/watchmedo.py", line 27, in import yaml ModuleNotFoundError: No module named 'yaml'` – amdev Sep 07 '20 at 21:47
  • Actually, I think you need `pip install watchdog[watchmedo]` to get the extra deps. – SuperStormer Sep 07 '20 at 21:51

1 Answers1

0

If you want to use watchmedo you need to install library for parsing yaml files by command python -m pip install pyyaml

Maksym Sivash
  • 63
  • 1
  • 6