Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of nginx plug-in files under Docker

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "what is the method of nginx plug-in file under Docker", the editor shows you the operation process through the actual case, the operation method is simple and fast, and practical. I hope that this article "what is the method of nginx plug-in file under Docker" can help you solve the problem.

Purpose of the plug-in file:

Files are not constrained by docker image files. You can modify and restart the container. You can use the updated files and will not be restored by the image.

Files, such as logs, recorded during the operation of the container can be automatically saved on external storage and will not be lost due to container restart.

There are two ways to run the container:

Docker run command

Docker-compose command

Docker run command, mount the external host directory to the path in the container through the-v parameter. If there are multiple mount points, you can specify them by multiple-v parameters, and you can only use the absolute path. The docker-compose command is easily described by service. To be exact, a service can contain multiple containers, and the mount configuration of the external path is also configured through the-v parameter. The advantage is that the relative path can be used, of course, relative to the path to the docker-compose.yml file. Another benefit is that docker-compose 's command to start the container is relatively simple.

Suppose the image packaging path structure is as follows: ├── build.sh ├── docker-compose.yml ├── Dockerfile ├── mynginx.conf ├── nginx-vol │ ├── conf.d │ │ └── mynginx.conf │ ├── html │ │ └── index.html │ └── logs │ ├── access.log error.log run.sh

Dockerfile is the configuration file for building images, and the contents are as follows:

FROM nginxLABEL maintainer= "xxx" email= "app=" nginx test "version=" v1.0 "ENV WEBDIR=" / data/web/html "RUN mkdir-p ${WEBDIR} EXPOSE 5180

On the basis of nginx, specify the new data file path as / data/web/html and the exposed port as 5180.

Compile the new image with the following command: docker build-t nginx:test-v1.

The compiled image tag is test-v1, and you can view the local image:

Docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEnginx test-v1 d2a0eaea3fac 56 minutes ago 141MBnginx latest 605c77e624dd 9 days ago 141MB

You can see that the image whose TAG is test-v1 is the new image that has just been compiled.

Create nginx external volume nginx-vol and related conf.d, logs, html folders, and put the corresponding contents in their respective directories. For example, the contents of iindex.html under the html folder are as follows:

System time setInterval ("document.getElementById ('datetime') [xss_clean] = new Date () .toLocaleString ();", 1000)

It's just a page showing the current time.

It is empty under logs to allow the log of the container to be written to external storage. Even if the container is stopped or the image is destroyed, the running log can still be retained.

The following is the personalized configuration of nginx for conf.d. The contents are as follows:

Server {listen 5180; # listen [:]: 5180; server_name localhost; # access_log / var/log/nginx/host.access.log main; location / {root / data/web/html; index index.html index.htm;} # error_page 404 / 404.html # redirect server error pages to the static page / 50x.html # error_page 500 502 503 504 / 50x.htl; location = / 50x.html {root / usr/share/nginx/html; # proxy the PHP scripts to Apache listening on 127.0.0.1 50x.html 80 # location ~\ .php$ {# proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1 fastcgi_index index.php; 9000 # root html; # fastcgi_pass 127.0.0.1 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;}

In fact, the port and root path are modified based on the default default.conf of nginx. The purpose is to show that the configuration file of nginx can also be stored externally. If your own program can modify the configuration file, then in this way, you can modify the configuration file while the container is running. The modified configuration file is actually stored on external storage, so it does not disappear when the container stops running, nor does it revert to mirrored internal files.

Docker run mode

For convenience, you can write the run command to a shell script, such as run.sh, as follows:

Docker run-- name nginx-v1-p 15180 home/project/nginx-test/nginx-vol/conf.d:/etc/nginx/conf.d 5180-v / home/project/nginx-test/nginx-vol/logs:/var/log/nginx-v / home/project/nginx-test/nginx-vol/html:/data/web/html-d nginx:test-v1

You can see that there are three-v in the command, which correspond to different external storage mounts and map to different directories within the container.

The ports after-p (lowercase) are the host port and the container port, respectively, that is, port 15180 of the host is mapped to port 5180 of the container, so that the nginx service port 5180 initiated by the container can be mapped by accessing port 15180 of the host.

View the containers that are running:

Docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMEScf2275da5130 nginx:test-v1 "/ docker-entrypoint." 6 seconds ago Up 5 seconds 80/tcp, 0.0.0.0 5180/tcp 15180-> 5180/tcp,:: 15180-> 5180/tcp nginx-v1

Detailed mapping view:

Docker inspect nginx-v1

The complete information is displayed, and the complete storage mount mapping can be seen in the "Mounts" section.

Looking directly under the nginx-vol/logs of the host, you can see that the nginx running log in the container is automatically written to the storage of the external host.

Ls-l nginx-vol/logs/total 12 root root RW ls-1 root root 1397 January 8 15:08 access.log-rw-r--r-- 1 root root 4255 January 8 15:59 error.log

Stop the container:

Docker stop nginx-v1

Delete the container:

Docker rm nginx-v1docker-compose mode

Install docker-compose

Apt-get install docker-compose

Write docker-compose.yml files

Version: "3" services: nginx: container_name: mynginx image: nginx:test-v1 ports:-80 version 5180 volumes: -. / nginx-vol/html:/data/web/html -. / nginx -vol/logs:/var/log/nginx -. / nginx-vol/conf.d:/etc/nginx/conf.d restart: always

Container_name: specify the container name

Image: the image to use and the corresponding label

Ports: mapping between host port and container port

Volumes: external storage mount mapping

Start the container

Docker-compose up-dCreating network "nginxtest_default" with the default driverCreating mynginx... Creating mynginx... Done

View the container

Docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES635e2999c825 nginx:test-v1 "/ docker-entrypoint." 24 seconds ago Up 22 seconds 80/tcp, 0.0.0.0 5180/tcp 80-> 5180/tcp,: 80-> 5180/tcp mynginx

You can see that the container runs according to the docker-compose.yml configuration, and the port, name and mount are all normal. Access port 80 of the host to correspond to the 5180 service of the container.

Stop the container

Docker-compose downStopping mynginx... DoneRemoving mynginx... DoneRemoving network nginxtest_default

As you can see, it is easier to use docker-compose.

This is the end of the content about "what is the method of nginx plug-in files under Docker". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report