Skip to content

Instantly share code, notes, and snippets.

@mflorence99
Last active June 10, 2023 22:40
Show Gist options
  • Save mflorence99/989ae26173b1746f7bbf to your computer and use it in GitHub Desktop.
Save mflorence99/989ae26173b1746f7bbf to your computer and use it in GitHub Desktop.
How to Create a No-Cache nginx Docker Container

How to Create a No-Cache nginx Docker Container

Motivation

It took me a long time to figure this out, so I wanted to memorialize the solution I found in case it helps anyone else.

I wanted to run an nginx container to test HTML/JavaScript/CSS code. Actually, I was writing Polymer components and I wanted a build/test workflow different to that recommended using the supplied polyserve mini-server.

That's just a detail, though. The key requirement is that any time a file is changed, the change must be deployed by the nginx server.

Out-of-the-box the standard nginx Docker image won't do this.

The Solution

Let me say right up front that the solution is to configure nginx with sendfile off. Here's a starting point for further research on [StackOverflow] (http://serverfault.com/questions/269420/disable-caching-when-serving-static-files-with-nginx-for-development).

Where I got lost is that I have absolutely no idea how to configure ```nginx`` and I don't really want to invest the time to find out.

The key to the solution is that you can run the nginx container passing it two volume mount points: one for the location of your static files, the other for the nginx configuration.

Configuring nginx

We will configure nginx by copying the container's configuration and modifying it.

  # create a place for nginx to be deployed from on your host filesystem
  mkdir ~/nginx
  mkdir ~/nginx/html
  mkdir ~/nginx/conf
  
  # fire up a temporary nginx container and attach to it
  # we will temporarily share the root of this new directory structure
  docker run -d -v ~/nginx:/usr/share/nginx/html --name temp nginx
  docker exec -it temp bash
  
  # now the command prompt will sat something like:
  # root@43c6a72b689a:/#
  
  # next, copy out the nginx conf files from the container back to the host
  cp -r /etc/nginx /usr/share/nginx/html/conf
  
  # exit from the temporary container
  exit
  
  # remove the temporary container
  docker stop temp
  docker rm temp
  
  # now you can edit ~/nginx/conf/nginx.conf for sendfile off
  
  # I also added expires 0 to ~/nginx/conf/conf.d/default.conf but not sure if necessary
  
  #  location / {
  #      expires 0;
  #      root   /usr/share/nginx/html;
  #      index  index.html index.htm;
  #  }
  
  # finally put and maintain whatever files you need in ~/nginx/html
  # start a real nginx server with something like this
  # note the standard mount points for static files and conf files
  docker run -d -P -p 8080:80 -v ~/nginx/html:/usr/share/nginx/html -v ~/nginx/conf:/etc/nginx:ro --name nginx nginx 

Considerations

I like to start my nginx container with a fixed port so the URL for testing is predictable. I use boot2docker so the IP nginx actually appears to be running is not localhost. Once I've grabbed that IP I add an nginx entry into my hosts file.

Finally it is trivial to add scripting to your .bash_profile (for example) to start and stop the nginx container.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment