Skip to content

Instantly share code, notes, and snippets.

@elnygren
Last active April 5, 2025 21:35
Show Gist options
  • Save elnygren/0e081ff7e521dd03a6c861b8740a4b96 to your computer and use it in GitHub Desktop.
Save elnygren/0e081ff7e521dd03a6c861b8740a4b96 to your computer and use it in GitHub Desktop.
Nginx+Docker with autoreload on config changes for local development

Nginx + Docker with autoreload

We build a nginx container that uses inotifywait to check for nginx.conf changes.
This allows us to mount the config to the container for easy editing.
Every time you hit save, watcher.sh calls nginx -s reload.

To get started:

make build
make start
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
RUN apt-get update && apt-get install -y inotify-tools
COPY watcher.sh /watcher.sh
RUN chmod +x /watcher.sh
CMD ["/watcher.sh"]
IMAGE_NAME="org/dev-nginx"
CONTAINER_NAME="org-dev-nginx"
# "public API"
.PHONY: start
start:
docker run -it --rm \
--name $(CONTAINER_NAME) -p 8080:80 \
-v $(PWD)/nginx.conf:/etc/nginx/nginx.conf \
$(IMAGE_NAME)
.PHONY: clean
clean: stop rm
.PHONY: validate
validate:
docker run --rm -t -a stdout \
-v $(PWD)/nginx.conf:/etc/nginx/nginx.conf:ro \
nginx:latest \
nginx -c /etc/nginx/nginx.conf -t
.PHONY: build
build:
docker build -t $(IMAGE_NAME) .
.PHONY: run
run:
docker run --name $(CONTAINER_NAME) -p 8080:80 -d $(IMAGE_NAME)
.PHONY: stop
stop:
docker stop $(CONTAINER_NAME)
.PHONY: rm
rm:
docker rm $(CONTAINER_NAME)
.PHONY: rmi
rmi:
docker rmi $(IMAGE_NAME)
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log debug;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
keepalive_timeout 65;
sendfile on;
gzip on;
#
# Backends
#
upstream backend {
server localhost:8000;
}
#
# Routing
#
server {
server_name myhost default_server;
location / {
proxy_pass http://sumpli-core;
proxy_set_header Host app.sumpli.com;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
#!/bin/bash
nginx_watch () {
while inotifywait -e modify /etc/nginx/nginx.conf; do
echo "Watcher: /etc/nginx/nginx.conf changed, reloading (nginx -s reload) ..."
nginx -s reload
done
}
nginx_watch &
nginx -g "daemon off;"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment