Containerized nginx as a dev server

Written by on Modified on Read time: 2 min

I was looking trough my options for a development dev server, but nothing seemed that good. So I decided to roll my own small script to start a podman container with nginx running in it to serve the files. That way it also matches my production system somewhat, though I did enable things like filelisting, so that developement is easier, and disabled some optimizations and tweaks that I use in prod.

Implementation

I made the following small script, and stored it in /usr/local/bin/serve.sh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#!/bin/bash
set -e
if [ -z "$1" ]; then
  DIR="$(pwd)"
elif [[ -d "$1" ]]; then
  DIR=$(cd "$1"; pwd)
else
  echo "Invalid directory"
  exit 1
fi

echo "Starting server on http://localhost:8080/"

podman run --rm -it -p 8080:8080 \
  -v "$DIR:/var/www:ro" \
  -v /usr/local/etc/nginx-serve.conf:/etc/nginx/nginx.conf:ro \
  -e NGINX_ENTRYPOINT_QUIET_LOGS=1 \
  nginx:alpine

It requires a config for nginx though from /usr/local/etc/nginx-serve.conf, which I made as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
events {
  # Dev server, assuming almost no load
  worker_connections  256;
}

http {
  access_log off;
  error_log /dev/null;
  charset UTF-8;
  server_tokens off;
  client_body_buffer_size 10m;
  client_header_buffer_size 10k;
  client_max_body_size 10m;
  large_client_header_buffers 2 8k;

  sendfile   on;
  tcp_nopush on;
  keepalive_timeout  120;

  server {
    listen 8080 default_server;

    ssi on;
    root /var/www;
    autoindex on;
    try_files $uri $uri.html $uri/index.html $uri/ =404;
  }
}

Now serving some directory is as simple as running serve.sh directory in my terminal to quickly start the server.