Detailed explanation of docker nginx container startup and mounting to local

Detailed explanation of docker nginx container startup and mounting to local

First, the structure inside the nginx container:

Enter the container:

docker exec -it b511b6049f57 bash

View the container structure directory: In fact, each container is equivalent to an independent system.

root@b511b6049f57:/# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr

The structure directory of nginx is in the container:

  • Log location: /var/log/nginx/
  • Configuration file location: /etc/nginx/
  • Project location: /usr/share/nginx/html

If you want to add a location locally, you need to mount the configurations in these containers locally:

The configuration file is relatively troublesome. Generally, nginx only needs to load nginx.conf. In dokcer, nginx.conf is loaded first, and then there is a line in nginx.conf including include /etc/nginx/conf.d/*.conf;, which loads the configuration file in the conf.d directory. So for the configuration, you only need to mount it to conf.d and overwrite it.

Create the corresponding folder and main configuration file nginx.conf locally:

mkdir -p /home/test/nginx/{log,conf,html}
touch nginx.conf

nginx.conf contains sub-configuration files (last line):

user nginx;

worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;


events {
  worker_connections 1024;
}


http {
  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  log_format main '"$remote_addr" "$http_host" "[$time_local]" "$request" "$status" "$body_bytes_sent" '
          '"$bytes_sent" "$gzip_ratio" "$http_referer" "$http_user_agent" "$http_x_forwarded_for" '
          '"$upstream_addr" "$upstream_response_time" "$request_time" "$request_body" "$http_authorization" ';
  access_log /var/log/nginx/access.log main;

  sendfile on;
  #tcp_nopush on;

  keepalive_timeout 65;

  #gzip on;

  include /etc/nginx/conf.d/*.conf;
}

Create a default.conf under conf:

server {

  listen 80;
  server_name localhost;

  #charset koi8-r;
  access_log /var/log/nginx/log/host.access.log main;

  location / {
    #root /data/nginx/html;
    root /usr/share/nginx/html;
    index index.html index.htm;
    #autoindex on;
  #try_files $uri /index/index/page.html;
    #try_files $uri /index/map/page.html;
  }

  #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;
  }

  location ~ /images {
    default_type application/json;
    return 200 '{"code": "A000000", "message": "ok", "timestamp": "20180307184426", "data": {"isvip": "1", "monthProList": []}}';
  }

  # 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;
  #}
}

After preparing the above local files, start the container and mount it to the local related configuration files:

docker run --name docker_nginx -d -p 80:80 \
-v /home/test/nginx/log:/var/log/nginx \
-v /home/test/nginx/conf:/etc/nginx/conf.d \
-v /home/test/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /home/test/nginx/html:/usr/share/nginx/html nginx

###
  The first -v: mount the log directory The second -v: mount the configuration directory The third -v: mount the main configuration file The fourth -v: mount the project directory

After the mounting is complete, visit the main page:

Then access the location /images we wrote in default before:


Restart nginx:

docker exec -it b511b6049f57 nginx -s reload

This is the end of this article about starting and mounting the docker nginx container locally. For more relevant docker nginx startup and mounting content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Implementation of mounting NFS shared directory in Docker container
  • Docker - Summary of 3 ways to modify container mount directories
  • Docker mounts local directories and data volume container operations
  • Solve the problem of docker log mounting
  • Docker starts the elasticsearch image and solves the error after mounting the directory
  • Steps to set up and mount shared folders on Windows host and Docker container
  • Docker deploys nginx and mounts folders and file operations
  • Solve the problem of failure to mount files or directories using ./ relative path in docker run

<<:  A brief introduction to MySQL storage engine

>>:  JavaScript implements the most complete code analysis of a simple magnifying glass (ES5)

Recommend

Two ways to implement HTML page click download file

1. Use the <a> tag to complete <a href=&...

10 tips for designing useful, easy-to-use web applications

Here are 10 tips on how to design better-usable w...

React diff algorithm source code analysis

Table of contents Single Node Diff reconcileSingl...

4 ways to avoid duplicate insertion of data in Mysql

The most common way is to set a primary key or un...

Implementation of Redis master-slave cluster based on Docker

Table of contents 1. Pull the Redis image 2. Crea...

Creating a file system for ARM development board under Linux

1. Please download the Busybox source code online...

React uses emotion to write CSS code

Table of contents Introduction: Installation of e...

Use Shell scripts to batch start and stop Docker services

Table of contents Start Docker Stop Docker Python...

A brief discussion on the role of HTML empty links

Empty link: That is, there is no link with a targ...

Mysql transaction concurrency problem solution

I encountered such a problem during development A...

WeChat applet implements a simple calculator

WeChat applet's simple calculator is for your...

How to change $ to # in Linux

In this system, the # sign represents the root us...