Road docker learning -nginx mirror (translation)

Benpian from https://hub.docker.com/_/nginx/?tab=description

It is nginx docker hub on the official website, there is a description of the use nginx, etc. above. From here you can find nginx mirror on docker using

docker pull nginx

To pull the latest version of nginx docker mirror.

I want to install nginx under docker, but according to the online article has not been enabled, the official translation of what information to record it.

Quick Reference

What is nginx

Nginx (pronounced "engine-x") is an open-source reverse proxy server for HTTP, HTTPS, SMTP, POP3 and IMAP protocol, and a load balancer, HTTP cache and web server (the source). nginx project from highly concerned about high concurrency, high performance and low memory usage begins. It is licensed under the terms of the license similar to the BSD second, and it runs on Linux, BSD variants, Mac OS X, Solaris, AIX , HP-UX and other * nix version. It also provides proof of concept for the ports of Microsoft Windows.

wikipedia.org/wiki/Nginx

How to use the mirror

Some hosting static content

$ docker run --name some-nginx -v /some/content:/usr/share/nginx/html:ro -d nginx

Alternatively, a simple Dockerfile can be used to generate a new image contains the necessary content (which is a much cleaner than the above binding mount solution):

FROM nginx
COPY static-html-directory /usr/share/nginx/html

Place this file in the same directory as the content directory ( "static-html-directory"), run docker build -t some-content-nginx, and then start the container (note the last sentence build surface has a point):

$ docker run --name some-nginx -d some-content-nginx

Exposure to external port

$ docker run --name some-nginx -d -p 8080:80 some-content-nginx

Then you can tap in the browser http://localhost:8080orhttp://host-ip:8080来进行访问。

Complex configuration

$ docker run --name my-custom-nginx-container -v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx

About nginx configuration syntax information, see the official documentation Official Guide (especially Beginner's Guide start guide).

If you want to adjust the default configuration, use the following copy it from running nginx container:

A mirror nginx run # 
$ Docker nginx-RUN---name tmp Container - D nginx
# /etc/nginx/nginx.conf copying files to host /host/path/nginx.conf container file $ docker cp tmp
container--nginx: /etc/nginx/nginx.conf / Host / path / nginx.conf
# delete this container (mandatory) $ Docker RM
-f tmp-nginx-container

This may be a simple Dockerfile (in / host / path /) cleaner done:

FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf

If you add a custom CMD in Dockerfile, must be included in the CMD -g 'daemon OFF;' ; In order to make nginx to stay in the foreground, so Docker correctly tracking process (otherwise the container will stop immediately after the start )!

Is then used docker build -t custom-nginx .to construct a mirror, and the mirror so as to run the following:

$ docker run --name my-custom-nginx-container -d custom-nginx

Use environment variables in nginx configuration

Out of the box, nginx does not support most of the configuration block in environment variables. However, if you need to dynamically generate before you start nginx nginx configuration, you can use envsubst as a solution.

The following is an example of docker-composition.yml (: https: //linux.die.net/man/1/envsubst envsubst description of the command) used:

web:
  image: nginx
  volumes:
   - ./mysite.template:/etc/nginx/conf.d/mysite.template
  ports:
   - "8080:80"
  environment:
   - NGINX_HOST=foobar.com
   - NGINX_PORT=80
  command: /bin/bash -c "envsubst < /etc/nginx/conf.d/mysite.template > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'"

mysite.template template file may contain the following variable references:

listen ${NGINX_PORT};

Read-only mode nginx

To run nginx in read-only mode, it is necessary to mount each roll position Docker nginx writing information. The default configuration requirements for nginx / var / cache and / var / run for write access. This is done by running nginx easy to f

$ docker run -d -p 80:80 --read-only -v $(pwd)/nginx-cache:/var/cache/nginx -v $(pwd)/nginx-pid:/var/run nginx

If you have a more advanced configuration, nginx needs to write another location, you can simply add more volume to mount to these locations.

Debug mode nginx

Since the 1.9.8 version of the image comes nginx-debug binaries, when a higher level of logging, it generates detailed output. Can be replaced with a simple CMD:

$ docker run --name my-nginx -v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx nginx-debug -g 'daemon off;'

Similar disposed docker-compose.yml looks like this:

web:
  image: nginx
  volumes:
    - ./nginx.conf:/etc/nginx/nginx.conf:ro
  command: [nginx-debug, '-g', 'daemon off;']

Amplify using nginx monitoring

Amplify is a free monitoring tool that can be used to monitor micro-service architecture based on nginx. Amplify software developed and maintained by the company behind nginx.

Amplify using across the collection container and the polymerization metrics, key performance data and presenting a consistent set of visualization, such as every second or active connection request. Performance is also very easy to quickly check

In order to use Amplify, it should be installed on a small python agent software (Amplify agent) in a container.

Mirror variants

There are many variations nginx mirror, each designed for a specific use case.

nginx:<version>

This is in fact a mirror image. If you are unsure what your needs are, you might want to use this. It is designed to be discarded as a container (source code and start loading the container to start the application), and the construction of the other mirror base.

nginx:<version>-alpine

This mirror based on the popular Alpine Linux project, can be found in the Alpine official image. Alpine Linux distributions than most base image (~ 5MB) is much smaller, and therefore typically generates leaner image.

When it is desired final image size as small as possible, we strongly recommend the use of this variant. The main issues to be aware of it is that it does use musl libc instead of glibc and friends, so some software may have problems, depending on the depth of their demand for libc. However, most software does not have this problem, so this variant is usually a very safe choice. For more discussion of issues that may arise, as well as the use of some of the pros and cons based on a comparison alpine mirror, see this Hacker News comment thread to see more discussion in this regard.

To minimize the size of an image, you do not typically include other tools (e.g., git or bash) based Alpine image. Use this image as a basis, add the desired content in their own Dockerfile in (if not familiar with how to install the package, see the examples in alpine image description).

Guess you like

Origin www.cnblogs.com/pangjianxin/p/10968056.html