Simple Docker Nginx

May 2, 2019

I’ve been using Docker for a few years now for my local development setup. The isolation Docker provides allows me to make plenty of mistakes installing software and still end up with a pristine environment.

Nginx is one of the containers that’s been a part of my development environment over the past several years. As I’ve learned bits here and there, I’ve refined the way I use the image.

To simplify how I call docker run for Nginx, I created an alias. With the following I simply cd to the location of my index.html and run nginx.

  local tag=`docker images nginx --format "{{.Tag}}"`

  local port="-p 80:80"

  if [[ "$1" =~ ^-?[0-9]+$ ]]; then
    port="-p $1:80"

  docker run --rm -it $port -v `pwd`:/usr/share/nginx/html:ro nginx:$tag

I prefer to grab specific versions instead of using the latest tag. To remove the headache of keeping the alias in sync with which version I’ve installed, I use the docker images command to find the current tag.

Also, you can see in the code above it defaults to port 80, but you can change that by simply passing in the port: nginx 8080.

This alias has made it very simple to get Nginx running quickly.