This Docker repository hosts a versatile Python script (on-change.py) designed for development environment automation. The script enables automated actions based on file modifications, including triggering dev deployment through Docker, avoiding the need for continuous integration and continuous deployment (CI/CD) in a local development setup. It's a valuable tool for streamlined development workflows, enabling auto-execution of specified commands when files change, enhancing efficiency and aiding in various use cases such as:
To monitor and continuously perform git pull with the given filepath, detecting changes in the package.json file, and triggering an npm install if changes are detected, you can use the following docker run command:
docker run -ti \
-v /var/run/docker.sock:/var/run/docker.sock \
--privileged \
-e FILES_TO_CHECK=/work-dir/package.json \
-e COMMAND_TO_CHECK=sh -c "/usr/bin/git config --global --add safe.directory /work-dir && /usr/bin/git pull" \
-e COMMAND_TO_RUN="/usr/local/bin/npm install" \
-e CONTAINER_NAME=main-app \
-e SLEEP_INTERVAL=10 \
r3dshadow/docker-onchange-exec:1.1
Explore the https://github.com/r3d-shadow/docker-onchange-exec/tree/main/example example folder to understand how you can structure your project and adapt the Docker Compose configuration.
version: '2.17'
networks:
my-network:
name: my-network
services:
main-app:
restart: always
container_name: main-app
build:
context: .
dockerfile: ./Dockerfile
git-on-change:
depends_on:
- main-app
restart: unless-stopped
image: r3dshadow/docker-onchange-exec:1.1
privileged: true
environment:
- FILES_TO_CHECK=/work-dir/package.json
- COMMAND_TO_CHECK=sh -c "/usr/bin/git config --global --add safe.directory /work-dir && /usr/bin/git pull"
- COMMAND_TO_RUN=/usr/local/bin/npm install
- CONTAINER_NAME=main-app
- SLEEP_INTERVAL=10
volumes:
- '/var/run/docker.sock:/var/run/docker.sock'
This configuration sets up two services: main-app and git-on-change. The git-on-change service will monitor the package.json file and automatically run git pull and npm install on main-app container when changes are detected.