Git Product home page Git Product logo

nginx-docker's Introduction

nginx-docker

Based on official nginx Docker image and h5bp, with templating and custom intialization script support.

Rationale

I wanted an nginx image that met the following criteria:

  • Extends the official nginx image.
  • Supports a configuration file template that is evaluated from environment variables, as described in this blog post.
  • Its base configuration stems from best practices outlined in h5bp.
  • Can run arbitrary scripts at startup easily.

Thus, nginx-docker was born.

How it works

Nginx config files are generated from Jinja2 templates. The values of the variables are resolved from the environment variables (ie. those used by Docker and those passed by docker run).

This is what happens when the container runs:

  1. Whatever is in the /templates directory is copied to /etc/nginx.
  2. All files of the form <FILENAME>.j2 (the extension of Jinja2 templates) inside /etc/nginx are found (recursively) and evaluated, leaving them as <FILENAME>. For example, /etc/nginx/sites-enabled/mysite.com.j2 would be stored as /etc/nginx/sites-enabled/mysite.com.
  3. All .sh scripts present in /docker-entrypoint-init.d (if any) are executed.
  4. Finally, nginx starts.

How to use this image

Before you get started, make sure you understand how the official nginx image works, since this one is an extension.

Using docker run

You may run a container from this image with:

docker run --name some-nginx dekked/nginx-docker

If you are not providing custom template files, the default ones for example.com site (based on h5bp) are going to be used. See the templates directory.

If you wish to run with your own templates or scripts, you can do so as follows:

docker run --name some-nginx -v "<YOUR_TEMPLATES_DIR>:/templates" -v "<YOUR_INIT_SCRIPTS_DIR>:/docker-entrypoint-init.d" dekked/nginx-docker

The default templates don't include any variables, but they can be easily added. For example, if your nginx site conf is like:

server {
  listen 80;
  server_name {{ NGINX_CONF_SERVER_NAME }};  # different on dev and prod

  # ...
}

Then you could run like docker run --name some-nginx -v "<PATH>/your-site.j2:/templates/sites-enabled/yoursite.com.j2" -e "NGINX_CONF_SERVER_NAME=yoursite.com" -it dekked/nginx-docker.

Using Docker-Compose

With Docker-Compose, a service may be defined like this:

nginx:
  image: dekked/nginx-docker
  ports:
    - "80:80"
  volumes:
    - <YOUR_TEMPLATES_DIR>/:/templates
    - <YOUR_INIT_SCRIPTS_DIR>:/docker-entrypoint-init.d
  env:
    MY_ENV_VAR: value
  env_file:
    - <PATH>/nginx.env
  # ...

Contributing

You are invited to contribute new features, fixes, or updates, large or small; I am always thrilled to receive pull requests, and do my best to process them as fast as I can.

Authors

nginx-docker's People

Contributors

dekked avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.