Run nginx in the Docker container

  • 2020-06-12 11:13:46
  • OfStack

nginx profile

Nginx is a performance oriented HTTP server. Compared with Apache and lighttpd, Nginx has the advantages of less memory and higher stability. Compared with the previous version ( < Different from Apache (=2.2), nginx does not adopt the design model of 1 thread per client. Instead, it makes full use of asynchronous logic, which reduces the overhead of context scheduling, so the concurrent service capability is stronger. Modular design is adopted as a whole, with rich module library and the third party module library, flexible configuration. Under Linux, nginx USES the epoll event model, and thanks to this, nginx is quite efficient under Linux. At the same time, Nginx adopts the efficient event model kqueue similar to epoll on OpenBSD or FreeBSD operating systems.

docker hub pull

docker hub is the official mirror source of docker, there is a good nginx docker image, of course, you can also publish your own mirror to the top.

If the official mirror is slow, consider using it

Ali Cloud's docker mirrored warehouse

The main steps are as follows:

Log on to Alicloud's docker mirrored warehouse
Gets the exclusive accelerator address
Upgrade docker client (recommended above 1.6.0)
Modify the daemon configuration file (Ali has given all commands, basically just copy paste)
Then use the

docker pull nginx

You can quickly download the official nginx docker image.

For the basic docker command, see Docker Primer

Nginx docker image

On the official docker page, there are some examples. Can be found that website home directory is/usr share/nginx/html, this search with me 1 some of the article shows that different.

My requirement is to realize the file browsing station built by nginx. To put it bluntly, it's a download site. The lab (on the or campus) shares files. Originally used is Python emergency HttpServer and Ftpserver, although later used python multithreaded start httpserver, but still often because of the card thread problem, resulting in address access failure. So that's fine as a temporary emergency tool, but if you want to share files for the long term, you have to use the fully functional http server.

The nginx configuration files are all under /etc/nginx/, and you can see the familiar conf.d folder, which is clearly the location of the user-defined configuration files.

Modify the custom configuration

The default. conf file reads as follows:


server {
  listen    80;
  server_name localhost;

  #charset koi8-r;
  #access_log /var/log/nginx/log/host.access.log main;
  root  /usr/share/nginx/html;
  location / {
    root  /usr/share/nginx/html;
    index index.html index.htm;
    ##  The following 3 Rows are added. 
    autoindex on;
    autoindex_exact_size on;
    autoindex_localtime on;
  }

  #error_page 404       /404.html;

  # redirect server error pages to the static page /50x.html
  #
  error_page  500 502 503 504 /50x.html;
  location = /50x.html {
    root  /usr/share/nginx/html;
  }

  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  #  proxy_pass  http://127.0.0.1;
  #}

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  #location ~ \.php$ {
  #  root      html;
  #  fastcgi_pass  127.0.0.1:9000;
  #  fastcgi_index index.php;
  #  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  #  include    fastcgi_params;
  #}

  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  #  deny all;
  #}
}

Just copy it in its entirety and add it


autoindex on;
autoindex_exact_size on;
autoindex_localtime on;

run

docker run -p 8001:80 --name mynginx-v /home/embedded_310/haoyueming/2017:/usr/share/nginx/html:ro -v /home/embedded_310/haoyueming/dockerfile/default.conf:/etc/nginx/conf.d/default.conf -d --restart=always nginx
run

There are two ways to start a container, one is to create a new container based on the image and start it, and the other is to restart the container in the terminated state (stopped). The main commands required to create and start are docker run.

-p

Ports bind external ports: ports in containers

� name

Give the container a specific name instead of the automatically generated 1 long string hex.

-v

It is also possible to specify a directory to mount 1 localhost into the container using the -ES130en tag. -ES131en Local folder: folder in the container: read and write permission

-d

More often than not, you need to have Docker run in the background rather than output the results of executing commands directly to the current host. At this point, you can do this by adding the -ES138en parameter.

� restart = always

Using the restart parameter when running the container, you can specify an restart policy that indicates how the container should or should not be restarted on exit.

no wOK container does not restart automatically when it exits. This is the default.

The wok [: max-ES155en] wok will only restart if the container exits with a non-0 status code. Optionally, the number of times docker daemon attempted to restart the container.

The always container will always be restarted regardless of the exit status code. When always is specified, docker daemon restarts the container an unlimited number of times. The container also tries to restart when daemon starts, regardless of the state of the container at the time.

The es167EN-ES168en restart the container regardless of the exit status code, but when daemon starts, do not try to start the container if it has already been stopped.

nginx

The name of the image that represents what image is used as the base package to create a new container.

tips

When I ran it before, 1 was generally interactive:

-i guarantees the container's stdin opening -t generates one tty terminal for the container and adds one /bin/bash at the end of the command to guarantee interaction. In fact, nginx is not running, leading me to believe that the container's port binding is not persistent.

Previous mistakes:

docker run -it nginx /bin/bash


Related articles: